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