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

Why does declaring runtime polymorphism for template instantiations result in linker error?

I have a perfectly working code: template class ThreadImplementation { ... void launch(){...} ~ThreadImplementation(){...} }; ... ThreadImplementation *t = new…
user1358
  • 623
  • 2
  • 8
  • 13
0
votes
1 answer

Some basic Inheritance problems in C++

I am learning OO in C++ programming these days in VS2010. I meet with some basic Inheritance problems in C++. Here is my code: Question 1: class bs { public: int a; virtual void name(){}; }; class A:virtual public bs { public: int a; …
zhfkt
  • 2,415
  • 3
  • 21
  • 24
0
votes
3 answers

Virtual Base Class in C++

I have a query regarding the virtual base class. In order to resolve the "dreaded diamond of death" /ambiguity problem in multiple inheritance, virtual base class is introduced. class A { public: void Foo() {} }; class B : public virtual A {}; class…
0
votes
2 answers

c++ template multiple inheritance from an interface

So I have this problem. Basicly I have a templated interface: template class Iinterface { public: virtual ~Iinterface() // A pure methode for the example that gives me the problem virtual…
Jp1987
  • 45
  • 1
  • 8
0
votes
1 answer

When using a virtual base class in a multiple inheritance scenario, is it necessary for all derived classes to reference the virtual base?

The US Air Force's JSF C++ coding standard requires that the virtual base class be declared for each derived class that accesses the virtual base. For example, in the following hierarchy: A / \ B1 B2 C1 C2 \ / D ... the rule they impose in…
Brian Vandenberg
  • 4,011
  • 2
  • 37
  • 53
0
votes
2 answers

What will be the sequence of inheritance

What will be the sequence of inheritance and what does the following code mean class A { int a; virtual void display() { cout<<"A"; } } class B { int b; virtual void displayB() { cout<<"B"; } } class C:public B,…
megha
  • 170
  • 2
  • 10
0
votes
1 answer

Multiple inheritance diamond

Here is the code: class Vehicle { public : Vehicle () { cout << " Vehicle Constructor " << endl ; } virtual ~ Vehicle () { cout << " Vehicle Destructor " << endl ; …
0
votes
0 answers

is this a good use of virtual inheritance?

I want to track the deletion of object of some selected classes, with minimal changes in the classes code itself. I considered overloading the delete operator (globally), but it would require that my overloaded delete was present in all the…
0
votes
1 answer

virtual vs non-virtual multiple inheritance in c++

I am currenlty trying to grasp the concept of multiple virtual/non-virtual inheritance in c++. if I understand correctly if a class B and C inherit virtually from class A, they share a kind of singleton object i.e. they both access same fields of…
Bober02
  • 15,034
  • 31
  • 92
  • 178
-1
votes
3 answers

Virtual inheritance and same name members

I have the the following classes: class Base { public: Base() { x = 3; } int x; virtual void foo() {}; }; class Med1 : public virtual Base { public: int x; Med1() { x = 4; } virtual void foo() {}; }; class Med2 : public…
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
-1
votes
3 answers

How to emulate override of parts of "virtual variadic functions"?

First of all, I know that variadic functions can't be virtual in c++. My question is how to emulate the next "incorrect" example. I want to have a class A with "virtual variadic function" and class B which inherits it and implements only "part of…
toxic
  • 39
  • 5
-1
votes
1 answer

C++ Multiple virtual inheritance, classes share the same subobject

I want to inherit 'x' from 'class B' and 'y' from 'class C' but both classes share the same 'A' subobject. Is there any solution? #include class A { protected: int x; int y; public: A() : x(10), y(10)…
-1
votes
1 answer

C++ primer 5th ed. Virtual inheritance and ctor-init

On C++ primer 5th Ed. Chapter 18. Multiple and virtual inheritance, I have this question: Exercise 18.30: Define a default constructor, a copy constructor, and a constructor that has an int parameter in Base. Define the same three constructors in…
-1
votes
1 answer

Store object as it's base VIRTUAL class

Why can't one store an object as it's base virtual class ? Consider the following example : It will segfault if Derived inherits virtually of Base #include #include class Base { public: virtual ~Base() = default; int…
Yanis.F
  • 612
  • 6
  • 18
-1
votes
1 answer

"X is an ambiguous base of Y": Multiple inheritance nightmare

Below is the class hierarchy I have. All Ixxxx classes are interfaces (abstract classes with no member data). All arrows represent inheritance. Colors are only here to provide better visualisation. Somewhere in the code using these classes, I have…
Silverspur
  • 891
  • 1
  • 12
  • 33
1 2 3
27
28