One: the memory model under C + + single inheritance:
A), simplest of a single inherited memory model: No virtual function member in base classes and derived classes:
#include <iostream>classbase{ Public: Base (Char_x =' /'): m_x (_x) {}~Base () {}Private: Charm_x;};classDerived: Publicbase{ Public: Derived (Char_x =' /',int_y =0,int_s =0): Base (_x), m_y (_y), M_z (_s) {}~Derived () {}Private: intm_y; intm_z;};intMain () {Derived D ('A', -, -); return 0;}
Under MSVC2015 Debugx86, &d = 0x00eff88c;&d.m_x = 0x00eff88c;&d.m_y = 0x00eff890;&d.m_z = 0x00eff894; As for d.m_ Why does x take up 4 bytes of space, which is related to memory alignment, thus, in the case of a single inheritance and non-virtual function member in a class, the memory model in the derived class: address from low to high, are: the base class sub-object data member address in the derived class object (the order is related to the data member declaration in the base class), the derived class-specific object data member address (IBID.);
b), single inheritance under virtual function member case:
#include <iostream>classbase{ Public: Base (Char_x =' /'): m_x (_x) {}Virtual voidShow () {std::cout<<"This is baseclass"<<Std::endl;} ~Base () {}Private: Charm_x;};classDerived: Publicbase{ Public: Derived (Char_x =' /',int_y =0,int_s =0): Base (_x), m_y (_y), M_z (_s) {}Virtual voidShow () {std::cout <<"This is Derived"<<Std::endl;} ~Derived () {}Private: intm_y; intm_z;};intMain () {Derived D ('A', -, -); return 0;}
Also in the above environment, take &d = 0x0133f730;&d.m_x = 0x00133f734;&d.m_y = 0x00133f738;&d.m_z = 0x00133f73c; discover &d! =&d.m_x, this is due to the fact that in this four-byte memory space, a pointer to virtual function table (which can be understood as an array) is stored in this table, which holds the virtual function address in the class. Vptr points to the address of the first virtual address declared in the class vptr; From this you can see that in a single inheritance model, the memory model of the derived class object: address from low to high, respectively: the address that the vptr points to, The base class sub-object data member address in the derived class object (the order is related to the data member declaration in the base class), the derived class-specific object data member address (IBID.);
Two: C + + multiple inheritance under the memory model:
This is also C + + with other object-oriented programming languages such as: java,c# a very big difference, in Java and C # in the elimination of multiple inheritance, instead of inheriting the interface through the way of implementing a multi-inheritance;
#include <iostream>classbase1{ Public: Base1 (Char_x =' /'): m_x (_x) {}Virtual voidShow () {std::cout<<"This is base1class"<<Std::endl;} ~Base1 () {}Private: Charm_x;};classbase2{ Public: Base2 (int_a =0): M_a (_a) {}Virtual voidDisplay () {std::cout <<"This is base2class"<<Std::endl;}Private: intm_a;};classDerived: Publicbase1,base2{ Public: Derived (Char_x =' /',int_a =0,int_y =0,int_s =0): Base1 (_x), Base2 (_a), m_y (_y), M_z (_s) {}Virtual voidShow () {std::cout <<"This was Derived inherited by Base1"<<Std::endl;} Virtual voidDisplay () {std::cout <<"This was dervied inherited by Base2"<<Std::endl;} ~Derived () {}Private: intm_y; intm_z;};intMain () {Derived D ('A',Ten, -, -); return 0;}
The memory addresses are distributed as follows:
As you can see, in the derived class object model, the address is low to high, respectively: When inheriting the declaration, the vptr of the first base class child object in the derived class, the address of the first base class child object in the derived class, the vptr of the second base class child object in the derived class, the address of the second base class child object in the derived Thus the analogy;
Summary: From the above analysis, in the memory model of the derived class object, the address from low to high, respectively: the base class vptr the address----The base class sub-object address---derived class vptr the address----the address of the specific object in the derived class:
It is worth mentioning that the memory model of an object is more complex in the case of virtual inheritance, which is similar to diamond inheritance;
The memory model of C + + single and multiple inheritance