Accessing private member vectors in a class?

gotkilled

Limp Gawd
Joined
May 19, 2005
Messages
448
In my class i have vector<Person*>members; as a private member. Is there any way I can access the information out of this vector even though it is private?

class test
{
public:
//....

private:
vector<Person*>members;

};

I've tried accessing it w/ test.members;, but the compiler does not let that go through. I'm very confused at all this....

I know that it's near impossible to access things in the private area of classes, but everything has been stored in vector<Person*> members; :confused:
 
Store it as a public member? That will allow it to be accessed from outside the class. And if it has to be private for some reason, just make a public function that will return its value...could also write a function that allows you to modify the value, though that makes the fact that it's private kinda useless.
Code:
 class test {
 public: 
 vector<Person*>members;
 private: 
 ...
 };
 
Ok, I made it back to public, but I'll probably have to make it private again. Anyway, I'm trying to access it by

test.members;

Is this correct?
 
gotkilled said:
Anyway, I'm trying to access it by

test.members;

Is this correct?


No. test is the name of the class; you would have to access members through an instantiated object of type test. If you have a test object named, say, obj, you'd say obj.members to access the ith member of the members vector.
 
Friend classes, accessors, or making it public.

Code:
Person* test::getPerson(int LongVariableNamesAreFun)
{
   return members[LongVariableNamesAreFun];
}

I think that would work.
 
I don't think you want a vector of Person pointers; I think you want a vector of Person objects.
 
Back
Top