C + + virtual inheritance

Source: Internet
Author: User

C + + virtual inheritance concept:
Virtual Inheritance virtual inheritance solves the problem of data inconsistency caused by different data copies in memory from different paths, and sets the common base class to virtual base class. Data members that inherit from different paths have only one copy in memory, and the same name function has only one mapping.

Grammar:
Class c:virtual public a,virtual public B,..., virtual public n
{

}
Execution order
1. Execute the constructor of the virtual base class, and multiple inheritance is constructed in the order of inheritance
2. Execute the constructor of the base class, and multiple inheritance is constructed in the order of inheritance
3. Execute the constructor of the member object, and multiple member objects are constructed in the order of declaration
4. Execute the derived class's own constructor

Note: The order in which the destructors are performed is reversed from the above sequence
When executing a virtual base class constructor, if it is multiple inheritance

The difference between virtual inheritance and ordinary inheritance
Normal inheritance: C inherits a indicates that C "is a" i.e. apple is the fruit
Virtual inheritance: C Virtual Inheritance A indicates that C "has a" that is, Apple is a fruit, but Apple can also be a mobile phone, C has a call a vptr

Common inheritance:

//Virtual_exercise.cpp: Defines the entry point of the console application. //#include "stdafx.h"classa{ Public: A ();voidPrint () {printf("This A class aprintf\n"); } ~a ();Private:}; A::a () {printf("This is A method\n");} A::~a () {printf("This is A destructor\n");}classB: Publica{ Public: B (); ~b ();voidA ();voidPrint () {printf("This B class aprintf\n"); }Private:};voidB::a () {printf("This is B ' s A method\n");} B::b () {printf("This is B method\n");} B::~b () {printf("This is B destructor\n");}classC: PublicA Publicb{ Public: C (); ~c ();Private:}; C::c () {printf("This is C method\n");} C::~c () {printf("This is C destructor\n");}int_tmain (intARGC, _tchar* argv[]) {a A;    A.print ();    b b;    B.A ();    B.print (); c C;//c.print (); This method of calling will cause an errorC.B::p rint ();    C.A::p rint (); GetChar ();return 0;}

Virtual Inheritance:

//Virtual_exercise.cpp: Defines the entry point of the console application. //#include "stdafx.h"classa{ Public: A ();voidPrint () {printf("This A class aprintf\n"); } ~a ();Private:}; A::a () {printf("This is A method\n");} A::~a () {printf("This is A destructor\n");}classB:Virtual  Publica{ Public: B (); ~b ();voidA ();voidPrint () {printf("This B class aprintf\n"); }Private:};voidB::a () {printf("This is B ' s A method\n");} B::b () {printf("This is B method\n");} B::~b () {printf("This is B destructor\n");}classC:Virtual  PublicAVirtual  Publicb{ Public: C (); ~c ();Private:}; C::c () {printf("This is C method\n");} C::~c () {printf("This is C destructor\n");}int_tmain (intARGC, _tchar* argv[]) {a A;    A.print ();    b b;    B.A ();    B.print ();    c C; C.print ();//So you can call the    //c.b::p rint ();    //c.a::p rint ();GetChar ();return 0;}

Look, the invocation mode and the output have changed ^_^, the attentive audience is not found that the virtual inheritance method is less than the normal inheritance of the method output one time

Well, I hope it will be of some help to you.

C + + virtual 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.