There is a code:

class _class { private: int val; public: _class() { val = 0; } _class(int i) { val = i; } int get(_class &ob) { return ob.val; // почему свойство val доступно? "ob" же это объект // и свойство "val" у него private ^_^ } }; int main(int argc, char *argv[]) { _class ob, ob1(10); cout << ob.get(ob1) << endl; return 0; } 

The same will happen if I stupidly create an object of class "_class" in the "get" method and try to get the variable "val", then I will get it without any problems. I do not understand something?

    2 answers 2

    It should be so. private operates at the class level, not a single object. It does not restrict access to members of other objects of its class.

      Attributes are private to the class, which means that these attributes are not available anywhere except for the methods of the class and its friends. _class::_get is a class class method _class , so the private attributes of class _class are available inside this method, which we actually see.