Inheritance, virtual inheritance mechanism

Source: Internet
Author: User

Scopes in the inheritance system:

1. In the inheritance system, both the base class and the derived class have separate scopes. 2. A member of the same name and a subclass member in the child class and parent class will mask direct access to the member by the parent class. (In a subclass member function, you can use the base class: base class member access)-hide-redefine 3. Note In practice it is best not to define a member with the same name in the inheritance system. Precautions:

(1) when the base class constructor takes no arguments, the derived class does not necessarily need to define the number of constructed polygons, and the system automatically calls the parameterless constructor of the base class; However, when the constructor of the base class is afraid to have only one parameter, all of its derived classes must define the constructor,
Even the defined function body of a derived class constructor may be empty, it only acts as a parameter, "third (int x, int y)", the derived class third does not use parameters x and y, x and y are simply passed to the base class constructor to be called second

(2) the Destructors class uses a default constructor or a constructor with no arguments, and the constructor can be defined in a derived class with a ": base class constructor name (parameter table)", which does not define a constructor if the derived class does not require a constructor

(3) if the base class of a derived class is also a derived class, each derived class only has to be responsible for the initialization of its direct base class data members, and so on.

There are virtual functions that simply add vfptr; If you add a virtual function to an inherited class, add a function pointer to vtable virtual inheritance adds Vbptr, note: The virtual base class element is at the end (this is different from the first base class) https://www.cnblogs.com/Azhu/p/4443099.html

This blog writes a clearer inheritance mechanism: 68931347

Inherit memory distribution

/** Normal Inheritance (no virtual base class used)*/ //base Class Aclassa{ Public:    intDataa;}; classB: Publica{ Public:    intDatab;}; classC: Publica{ Public:    intDatac;}; classD: PublicB Publicc{ Public:    intDatad;};

We can see the memory layout of Class D as follows:

From the memory layout of Class D, you can see that a derives the members of B and C,b and C that contain a respectively. D is then derived from B and C, at which point D contains the members of B and C. In this way, a total of 2 a members are present in D. Note the numbers on the left, which indicate the start address of each member in D in D, and five member variables in D (B::d ataa, Datab, C::d ataa, Datac, Datad) each occupy 4 bytes, sizeof (D) = 20.

To compare this later, let's look at the memory layout of B and C:

Virtual Inheritance Memory distribution

/** Virtual Inheritance (virtual base class)*/#include<iostream>//base Class Aclassa{ Public:    intDataa;}; classD |Virtual  Publica{ Public:    intDatab;}; classE |Virtual  Publica{ Public:    intDatac;}; classB | PublicB Publicc{ Public:    intDatad;};

As we can see, the subclasses in the diamond-like inheritance system are quite different in the memory layout and the subclass classes in the common multi-inheritance system. For the value of Class B and c,sizeof to 12, in addition to the member variable containing Class A dataa a pointer vbptr, class D in addition to inherit B, c the respective member variables Datab, DATAA and their own member variables, there are two respectively, a pointer to B, C.

Then the memory layout of the Class D object becomes as follows:

Vbptr: Inherit from a pointer in parent class B

int Datab: A member variable that inherits from the parent class B

VBPTR: pointer inherited from parent class C

int DATAC: Member variable inherited from parent class C

int DATAD:D OWN member variable

int A: member variable inherited from parent Class A

Obviously, virtual inheritance is able to implement only one copy of a common base class in a multiple-derived subclass, the key is the vbptr pointer. What exactly does that vbptr mean? How to realize the virtual inheritance? In fact, the above class D memory layout has already given the answer:

In fact,Vbptr refers to the virtual base class table pointer (virtual base tables pointer), which points to a virtual table, which records the offset address of vbptr and this class in the virtual table; The second item is the offset between the vbptr and the common base class elements . In this example, the Vbptr in class B points to the virtual table D::[email protected]@, and the virtual table indicates that the member variable of the common base class A dataa a displacement of 20 at the beginning of Class B, so that the member variable DATAA is found. Virtual inheritance does not have to maintain the same two copies of the common base class as the common multiple inheritance, saving storage space.

To further determine whether the above idea is correct, we can write a simple program to verify:

intMain () {D* d =NewD; D->dataa =Ten; D->datab = -; D->datac = +; D->datad =10000; B* B = D;//Convert to base class Bc* C = D;//Convert to base class CA * Fromb = (b*) D; A* FROMC = (c*) D; Std::cout<<"D Address:"<< D <<Std::endl; Std::cout<<"Address B:"<< b <<Std::endl; Std::cout<<"C Address:"<< C <<Std::endl; Std::cout<<"Fromb Address:"<< Fromb <<Std::endl; Std::cout<<"FROMC Address:"<< FROMC <<Std::endl; Std::cout<<Std::endl; Std::cout<<"vbptr Address:"<< (int*) d <<Std::endl; Std::cout<<"[0] = ="<< * (int*)(*(int*) d) <<Std::endl; Std::cout<<"[1] = ="<< * (((int*)(*(int*) d) +1) << Std::endl;//Offset NumberStd::cout <<"Datab Value:"<< * ((int*) D +1) <<Std::endl; Std::cout<<"vbptr Address:"<< (int*) D +2) <<Std::endl; Std::cout<<"[0] = ="<< * (int*)(*((int*) D +2)) <<Std::endl; Std::cout<<"[1] = ="<< * ((int*)(*((int*) D +2)) +1) << Std::endl;//Offset NumberStd::cout <<"Datac Value:"<< * ((int*) D +3) <<Std::endl; Std::cout<<"Datad Value:"<< * ((int*) D +4) <<Std::endl; Std::cout<<"Dataa Value:"<< * ((int*) D +5) <<Std::endl;}

Result is

Turn from: 48028491

Inheritance, virtual inheritance mechanism

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.