rambokayambo
rambokayambo

Reputation: 341

passing a vector member variable by reference to another class method

NOTE: I use pseudocode in my question

lets say i have a class called circle with an interface called circle.h which i also have a method called readdata but this is defined in another class called rectangle(rectangle.h is the interface ) i want to call the method readdata in my circle class and pass in my private member variable which is a vector. How can this be done? is it correct to pass in a PRIVATE member variable by reference to another class. Isn't this defeating the whole purpose of having private member variables because now i am giving class rectangle access to circle class vector variable since i pass it in by reference. Here is how i do it(psuedocode)

circle.h

private:
vector<struct> vect;

public:
dataread()

circle.cpp

rectangle.h

readdata(vector &)




method dataread() //class method to fill up my struct
{
 rectangle::readdata(vect);   //i call rectangle readdata method but i
 pass in a reference to my memebr variable....is this safe?
}

should i just declare the vector locally(in dataread method) and assign it to the reference? any help would be greatly appreciated. Right now it compiles but i have been told this is not good programming practice

Upvotes: 1

Views: 2193

Answers (3)

Razvan
Razvan

Reputation: 10093

Passing a pointer to a "private memory area" is not necessarily against the encapsulation idea because the object owning that memory area decides whom to allow access to. It doesn't allow "anyone to access it". On the other hand this doesn't look very natural.

You should return a pointer from the readdata method and use it in your circle instance. At the same time doing this breaks a principle which I, personally, use: the one who allocates memory is responsible for it so it should also be the one frees it when appropriate. Taking this into consideration, it would probably be a good idea to return the actual vector and not a pointer to it (but this means copying a large amount of memory in case you're not using a compiler with "return value optimization").

Upvotes: 0

IronMensan
IronMensan

Reputation: 6831

There is nothing wrong with passing references to private members to methods in other classes. For example:

void myClass::myMethod() {
    std::copy(myVector1.begin(), myVector1.end(), myVector2.begin());
}

While that doesn't pass a reference to myVector1 directly, it does pass a writable iterator which is just about the same thing. The class is making a request for some object/function to do something with its data. So long as that other object/function only does what it is supposed to do, there's no problem.

Upvotes: 1

Rocky Pulley
Rocky Pulley

Reputation: 23301

I think I get what you are asking. Yes, you can pass a reference to your private data and No, you shouldn't do it. You can pass a const reference so that it can't be modified or pass a new vector with the contents copied. The best thing to figure out is why you need to do it that way, then figure out the best method for getting the data there.

Upvotes: 0

Related Questions