Inheritance and Polymorphism in C ++ (lower)-single inheritance, multi-inheritance, and diamond inheritance (including inheritance of virtual functions)

Source: Internet
Author: User

Inheritance and Polymorphism in C ++ (lower)-single inheritance, multi-inheritance, and diamond inheritance (including inheritance of virtual functions)

1. classes with virtual functions

class Base{public:                 virtual void FunTest1()                {                                cout << "Base::FunTest1()" << endl;                }                 virtual void FunTest2()                {                                cout << "Base::FunTest2()" << endl;                }                 int _data1;};int main(){                 Base b;                b._data1 = 0x01;                 return 0;}


 

Object Model:

Disassembly of the mian function:

If the class does not write the constructor itself and contains virtual functions, the compiler will combine them into a default constructor.

 

Base () constructor analysis:

1> In its constructor, we mainly complete the following: In the space pointed to by & B, we enter a virtual pointer,

2> note: the virtual table is created by the compiler after compilation and linking. In the constructor, only the address of the virtual table is filled in the first four bytes of the object.

3> In the virtual table, the virtual function address is stored in the order it declares in the class.

4> the last 00 00 00 in the virtual table indicates that the virtual table ends (the compiler is different)

Vptr points to the virtual table:

Object Model:

First, the virtual function pointer, and then the data member of the class

 

 

 

2. Single inheritance (no virtual function overwrites In the derived class)

Class Base {public: virtual void FunTest1 () {cout <"Base: FunTest1 ()" <
 
  

 

Fill in the virtual pointer.

In the constructor of Derive:

The final object model of the derived class is: if the derived class does not overwrite the virtual functions in the base class, the virtual table in the derived class is the virtual function address of the base class, then add your own virtual function address. The order of virtual function addresses is the Order declared in the class.

Virtual table of Base in a derived class

 

 

 

3. Single inheritance (overwrite with virtual functions)

Class Base {public: virtual void FunTest1 () {cout <"Base: FunTest1 ()" <endl;} virtual void FunTest2 () {cout <"Base :: funTest2 () "<endl;} int _ data1;}; class Derive: public Base {public: virtual void FunTest1 () // override FunTest1 {cout <"Derive: FunTest1 ()" <endl;} virtual void FunTest3 () {cout <"Derive: FunTest3 () "<endl;} virtual void FunTest4 () {cout <" Derive: FunTest4 () "<e Ndl;} int _ data2;}; typedef void (* VtbFun) (); void PrintVtable () {cout <"Derive class virtual function table:" <endl; derive d1; d1. _ data1 = 0x01; d1. _ data2 = 0x02; int * pVTable = (int *) * (int *) & d1; vtbFun FunTest = (VtbFun) * pVTable; while (NULL! = FunTest) {FunTest (); cout <(int *) FunTest <endl; pVTable + = 1; FunTest = (VtbFun) * pVTable ;} cout <"virtual table end:" <endl ;}int main () {PrintVtable (); return 0 ;}


 

Create a rule for the object model and virtual table of the derived class: first move the virtual table of the base class. If the derived class overwrites a function of the base class, the address of the virtual function overwritten by the derived class will replace the address of the virtual function in the base class at the corresponding location in the virtual table, after replacement, add the virtual function address of the derived class to the virtual table in the declared order.

4. Multi-inheritance (no overwrite of virtual functions)

Class Base {public: virtual void FunTest1 () {cout <"Base: FunTest1 ()" <endl;} virtual void FunTest2 () {cout <"Base :: funTest2 () "<endl;} int _ data1;}; class Base1 {public: virtual void FunTest3 () {cout <" Base1: FunTest3 () "<endl;} virtual void FunTest4 () {cout <" Base1: FunTest4 () "<endl;} int _ data2;}; class Derive: public Base, public Base1 {public: virtual void FunTest5 () {cout <"Derive: FunTest5 ()" <endl;} int _ data3;}; typedef void (* VtbFun) (); void PrintVtable () {cout <"Derive class virtual function table:" <endl; Derive d1; d1. _ data1 = 0x01; d1. _ data2 = 0x02; int * pVTable = (int *) * (int *) & d1; VtbFun FunTest = (VtbFun) * pVTable; while (NULL! = FunTest) {FunTest (); cout <(int *) FunTest <endl; pVTable + = 1; FunTest = (VtbFun) * pVTable ;} cout <"virtual table ends. "<endl ;}int main () {Derive d; d. _ data1 = 0x01; d. _ data2 = 0x02; d. _ data3 = 0x03; PrintVtable (); return 0 ;}

 

5. Multi-inheritance (overwrite with virtual functions)

ClassBase {public: virtualvoidFunTest1 () {cout <"Base: FunTest1 ()" <
   
    

 

In this case, the object model of the derived class and the structure of the virtual table are as follows:

Create a virtual table from a derived class: first create the same part as Base1 and rewrite FunTest3 in Derive. Therefore, replace the original Base1: FunTest3 in the virtual table with Derive: FunTest3, to improve the access speed, add your own virtual functions to the end of the first virtual table, and create the Base virtual table in the same process as Base1.

6. Virtual inheritance

classBase{public:virtualvoidFunTest1(){cout<<"Base::FunTest1()"<
     
      

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.