c++ question

Status
Not open for further replies.

AtlasDreams

Wise, Aged Ars Veteran
119
recently in my c++ class i fellow student stumbled upon something that i though was quite interesting

say for instance, we have a linked list class, the following being an implementation of one of the member functions

void List::Whatever(List a)
{
cout<<a.mySomething; //mySomething is a private data member
}

the compiler does not report an error, and when executed everything works fine
how come this doesn't violate encapsualtion? why does the List class have access to the private data of a parameter of type "List"?
accessing private data of a different object would cause errors...but why not when the object is what you are implementing?
sorry if not alot of this makes sense, im only in high school and not an incredibly experienced programmer
Atlas
 

sevensor

Wise, Aged Ars Veteran
109
The interesting thing is that you can get at the private member data of another object of the same type from within one of the member functions. So if we have:
code:<HR>
Code:
List a, b;
b.Whatever(a);
It's all right that b.Whatever is messing with the private data of a.
Any member function of the class has access to any member variable of any instance of that class.
 
Status
Not open for further replies.