Protected / Private Variables accessable.
Yop guys,
I have this bizzare (to me) question. I have a simple class:
Code:
class Koala
{
public:
Koala();
Koala(const char* name);
~Koala();
Koala& GetPartner();
void KillPartner();
private:
const char* _name;
Koala* _partner;
bool _hasPartner;
};
Here is one of the methods:
Code:
void Koala::KillPartner()
{
Koala *tmp;
if (this->_hasPartner == true)
{
tmp = &(this->GetPartner()); // So I have his next partner which is another instance of the object.
//And here I can access all of his private variables...
delete tmp;
}
}
Now I am able to access ALL the private variables, of all my different instances of the object. (Koala A; Koala B; And Koala B can access private variables of Koala A as long as I have a pointer to the object).
I compile like this: "g++ -W -Wall *.cpp".
So my question.. How come it works? Arent Private/Protected variables supposed to be protected from each other? Or are all private variables, private between all the instances of the same object.
Thank you guys!