Jounathaen
Jounathaen

Reputation: 890

Access private member vector of multiple objects in other class

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:

Upvotes: 1

Views: 538

Answers (1)

Mr.C64
Mr.C64

Reputation: 42944

Writing a getVector() function in class A, which returns a reference to the Vector vec_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?

If B just needs to observe the content of vec_A, consider returning a const reference (const &):

class A {
 ...
  public:

    // Give read-only access to vec_A
    const std::vector<DataStruct> & getVector() const {
        return vec_A;
    }
};

I prefer this solution to just making the vector data member public: with accessors (getter/setter) you have better control. For example you can implement a read-only access (like shown above). Or, if you want to provide a setter, you can also implement some checking on the input argument, etc.

Upvotes: 1

Related Questions