Questions tagged [virtual-inheritance]

Virtual Inheritance is used to solve the Dreaded Diamond Problem associated with multiple inheritance in C++.

Virtual Inheritance is a kind of inheritance in which the part of the object that belongs to the virtual base class becomes a common direct base for the derived class and any other class that derives from it. In other words, if class A is virtually derived from class V, and class B is derived (directly or indirectly) from A, then V becomes a direct base class of class B and any other class derived from A.

Virtual Inheritance is used to solve the problem of Dreaded Diamond Problem in C++.

When Do we use Virtual Inheritance?

Generally, virtual base classes are most suitable when the classes that derive from the virtual base, and especially the virtual base itself, are pure abstract classes. This means the classes above the "join class" have very little if any data.

420 questions
24
votes
3 answers

Order of constructor call in virtual inheritance

class A { int i; public: A() {cout<<"in A's def const\n";}; A(int k) {cout<<"In A const\n"; i = k; } }; class B : virtual public A { public: B(){cout<<"in B's def const\n";}; B(int i) : A(i)…
Kunal
  • 511
  • 2
  • 6
  • 12
23
votes
2 answers

Downcast in a diamond hierarchy

Why static_cast cannot downcast from a virtual base ? struct A {}; struct B : public virtual A {}; struct C : public virtual A {}; struct D : public B, public C {}; int main() { D d; A& a = d; D* p = static_cast(&a); //error } g++ 4.5…
log0
  • 10,489
  • 4
  • 28
  • 62
23
votes
3 answers

c++ virtual inheritance

Problem: class Base { public: Base(Base* pParent); /* implements basic stuff */ }; class A : virtual public Base { public: A(A* pParent) : Base(pParent) {} /* ... */ }; class B : virtual public Base { public: B(B* pParent) :…
Robert
  • 2,330
  • 29
  • 47
22
votes
3 answers

Why must virtual base classes be constructed by the most derived class?

The following code won't compile: class A { public: A(int) {} }; class B: virtual public A { public: B(): A(0) {} }; // most derived class class C: public B { public: C() {} // wrong!!! }; If I call A's constructor in C's constructor…
JFMR
  • 23,265
  • 4
  • 52
  • 76
21
votes
2 answers

C++ abstract base class constructors/destructors - general correctness

I would like to have a C++ Interface that must be overridden (if this is possible) when inherited. So far, I have the following: class ICommand{ public: // Virtual constructor. Needs to take a name as parameter //virtual ICommand(char*)…
Stuart Blackler
  • 3,732
  • 5
  • 35
  • 60
20
votes
5 answers

understanding vptr in multiple inheritance?

I am trying to make sense of the statement in book effective c++. Following is the inheritance diagram for multiple inheritance. Now the book says separate memory in each class is required for vptr. Also it makes following statement An oddity in…
Xinus
  • 29,617
  • 32
  • 119
  • 165
19
votes
3 answers

C++ private virtual inheritance problem

In the following code, it seems class C does not have access to A's constructor, which is required because of the virtual inheritance. Yet, the code still compiles and runs. Why does it work? class A {}; class B: private virtual A {}; class C:…
Oak
  • 26,231
  • 8
  • 93
  • 152
18
votes
1 answer

Memory layout of a class under multiple or virtual inheritance and the vtable(s)?

I am reading "Inside the C++ Object Model", trying to understand how multiple and virtual inheritance is achieved via the vtables.(I understand single polymorphism perfectly-well). I am having difficulties understand what exactly is done when a…
15
votes
1 answer

Why does uniform initialization in C++11 behave weirdly with virtual base classes?

Right now, I am learning the features of Inheritance in C++ and wanted to test out the recently learnt concept of Virtual Base classes. I tried the following simple code: #include using namespace std; class A { private: int…
15
votes
1 answer

Virtual Inheritance with Constructor Inheritance

I have a class hierarchy which boils down to class Module { }; struct Port { Module& owner; Port(Module& owner) : owner(owner) {} }; struct InPort : virtual Port { using Port::Port; }; struct OutPort : virtual Port { using…
danielschemmel
  • 10,885
  • 1
  • 36
  • 58
15
votes
2 answers

Why is it disallowed to convert from VirtualBase::* to Derived::*?

Yesterday, me and my colleague weren't sure why the language forbids this conversion struct A { int x; }; struct B : virtual A { }; int A::*p = &A::x; int B::*pb = p; Not even a cast helps. Why does the Standard not support converting a base…
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
15
votes
5 answers

Diamond-inheritance scenario compiles fine in G++, but produces warnings/errors in VC++/Eclipse

I have a base class 'Base', which is a pure virtual class: class Base { public: virtual void A() = 0; virtual void B() = 0; virtual ~Base() { } // Eclipse complains that a class with virtual members must have virtual destructor }; I…
Tibi
  • 4,015
  • 8
  • 40
  • 64
15
votes
2 answers

Virtual tables and memory layout in multiple virtual inheritance

Consider following hierarchy: struct A { int a; A() { f(0); } A(int i) { f(i); } virtual void f(int i) { cout << i; } }; struct B1 : virtual A { int b1; B1(int i) : A(i) { f(i); } virtual void f(int i) { cout << i+10;…
JeB
  • 11,653
  • 10
  • 58
  • 87
14
votes
4 answers

is virtual inheritance from pure abstract classes (interfaces) necessary

Why is it that in the code below the compiler complains that PureAbstractBase is an ambiguous base class of MultiplyInheritedClass? I realize I have two copies of the PureAbstractBase in MultiplyInheritedClass and that FirstConreteClass and…
bpw1621
  • 2,962
  • 2
  • 32
  • 35
14
votes
4 answers

Virtual inheritance in C++

I found this in a website while reading about virtual inheritance in c++ When multiple inheritance is used, it is sometimes necessary to use virtual inheritance. A good example for this is the standard iostream class hierarchy: //Note: this is a…
Vijay
  • 65,327
  • 90
  • 227
  • 319
1
2
3
27 28