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
9
votes
1 answer

Misaligned address using virtual inheritance

The following apparently valid code produces a misaligned address runtime error using the UndefinedBehaviorSanitizer sanitiser. #include #include struct A{ std::function data; // seems to occur only if data is a…
9
votes
1 answer

C++ Virtual Inheritance Memory Layout

Virtual Inheritance Memory Layouts I am trying to fully understand what is happening under the hood in the memory with virtual inheritance and vTables/vPtrs and what not. I have two examples of code I have written and I understand exactly why they…
9
votes
3 answers

Fixing C++ Multiple Inheritance Ambiguous Call

I have three classes structured like this: #include using namespace std; class Keyword { public: virtual float GetValue() = 0; }; class CharacterKeyword : public Keyword { public: virtual float GetValue(){return…
larrylampco
  • 597
  • 1
  • 9
  • 33
8
votes
3 answers

Why do the constructor of the derived classes want to initialize the virtual base class in C++?

My understanding, for instance reading this, is that the constructor of a derived class does not call its virtual base class' constructor. Here is a simple example I made: class A { protected: A(int foo) {} }; class B: public virtual A…
scozy
  • 2,511
  • 17
  • 34
8
votes
1 answer

C++ copy constructor in inheritance

#include using namespace std; class A { public: A(){ cout <<"1";} A(const A &obj){cout <<"2";} }; class B: virtual A { public: B(){cout <<"3";} B(const B & obj):A(obj){cout<<"4";} }; class C: virtual A { public: …
8
votes
2 answers

C++: inheriting overloaded non-virtual method and virtual method both with the same name causes problem

I am trying to inherit two equally named methods with different parameter lists to a derived class. One of them is virtual and overridden in the derived class, the other one is non-virtual. Doing so, i get a compile error while trying to access the…
Emme
  • 83
  • 1
  • 3
8
votes
2 answers

Crash in constructor with using virtual inheritance and delegating constructors

struct D { virtual void m() const = 0; }; struct D1 : public virtual D { }; struct D2 : public virtual D { }; struct B : public D2 { B() { } B(int val) : B() { } void m() const { } }; struct A : public B, public D1 { A() :…
8
votes
2 answers

C++ virtual inheritance initializer list

in the following code: class A { public: int x; A(int x):x(x){} }; class B: public virtual A { public: B(int x):A(x){} }; class C: public virtual A { public: C(int x):A(x){} }; class D: public B, public C { public: D(int…
prongs
  • 9,422
  • 21
  • 67
  • 105
8
votes
3 answers

Force deriving from a class virtually

We have a special framework for interfaces in our project, and part of the requirements is that classes which represent an interface may only be used as virtual base classes, not as non-virtual ones. Is there a way to enforce this in code? That is,…
8
votes
3 answers

Virtual inheritance vs. non-default constructors

This code is rejected by (at least) MSVC, ICC, and GCC: class A { public: A( int ) { } }; class B: virtual public A { public: //B(): A( -1 ) { } // uncomment to make it compilable virtual void do_something() = 0; }; class C: public B…
vpozdyayev
  • 1,014
  • 5
  • 14
7
votes
1 answer

Virtual inheritance constructor selection

Why does this print 20000? Code explicitly calls specific base constructors all the way up the inheritance train, yet ignores specified constructor and uses the default constructor instead. #include struct Car { Car() : price(20000)…
code
  • 1,056
  • 10
  • 22
7
votes
4 answers

How to call copy constructor of all base classes for copying most derived class object in diamond inheritance in C++?

Consider the below code: #include using namespace std; class A { public: A() {cout << "1";} A(const A &obj) {cout << "2";} }; class B: virtual A { public: B() {cout << "3";} B(const B & obj) {cout<< "4";} }; class C:…
7
votes
3 answers

virtual inheritance constructor order

I am trying to understand better the concept of virtual inheritance, and what are its perils. I read in another post (Why is Default constructor called in virtual inheritance?) that it (= virtual inheritance) changes the order of constructor call…
TCS
  • 5,790
  • 5
  • 54
  • 86
7
votes
1 answer

Avoiding explicit constructor calls of virtual base classes

Basic question: is it possible to avoid having to explicitly call the (non-default) constructor of every virtual base class? Background: I'm working on some type-safe C++ wrapper classes around Windows COM objects. My current approach is to have a…
Drew McGowen
  • 11,471
  • 1
  • 31
  • 57
7
votes
1 answer

Is this use pattern of virtual inheritance for "method injection" a known paradigm?

Yesterday, I came across this question: forcing unqualified names to be dependent values Originally, it seemed like a very specific question related to broken VC++ behaviour, but while trying to solve it, I stumbled upon a use pattern of virtual…
Oguk
  • 1,127
  • 7
  • 13