Consider this:
struct b { b() { cout << "b()" << endl; } ~b() { cout << "~b()" << endl; } }; struct a { b ob; a() { cout << "a()" << endl; } ~a() { cout << "~a()" << endl; } }; int main() { a oa; }
"Then destructor A will call destructor B since it belongs to him." This is not the right way to call destructors in the case of compound objects. If you see the example above, a destroyed first, and then b destroyed. a destructor will not call handle b so that the control returns back to a destructor.
"What will be the safe way to access A in destructor B?" . According to the above example, a already destroyed, so a not available in the b destructor.
", since we can also be in the destructor A). This is not true. Again, when the control leaves the destructor a , then only the control enters the destructor b .
A destructor is a member function of class T. After the control exits the destructor, class T cannot be accessed. All class T data members can be accessed in class constructors and destructors in accordance with the above example.
sameerkn
source share