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

Implicitly calling a constructor of an inaccessible virtual base class

Consider the code below. Both g++ and clang++ complain (correctly) that the constructor A(int) is private in class D. Note that, as A is a virtual base class of D, A has to be initialized in the mem-initializer of class D, the most derived class,…
Belloc
  • 6,318
  • 3
  • 22
  • 52
11
votes
2 answers

Is virtual inheritance still necessary when base classes contain no data members?

Would the following code still be negatively affected by the lack of virtual inheritance? If so, would the negative effects be the same as (or as bad as) the negative effects of multiple inheritance without virtual inheritance if class A did contain…
Giffyguy
  • 20,378
  • 34
  • 97
  • 168
11
votes
3 answers

multiple inheritance without virtual inheritance

I am trying to understand multiple inheritance, here is my code: struct A { A() {} static int n; static int increment() { return ++n; } }; int A::n = 0; struct B : public A {}; struct C : public A {}; struct D : public B, C {}; int main() { …
Deqing
  • 14,098
  • 15
  • 84
  • 131
11
votes
2 answers

Virtual Inheritance and dreaded diamond

I am having a hard time with a dreaded diamond problem. For a reminder, here is the classical class hierarchy of this problem: B / \ C1 C2 \ / D To solve it, the standard solution is to make C1 and C2 use virtual inheritance to…
10
votes
1 answer

C++, ambiguous inheritance error in vs 2010

I have some troubles with the application of polymorphism in this example. This question is similar to my last question C++, virtual inheritance, strange abstract class + clone problem There are 3 abstract classes: class A { public: virtual A *…
Johnas
  • 1,263
  • 3
  • 12
  • 23
10
votes
2 answers

ladder-like C++ virtual inheritance

If I have a class inheritance relation like the following a / \ b c \ | | d \/ \ e f \ / g Is the…
Joe C
  • 2,757
  • 2
  • 26
  • 46
10
votes
1 answer

C++ code segfaults when compiled -O with Apple's LLVM compiler, but not with g++ -7.2.0

Update: I've created an even more M, but still CVE that reproduces the crash. Summary: removed all use of the Bool* bools_ field in Base class (but it still must be defined or the crash does not happen). Also removed Base::Initialize() and the…
fizzixgal
  • 133
  • 6
10
votes
3 answers

Virtual tables and virtual pointers for multiple virtual inheritance and type casting

I am little confused about vptr and representation of objects in the memory, and hope you can help me understand the matter better. Consider B inherits from A and both define virtual functions f(). From what I learned the representation of an…
Artium
  • 5,147
  • 8
  • 39
  • 60
10
votes
3 answers

Pure Virtual Class and Collections (vector?)

I'm working on a graphics application that is using virtual classes fairly extensively. It has: A picture class, which is essentially a collection of shapes. A shapes class, which is purely virtual and has a few classes that inherit from…
the_gastropod
  • 357
  • 1
  • 2
  • 9
10
votes
1 answer

How can static_cast be used with virtual inheritance?

So it's impossible to downcast using static_cast with virtual inheritance, but how is it possible to do the following upcast: class Base {...}; class Derived : public virtual Base {...}; ... Derived *d = new Derived(); Base *b =…
user2628520
10
votes
2 answers

Virtual Inheritance, one class enough?

I understand the concept of virtual inheritance, but I couldn't find the answer to this anywhere. Say you have class D which inherits class B and C. Both B and C inherit class A. So you could make B and C virtually inherit A to avoid two instances…
Invalid
  • 1,870
  • 1
  • 16
  • 22
10
votes
4 answers

Object layout in case of virtual functions and multiple inheritance

I was recently asked in an interview about object layout with virtual functions and multiple inheritance involved. I explained it in context of how it is implemented without multiple inheritance involved (i.e. how the compiler generated the virtual…
Ankur
  • 11,239
  • 22
  • 63
  • 66
9
votes
3 answers

How to detect and assert virtual inheritance for a specific class?

I have a C++ class that implements reference-counting and I want all users of this class to inherit from this class only virtually so that no object ends up with more than one reference counter. I'd like some way to assert this requirment either…
sharptooth
  • 167,383
  • 100
  • 513
  • 979
9
votes
4 answers

Subclass address equal to virtual base class address?

We all know that when using simple single inheritance, the address of a derived class is the same as the address of the base class. Multiple inheritance makes that untrue. Does virtual inheritance also make that untrue? In other words, is the…
user1610015
  • 6,561
  • 2
  • 15
  • 18
9
votes
2 answers

Should you write "public virtual" or "virtual public" in virtual inheritance?

Based on http://en.wikipedia.org/wiki/Virtual_inheritance class Animal { ... }; // Two classes virtually inheriting Animal: class Mammal : public virtual Animal { ... }; I also saw books use the following syntax, class Mammal : virtual public…
q0987
  • 34,938
  • 69
  • 242
  • 387