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
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