I have two classes (A & B) with a similar structure, which both contain a vector of structs.
class A/B{
private: std::vector<DataStruct> vec_A/vec_B;
...
public:
...
}
To create/update an object of B, I have to combine the data from the vectors of multiple objects of class A (combining similar entries in the vector into one entry in the vector of object B and do data transformations in B).
How do I do this?
My thoughts were:
- Making class B a friend of A. But I've heard, that making classes friends in C++ is only the last resort and should be avoided.
- Writing a
getVector()
function in class A, which returns a reference to the Vectorvec_A
, so that class B can operate on the data. For me this seems to simply bypass the data encapsulation. What would be the advantage to setting the vector to public? - Writing a
getVector()
function in class A, which returns a copy ofvec_A
. But asvec_A
is not that small, this seems to be inefficient (ok, only a few kB. Should work but in general...) - Write a function for class A, which does the calculation and creates an object of class B. Then concatenate multiple objects of B into one final object of B. However, this seems to be a lot of overhead to me.
Aucun commentaire:
Enregistrer un commentaire