C ++ Object Memory layout -- ④ vs compiler -- a single virtual inheritance
Under the vs2005 compiler, it is proved that the memory layout inherited by a single virtual machine must contain virtual base table pointers no matter whether there are virtual functions or not. The content in the virtual base table is the offset of the class instance and the relative offset value of the base class instance.
If there are virtual functions, the virtual function tables of the base class are separated from the virtual function tables of the derived classes.
In the memory layout, the address ranges from low to high. The sequence is as follows: virtual function table pointer of the derived class, virtual base class Table pointer, member variable of the derived class, and virtual function table pointer of the base class, the member variable of the base class.
That is to say, the derived class is on and the base class is under. This is opposite to normal inheritance.
In particular, the GNU gcc compiler is different from vs in dealing with virtual inheritance. Its memory layout is: the virtual function table of the derived class is merged with the virtual base class table and analyzed separately.
In addition, if the derived class implements the virtual function of the base class, there will be an additional interval of 0 between the object of the derived class, the derived class, And the instance of the base class.
// Vs compiler -- a single virtual inheritance. CPP // 2010.8.18 # include <iostream> using namespace STD; //////////////////////////////////////// /// // class base {public: base (int A = 10): A (a) {cout <"base: Base ()" <Endl;} virtual void show1 () {cout <"base: show1 ()" <Endl;} PRIVATE: int ;}; //////////////////////////////////////// /// // class derived: virtual public base {public: de Rived (int B = 100): B (B) {cout <"derived: derived ()" <Endl;} virtual void show2 () {cout <"derived: show2 ()" <Endl ;}private: int B ;}; //////////////////////////////////////// /// // int main () {derived OBJ; int ** P = (INT **) & OBJ; typedef void (_ thiscall * Fun) (void * pthis ); // very important cout <"virtual inherits the object memory layout of the derived class of the base class:" <Endl; For (INT I = 0; I! = Sizeof (OBJ)/4; ++ I) {cout <p [I] <Endl ;}cout <Endl <"first item of the first virtual function table, virtual function derived: show2 () Address: "<(int *) P [0] [0] <Endl; (fun) (P [0] [0]) (p); cout <"the first entry in the second virtual function table. The virtual function base: show1 () Address: "<(int *) P [3] [0] <Endl; (fun) (p [3] [0]) (p + 3 ); cout <Endl <"first entry of the virtual base table, address of the object in this category-address of the virtual base table pointer =" <(int *) P [1] [0] <Endl; cout <"second entry of the virtual base table, base Class Object address-virtual base class Table pointer address = "<(int *) P [1] [1] <Endl; System (" pause "); return 0;}/* base: Base () derived: derived () virtually inherits the object memory layout of the derived class of the base class: 0041c2f80041c2fc000000640041c2f000000000000a first item of the first virtual function table, virtual function derived: show2 () Address: 00401280 derived: show2 () second virtual function table first item, virtual function base: show1 () Address: 00401250 base :: show1 () indicates the first item of the virtual base table. The address of this Class Object-the second item of the virtual base table pointer address = fffffffc, base Class Object address-virtual base class Table pointer address = 00000008 press any key to continue... */