first, the object model in C + +
1. Concept
The part of the language directly supports the object-oriented programming;
The underlying implementation mechanism for various support. (Not understand ...) )
2. Classification of Members in classA) member functions
I. Static function
II. Non static function
III. virtual function
b) Data members
I. Static member Data
II. Non static member data
3. C + + object modelA) The class object memory layout includes
I. Non STATIC member data
Ii. vptr (virtual function table pointer)
Iii. vbptr (Virtual base class table pointer)
b) does not include
I. Static member data (stored in the static storage area)
II. member functions (stored in code area)
c) Virtual table
Abbreviation VTBL. Holds pointers that point to each virtual function of the class. function addresses in a virtual table are arranged in the order in which they are declared. VTBL is formed after the class declaration, and vptr is generated by the compiler.
d) The location of the vptr is typically placed at the forefront of a class object. e) Virtual base class table
Vbptr points to a table that holds virtual inheritance in which the virtual base class stores offsets relative to the virtual base class table pointer.
ii. Types of inheritance
1, ordinary inheritance (does not contain virtual functions)A, single inheritance
Class Base{public: base (int a = 1): Base (a) {} void Fun0 () {cout << Base << Endl;} int base;}; Class Derive:public Base{public: Derive (int a = 2):d erive (a) {} void Fun1 () {cout << base1 << Endl;} int derive;};
B, multiple inheritance
Class Base1{public: Base1 (int a = 2): Base1 (a) {} void Fun1 () {cout << base1 << Endl;} int base1;}; Class Base2{public: Base2 (int a = 3): Base2 (a) {} void Fun2 () {cout << base2 << Endl;} int base2;}; Class Derive:public Base1, public base2{public: Derive (int value = 4):d erive (value) {} void Fun3 () {cout << Derive << Endl;} int derive;};
C, Diamond Inheritance Press CTRL + C to copy code<textarea></textarea>Press CTRL + C to copy the code
Note: There is ambiguity in the diamond inheritance, and the compilation does not pass, and the base class variable can only be accessed by specifying a particular base class.
Derive D;
D.base = 3; Not correct
D.base1::base = 3; That's right
2. Normal inheritance (contains virtual functions)A, single inheritance (contains virtual functions)
Class Base{public: base (int a = 1): Base (a) {} virtual void Fun0 () {cout << Base << Endl;} int base;}; Class Derive:public Base{public: Derive (int a = 2):d erive (a) {} virtual void Fun0 () {}; virtual void fun1 () {cout << derive << Endl;} int derive;};
Note: The new virtual function in the derived class is appended to the virtual function table.
B, multiple inheritance (contains virtual functions)
Class Base1{public: Base1 (int a = 2): Base1 (a) {} virtual void fun1 () {cout << base1 << Endl;} int base1;}; Class Base2{public: Base2 (int a = 3): Base2 (a) {} virtual void fun2 () {cout << base2 << Endl;} int base2;}; Class Derive:public Base1, public base2{public: Derive (int value = 4):d erive (value) {} virtual void Fun3 () {cou T << derive << Endl;} int derive;};
Note: The new virtual function in a derived class is appended to the virtual function table of the first base class.
C, Diamond Inheritance (contains virtual functions)
Class Base{public: base (int a = 1): Base (a) {} virtual void Fun0 () {cout << Base << Endl;} int base;}; Class Base1:public Base{public: Base1 (int a = 2): Base1 (a) {} virtual void fun1 () {cout << base1 << Endl ;} int base1;}; Class Base2:public Base{public: Base2 (int a = 3): Base2 (a) {} virtual void fun2 () {cout << base2 << Endl ;} int base2;}; Class Derive:public Base1, public base2{public: Derive (int value = 4):d erive (value) {} virtual void Fun3 () {cou T << derive << Endl;} int derive;};
Note: analysis, from top to bottom in turn. There are two problems of ambiguity and memory redundancy.
3. Virtual inheritance (does not contain virtual functions)
New virtual base class pointers, pointing to the virtual base class table, the first item in the Virtual base class table, stores the offset of the virtual base class pointer, and then stores the offset of the virtual base class ( offset is the storage address relative to the virtual base class table pointer ).
A, single virtual inheritance (does not contain virtual functions)
Class Base{public: base (int a = 1): Base (a) {} void Fun0 () {cout << Base << Endl;} int base;}; Class Base1:virtual public base{public: Base1 (int a = 2): Base1 (a) {} void Fun1 () {cout << base1 << Endl ;} int base1;};
B, multiple virtual inheritance (does not contain virtual functions)
Class Base1{public: Base1 (int a = 2): Base1 (a) {} void Fun1 () {cout << base1 << Endl;} int base1;}; Class Base2{public: Base2 (int a = 3): Base2 (a) {} void Fun2 () {cout << base2 << Endl;} int base2;}; Class Derive:virtual public Base1, virtual public base2{public: Derive (int value = 4):d erive (value) {} void Fun3 ( ) {cout << derive << Endl;} int derive;};
C, Diamond Virtual inheritance (does not contain virtual functions)
The first form of:
Class Base{public: base (int a = 1): Base (a) {} void Fun0 () {cout << Base << Endl;} int base;}; Class base1:virtual Base{public: Base1 (int a = 2): Base1 (a) {} void Fun1 () {cout << base1 << Endl;} int base1;}; Class base2:virtual Base{public: Base2 (int a = 3): Base2 (a) {} void Fun2 () {cout << base2 << Endl;} int base2;}; Class Derive:virtual public Base1, virtual public base2{public: Derive (int value = 4):d erive (value) {} void Fun3 () {cout << derive << Endl;} int derive;};
Note: When analyzing the memory distribution of a derived class, it is also analyzed from top to bottom. Virtual inheritance places the base class at the end of memory, but the order at the end is also in a certain order. First the base is placed at the end, then the BASE1 is placed at the end, and the last Base2 is placed at the end.
The second form of:
Class Base{public: base (int a = 1): Base (a) {} void Fun0 () {cout << Base << Endl;} int base;}; Class Base1:virtual public base{public: Base1 (int a = 2): Base1 (a) {} void Fun1 () {cout << base1 << Endl ;} int base1;}; Class Base2:virtual public base{public: Base2 (int a = 3): Base2 (a) {} void Fun2 () {cout << base2 << Endl ;} int base2;}; Class Derive:public Base1, public base2{public: Derive (int value = 4):d erive (value) {} void Fun3 () {cout << ; Derive << Endl;} int derive;};
Note: The principles of analysis, from top to bottom, analyzed in turn.
4. Virtual inheritance (contains virtual functions)A, single virtual inheritance (contains virtual functions)
Class Base{public: base (int a = 1): Base (a) {} virtual void Fun0 () {cout << Base << Endl;} int base;}; Class base1:virtual Base{public: Base1 (int a = 2): Base1 (a) {} virtual void fun1 () {cout << base1 << End l;} int base1;};
A derived class has its own virtual function table and virtual function table pointer, rather than a virtual function table with the base class, compared to the normal single inheritance that contains virtual functions. Note the order in which virtual function table pointers and virtual base class table pointers are stored.
B, multiple virtual inheritance (contains virtual functions)
Class Base1{public: Base1 (int a = 2): Base1 (a) {} virtual void fun1 () {cout << base1 << Endl;} int base1;}; Class Base2{public: Base2 (int a = 3): Base2 (a) {} virtual void fun2 () {cout << base2 << Endl;} int base2;}; Class Derive:virtual public Base1, virtual public base2{public: Derive (int value = 4):d erive (value) {} virtual void Fun3 () {cout << derive << Endl;} int derive;};
C, Diamond Virtual inheritance (contains virtual functions)
The first form of:
Class Base{public: base (int a = 1): Base (a) {} virtual void Fun0 () {cout << Base << Endl;} int base;}; Class Base1:virtual public base{public: Base1 (int a = 2): Base1 (a) {} virtual void fun1 () {cout << base1 <& Lt Endl;} int base1;}; Class Base2:virtual public base{public: Base2 (int a = 3): Base2 (a) {} virtual void fun2 () {cout << base2 <& Lt Endl;} int base2;}; Class Derive: Public Base1, public base2{public: Derive (int value = 4):d erive (value) {} virtual void fun3 () {cout << derive << Endl;} int derive;};
The second form of:
Class Base{public: base (int a = 1): Base (a) {} virtual void Fun0 () {cout << Base << Endl;} int base;}; Class Base1:virtual public base{public: Base1 (int a = 2): Base1 (a) {} virtual void fun1 () {cout << base1 <& Lt Endl;} int base1;}; Class Base2:virtual public base{public: Base2 (int a = 3): Base2 (a) {} virtual void fun2 () {cout << base2 <& Lt Endl;} int base2;}; Class Derive:virtual public base1,virtual public base2{public: Derive (int value = 4):d erive (value) {} virtual V OID Fun3 () {cout << derive << Endl;} int derive;};
Self-repairing the memory structure of C + + class object ...
Note: In the virtual function above, if the derived class overrides the virtual function of the base class, the virtual function in the corresponding virtual function table should be modified to be a re-virtual function, i.e. Base::fun ()->derive::fun ().
C + + inheritance Rollup (single inheritance, multiple inheritance, virtual inheritance, Diamond inheritance)