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
14
votes
7 answers

Where is the "virtual" keyword necessary in a complex multiple inheritance hierarchy?

I understand the basics of C++ virtual inheritance. However, I'm confused about where exactly I need to use the virtual keyword with a complex class hierarchy. For example, suppose I have the following classes: A / \ …
jchl
  • 6,332
  • 4
  • 27
  • 51
14
votes
2 answers

C++ multiple inheritance preventing diamond

Is there a way to define a class Foo in C++ so that I can inherit from it I can't "diamond inherit" from it I.e. class Cat: public Foo{} // okay class Dog: public Foo{} // okay class Weird: public Cat, public Dog {} // I want this to throw a…
anon
  • 41,035
  • 53
  • 197
  • 293
13
votes
1 answer

Dominance in virtual inheritance

What are the C++98/C++03 standards' and the C++0x future standard's exact rules for dominance in virtual inheritance? I'm not asking for just the specific paragraphs, although I'm asking also for that (somewhere in section 10, I'd guess). I'm asking…
Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
13
votes
2 answers

What is a "virtual thunk" to a virtual function that inherits from a virtual base class?

Something went wrong when I try to access the memory layout of a derived class object which inherits from a virtual base class. Programming environment: GNU/Linux 3.19.0-32-generic, x86_64 Compiler: gcc 4.8.4 //virtual base class class Base { public…
llinvokerl
  • 1,029
  • 10
  • 25
13
votes
7 answers

final class in c++

class Temp { private: ~Temp() {} friend class Final; }; class Final : virtual public Temp { public: void fun() { cout<<"In base"; } }; class Derived : public Final { }; void main() { Derived obj; …
Learner
  • 597
  • 1
  • 5
  • 17
13
votes
2 answers

Inherit from multiple partial implementations of an abstract base class?

Is it possible to have a number of partial implementations of an abstract interface, and then collect these partial implementations into a single concrete class by using multiple inheritence? I have the following example code: #include…
Steve Lorimer
  • 27,059
  • 17
  • 118
  • 213
13
votes
3 answers

C++ constructors: why is this virtual function call not safe?

This is from the C++11 standard sec 12.7.4. This is rather confusing. What does the last sentence in the text mean exactly? Why is the last method call in B::B undefined? Shoudn't it just call a.A::f? 4 Member functions, including virtual…
ThomasMcLeod
  • 7,603
  • 4
  • 42
  • 80
12
votes
2 answers

Multiple (diamond) inheritance compiles without "virtual", but doesn't with

Given the following code (without virtual inheritance) : class A { public: virtual void f() = 0; }; class B : public A { public: virtual void f() {} }; class C : public A { public: virtual void f() {} }; class D : public B, public…
Ron_s
  • 1,429
  • 1
  • 14
  • 24
12
votes
7 answers

Static Virtual functions in c++

I have a base class and a derived one and I want to change base functions while keeping them static as they should be passed to other functions as static. How can I do that?
12
votes
4 answers

Calling a virtual base class's overloaded constructor

Is there a (practical) way to by-pass the normal (virtual) constructor calling order? Example: class A { const int i; public: A() : i(0) { cout << "calling A()" << endl; } A(int p) : i(p) { cout << "calling A(int)"…
dyp
  • 38,334
  • 13
  • 112
  • 177
12
votes
1 answer

Eliminate duplicate entries from C++11 variadic template arguments

I'm using variadic templates with multiple virtual inheritance in C++ to aggregate types into a single structure definition. Here is a sample set of structures: struct meas { int i; }; struct meas2 : public virtual meas { int j; }; struct meas3 :…
11
votes
5 answers

Is Virtual Inheritance necessary for Exceptions?

I understand the need for virtual inheritance when using multiple inheritance -- it solves the Dreaded Diamond Problem. But what if I'm not using multiple inheritance? Is there a need for virtual inheritance at all? I seem to recall hearing that it…
Tim
  • 8,912
  • 3
  • 39
  • 57
11
votes
3 answers

Why does virtual inheritance need a vtable even if no virtual functions are involved?

I read this question: C++ Virtual class inheritance object size issue, and was wondering why virtual inheritance results in an additional vtable pointer in the class. I found an article here: https://en.wikipedia.org/wiki/Virtual_inheritance which…
Klaus
  • 24,205
  • 7
  • 58
  • 113
11
votes
5 answers

Why does virtual inheritance need to be specified in the middle of a diamond hierarchy?

I have diamond hierarchy of classes: A / \ B C \ / D To avoid two copies of A in D, we need to use virtual inheritance at B and C. class A { }; class B: virtual public A {}; class C: virtual public A {…
bjskishore123
  • 6,144
  • 9
  • 44
  • 66
11
votes
4 answers

Method resolution order in C++

Consider the following class hierarchy: base class Object with a virtual method foo() an arbitrary hierarchy with multiple inheritance (virtual and non-virtual); each class is a subtype of Object; some of them override foo(), some don't a class X…
Kos
  • 70,399
  • 25
  • 169
  • 233
1 2
3
27 28