Virtual inheritance in C++
What is it and why is it used in C++?
7
Author: Nicolas Chabanovsky, 2010-10-21
1 answers
Virtual inheritance is necessary in this situation.
class A { int a; };
class B: public A {};
class C: public A {};
class D: public B, public C {};
In the D
class, in this case, there will be two fields named a
and they will both belong to the A
class.
The problem is to determine which variable is being accessed. To avoid this situation, use virtual inheritance. The correct type of ad in this example would be
class A { int a; };
class B: public virtual A {};
class C: public virtual A {};
class D: public B, public C {};
12
Author: Nicolas Chabanovsky, 2016-07-31 14:56:00