In the article blog.csdn.net/libaineu200… In, I found:

// Copy constructor String::String(const String &other) {// Input parameter is const int length=strlen(other.m_data);Copy the code

Why can other access private variables?

Because: Access control permissions are based on classes, not objects. For this reason, member functions can access the private member variables of objects of that class, but not the private members of objects of other classes.

C++ qualifiers are class qualifiers, not object qualifiers, as long as the same type can access each other. Both are of the same type, so you can access them directly, but you need to specify which object they are. Access permissions (e.g., public,private) are for “classes”, not “objects”. Private access permissions are inaccessible to other classes, not to different objects of the class. In fact, this is very reasonable, the class is designed by themselves, of course, they also know the internal structure of the class, so there is no need to “encapsulate” their own class. For the data members that are allowed to access objects in member functions, on the one hand, security and encapsulation are guaranteed, and on the other hand, convenient operation is provided. Only member functions can access private members. Friends and derivations are not involved. In this way, security is still guaranteed and encapsulation is completed. For the second statement, imagine how inconvenient it would be if you had to rely on interfaces to transfer data. Since there is enough security and encapsulation in member functions, it does not make sense to resort to interfaces. As a flexible handling of data members, the designer allows access to private members of objects in member functions, which provides great convenience for users. It also reflects the flexibility and principle of language.

In plain English, a member function of its own class can access private variables. Private data members can be accessed from class objects.