Virtual inheritance is rarely used in general applications, so it is often overlooked, mainly because in C
, multiple inheritance is not recommended, and once you leave multiple inheritance, virtual inheritance completely lost the existence necessary (because this only reduces efficiency and more space, it is really no place).
One example in the following is an example:
#include
Class CA
{
Int K
;
/ / In order to facilitate the specification of the memory structure,
public
:
Void F
() {Cout
<<
"CA :: F"
<< ENDL
;}};
Class CB
:
Public CA
{};
Class CC
:
Public CA
{};
Class CD
:
Public CB
,
Public CC
{};
Void
main
() {
CD D
;
di
.f
();
When compiling the above code, we will receive the following error tips:
Error C2385
:
'Cd :: f' is ambiguous
That is, the compiler cannot determine you in D.
.f
() Which one is to be called in the end is it. It may make people feel somewhat strange, named only a CA
: F, since everyone is derived from CA, then natural is called CA
:: F, why can't you be sure?
This is because the compiler needs to determine the function definition of the subclass when performing compilation, such as CA
:: F is determined, then in compiling CB, CC, you need to generate CB in the syntax tree of the compiler.
:: f, cc
:: F, etc., then, when compiling the CD, since the CB, CC has a function f, at this time, the compiler will attempt to generate two CDs.
:: F logo, obviously it is wrong. (When we don't use CD
: When f, the above identity will not be generated, so if you remove D
.f
() A sentence, the program will be compiled smoothly)
To solve this problem, there are two ways:
1, overload function f
(): At this time, because we clearly define the CD
:: F, compiler to check to CD
:: F
() If you do not need to build a CD as level as above?
:: F logo;
At this time, the element structure of the CD is as follows:
-------- | CB
(CA
) || CC
(CA
) --------
There is time
Sizeof
(CD)
) =
8
(CB, CC has an element K)
2, use virtual inheritance: virtual inheritance is also called sharing inheritance, which is actually implemented during compilation. When using virtual inheritance, the above program will become the following form:
#include
Class CA
{
Int K
;
public
:
Void F
() {Cout
<<
"CA :: F"
<< ENDL
;}};
Class CB
:
Virtual Public CA
{};
Class CC
:
Virtual Public CA
{};
Class CD
:
Public CB
,
Public CC
{};
Void
main
() {
CD D
;
di
.f
();
At this time, when the compiler determines D
.f
() When the specific meaning of the call, the following CD structure is generated: ---- | CB
|| CC
|| CA
| ------
At the same time, in CB, CC includes VBPTR to CA, respectively.
Virtual base Table Pointer, which is recorded from the offset between the elements of the CB, CC to the elements of CA. At this time, the function F identification of each subclass is not generated, unless the subclass is overloaded to the "sharing" purpose.
Therefore, there is always, at this time
Sizeof
(CD)
) =
12 (two VBPTR
Sizoef
(
int
))
;
All of this is determined during compilation, just the compiler to provide us with more things to provide us with such a new syntax function.
Note: The above discussion is limited to MS Visual C
compiler.