Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do objects of the same class have access to each other's private data?

Why do objects of the same class have access to each other's private data?

class TrivialClass { public:    TrivialClass(const std::string& data) :     mData(data) {};    const std::string& getData(const TrivialClass& rhs) const {     return rhs.mData;   };  private:   std::string mData; };  int main() {   TrivialClass a("fish");   TrivialClass b("heads");    std::cout << "b via a = " << a.getData(b) << std::endl;   return 0; } 

This codes works. It is perfectly possible for object a to access private data from object b and return it. Why should this be so? I would think that private data is private. (I started out by trying to understand copy constructors in the pimpl idiom, but then I discovered that I didn't even understand this simple situation.)

like image 359
Keith Avatar asked Aug 03 '11 03:08

Keith


People also ask

Can objects of same class access each others private fields?

Any method within your class can access the private data of any instance of that class; there's not a way to keep data private to within an instance unless you forbid methods that explicitly access private data members of other instances.

Can two objects from the same class access each other's private variables?

This is perfectly legal. Objects of the same type have access to one another's private variables. This is because access restrictions apply at the class or type level (all instances of a class) rather than at the object level (this particular instance of a class).

Can objects access private data members?

Private: The class members declared as private can be accessed only by the functions inside the class. They are not allowed to be accessed directly by any object or function outside the class. Only the member functions or the friend functions are allowed to access the private data members of a class.

Can a private member of a class access the methods of the same class?

You can access private members from any code block that is defined in the same class. It doesn't matter what the instance is, or even if there is any instance (the code block is in a static context). But you cannot access them from code that is defined in a different class.


1 Answers

Because that's how it works in C++. In C++ access control works on per-class basis, not on per-object basis.

Access control in C++ is implemented as a static, compile-time feature. I think it is rather obvious that it is not really possible to implement any meaningful per-object access control at compile time. Only per-class control can be implemented that way.

Some hints of per-object control are present in protected access specification, which is why it even has its own dedicated chapter in the standard (11.5). But still any per-object features described there are rather rudimentary. Again, access control in C++ is meant to work on per-class basis.

like image 187
AnT Avatar answered Sep 17 '22 17:09

AnT