First, Introduction
Looking at a piece of code found in the project today, it is possible to re-access the private members of the class's object in the class's member function. It feels incredible.
To write your own instance code:
#include <iostream>using namespacestd;classctest{ Public: CTest (intN) {m_a=N; } ctest (CTest&t) {m_a=T.m_a; } Public: voidPrint () {cout<< M_a <<Endl; }Private: intm_a;};intMainintargcChar**argv) {CTest T (Ten); CTest T2 (t); T.print (); T2. Print (); return 0;}
This code can be run correctly.
Ii. Conclusion
When learning, only remember that private members of a class cannot be accessed externally. So it is considered absolutely impossible to access the object T.m_a in a way that can actually be used. It is accessed inside the class, although it is inside another object.
So how does it come about that the private members of the object can be accessed in other objects of the same kind, but not accessible in objects of different classes?
A private member of C + + that accesses an object inside a class