C + + Virtual base class derivation and inheritance

Source: Internet
Author: User

In learning design patterns I have a question, association and inheritance in addition to the use of differences, as if there is no difference in memory, inheritance is also the parent class as the element of the subclass (memory), the association is also the case. And the association seems to account for more memory. This is the problem in the design mode "dependency reversal principle".

Inheritance is divided into public inheritance, protect inheritance, private inheritance

Public: The public,protected member in the parent class is in the derived class and the property is not changed.

Protected: The public,protected member in the parent class is in the derived class and becomes a protected member.

Private: The public,protected member in the parent class is in the derived class and becomes a private member.

Special note: Private members in the parent class are not inherited into subclasses (this is also a difference between association and inheritance).

Special Note: There is also an inheritance called virtual inheritance

A detailed review of virtual inheritance:

The form of virtual inheritance:

Class Derived classes: Virtual base class 1,virtual base class 2, Virtual base class 3 ...

{
};

Constructor procedure:

The constructor of the virtual base class is executed first, and the constructors of many virtual base classes are constructed in the order of inheritance;

Executes the constructor of the base class, and the constructors of multiple base classes are constructed in the order in which they are inherited;

Executes the constructor of a member object, and the constructors of multiple member objects are constructed in the order in which they are declared;

Executes the derived class's own constructor;

The destruction is performed in the reverse order of the construction;

The essence of virtual inheritance is that in multiple inheritance, if there is a base class in a derived class inheriting to the same base class, then these base classes must contain copies of the same base class (each of them), so if you want to use it in a derived class, you must add the scope resolution to uniquely identify it. This problem can be solved by using the virtual base class when the other base classes inherit. We can set the base class of the common inheritance to the virtual base class, when the data member of the same name inherited from different paths has only one copy in memory, and the same name function has only one mapping.

1#include <iostream>2 3 using namespacestd;4 5 classBase6 {7 Private:8     intVal;9  Public:TenBase () {cout <<"base non-parametric constructors"<<Endl;}; OneBase (intVal) { This->val = val; cout <<"Base has parameter constructors"<<Endl;} A     voidPrint () {cout <<"val ="<<val <<Endl;} - }; -  the classSUB1:Virtual  PublicBase - { -  Public : -SUB1 (intval): Base (val) {cout <<"Sub1 with parameter constructors"<<Endl;}; + }; -  + classSUB2:Virtual  PublicBase A { at  Public: -SUB2 (intval): Base (val) {cout <<"Sub2 with parameter constructors"<<Endl;} - }; -  - classChild: PublicSUB1, PublicSUB2 - { in  Public : -Child (intval): Sub1 (Val), Sub2 (val) {cout <<"Child with parameter constructor"<<Endl;} to }; +  - intMain () the { *Child Child (3); $ child.print ();Panax Notoginseng}

Output Result:

Base non-parametric constructors
Sub1 with parameter constructors
Sub2 with parameter constructors
Child with parameter constructor
val =-858993460
Press any key to continue ...

Visible Sub1, and Sub2 constructors do not perform base constructors, the virtual inheritance constructor is such that the parameterless constructor of the virtual base class is executed first, then the constructor of the SUB1 is executed, and in the execution of the constructor of the Sub2, the constructor of the child is executed, and the constructors for base are no longer executed when the constructors for SUB1 and SUB2 are executed.

If you do not use virtual inheritance for access:

1#include <iostream>2 3 using namespacestd;4 5 classBase6 {7 Private:8     intVal;9  Public:TenBase () {cout <<"base non-parametric constructors"<<Endl;}; OneBase (intVal) { This->val = val; cout <<"Base has parameter constructors"<<Endl;} A     voidPrint () {cout <<"val ="<<val <<Endl;} - }; -  the classSUB1: PublicBase - { -  Public : -SUB1 (intval): Base (val) {cout <<"Sub1 with parameter constructors"<<Endl;}; + }; -  + classSUB2: PublicBase A { at  Public: -SUB2 (intval): Base (val) {cout <<"Sub2 with parameter constructors"<<Endl;} - }; -  - classChild: PublicSUB1, PublicSUB2 - { in  Public : -Child (intval): Sub1 (Val), Sub2 (val) {cout <<"Child with parameter constructor"<<Endl;} to }; +  - intMain () the { *Child Child (3); $ Panax Notoginseng     //Child.print ()//Illegal Access -  the Child . SUB1::p rint (); + Child . SUB2::p rint (); A}

Results:

Base has parameter constructors
Sub1 with parameter constructors
Base has parameter constructors
Sub2 with parameter constructors
Child with parameter constructor
val = 3
val = 3
Press any key to continue ...

C + + Virtual base class derivation and inheritance

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.