When looking at <com essence>, there is a small problem: if Class A contains class B instances, Class A dynamically obtains the address of Class A when class B's member functions are executed.
After thinking for a while, I wrote the instance Code as follows:
Class Croom
{
Public:
Class bed
{
Public:
Int m_member1;
Int m_member2;
};
Class door
{
Public:
Int m_member1;
Void open ()
{
Cout <"open operation" <Endl;
}
Int getparentclsthispointer ()
{
Croom * plobj = NULL;
Int lioffset = reinterpret_cast <int> (& plobj-> mdoor); // obtains the offset of A Class door instance in a Croom instance.
Int liparentclsthispointer = reinterpret_cast <int> (this)-lioffset; // obtain the address of the Croom instance.
Return liparentclsthispointer;
};
};
Bed mbed;
Door mdoor;
};
Class Croom contains an instance of the door class.GetparentclsthispointerDynamically obtain the address of the class Croom instance.