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
470
votes
11 answers

In C++, what is a virtual base class?

I want to know what a "virtual base class" is and what it means. Let me show an example: class Foo { public: void DoSomething() { /* ... */ } }; class Bar : public virtual Foo { public: void DoSpecific() { /* ... */ } };
popopome
  • 12,250
  • 15
  • 44
  • 36
120
votes
5 answers

How does virtual inheritance solve the "diamond" (multiple inheritance) ambiguity?

class A { public: void eat(){ cout<<"A";} }; class B: virtual public A { public: void eat(){ cout<<"B";} }; class C: virtual public A { public: void eat(){ cout<<"C";} }; class D: public B,C { public: void eat(){…
Moeb
  • 10,527
  • 31
  • 84
  • 110
87
votes
1 answer

Why is Default constructor called in virtual inheritance?

I don't understand why in the following code, when I instanciate an object of type daughter, the default grandmother() constructor is called ? I thought that either the grandmother(int) constructor should be called (to follow the specification of my…
Simon Desfarges
  • 972
  • 1
  • 7
  • 9
73
votes
7 answers

C++ cannot convert from base A to derived type B via virtual base A

I have four classes: class A {}; class B : virtual public A {}; class C : virtual public A {}; class D: public B, public C {}; Attempting a static cast from A* to B* I get the below error: cannot convert from base A to derived type B via virtual…
53
votes
6 answers

Why can't static_cast be used to down-cast when virtual inheritance is involved?

Consider the following code: struct Base {}; struct Derived : public virtual Base {}; void f() { Base* b = new Derived; Derived* d = static_cast(b); } This is prohibited by the standard ([n3290: 5.2.9/2]) so the code does not…
Eran
  • 21,632
  • 6
  • 56
  • 89
44
votes
1 answer

C++ Inheritance via dominance warning

I'm trying to implement a rather large object that implements many interfaces. Some of these interfaces are pure virtual. I may have a problem in diamond inheritance. Visual Studio is reporting a warning of C4250 ('class1' : inherits…
Cem Kalyoncu
  • 14,120
  • 4
  • 40
  • 62
39
votes
2 answers

What is the VTT for a class?

Recently ran across a C++ linker error that was new to me. libfoo.so: undefined reference to `VTT for Foo' libfoo.so: undefined reference to `vtable for Foo' I recognized the error and fixed my problem, but I still have a nagging question: what…
leedm777
  • 23,444
  • 10
  • 58
  • 87
32
votes
9 answers

Is there any penalty/cost of virtual inheritance in C++, when calling non-virtual base method?

Does using virtual inheritance in C++ have a runtime penalty in compiled code, when we call a regular function member from its base class? Sample code: class A { public: void foo(void) {} }; class B : virtual public A {}; class C :…
Goofy
  • 5,187
  • 5
  • 40
  • 56
31
votes
2 answers

In C++, should I almost always use virtual inheritance?

I see from this entry that virtual inheritance adds sizeof(pointer) to an object's memory footprint. Other than that, are there any drawbacks to me just using virtual inheritance by default, and conventional inheritance only when needed? It seems…
SuperElectric
  • 17,548
  • 10
  • 52
  • 69
29
votes
4 answers

Performance impact of virtual inheritance

I am considering using virtual inheritance in a real-time application. Does using virtual inheritance have a performance impact similar to that of calling a virtual function? The objects in question would only be created at start up but I'm…
Graeme
  • 4,514
  • 5
  • 43
  • 71
29
votes
3 answers

Mixing virtual and non-virtual inheritance of a base class

This is the code: struct Biology { Biology() { cout << "Biology CTOR" << endl; } }; struct Human : Biology { Human() { cout << "Human CTOR" << endl; } }; struct Animal : virtual Biology { Animal() { cout << "Animal CTOR" <<…
28
votes
7 answers

When virtual inheritance IS a good design?

EDIT3: Please be sure to clearly understand what I am asking before answering (there are EDIT2 and lots of comments around). There are (or were) many answers which clearly show misunderstanding of the question (I know that's also my fault, sorry for…
Roman L
  • 3,006
  • 25
  • 37
28
votes
2 answers

Equivalent of Java interfaces in C++?

Possible Duplicate: How do you declare an interface in C++? Interface as in java in c++? I am a Java programmer learning C++, and I was wondering if there is something like Java interfaces in C++, i.e. classes that another class can…
27
votes
5 answers

How C++ virtual inheritance is implemented in compilers?

How the compilers implement the virtual inheritance? In the following code: class A { public: A(int) {} }; class B : public virtual A { public: B() : A(1) {} }; class C : public B { public: C() : A(3), B() {} }; Does a compiler…
MKo
  • 4,768
  • 8
  • 32
  • 35
24
votes
2 answers

Virtual Inheritance: Error: no unique final overrider

I know virtual inheritance is covered here before and before asking this question, I went through the detail of the virtual inheritance and went through the details of a similar problem like the…
1
2 3
27 28