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
2 answers

Unable to call base constructor in template class using virtual inheritance

Related to this question; the following code tries to hide the public constructors of each specific component implementation while providing a generic create function on each component (the implementation of it always does the same: communcating…
maxdev
  • 2,491
  • 1
  • 25
  • 50
0
votes
0 answers

Virtual inheritance and move constructors

I'm observing strange behaviour with clang++ (3.5-1ubuntu1). If virtual inheritance is used, the copy constructor of the 'Base' class appears to be skipped. Please see the sample and results below. The complicated inheritance hierarchy is…
0
votes
1 answer

passing a reference to virtual class implementation as thread argument

Once again I need your help making std::thread work with templated objects. This time my issue is in one of the arguments. I try passing a reference to a (valid) instantiation of a virtual class as one of the arguments of my thread, and it just…
Amxx
  • 3,020
  • 2
  • 24
  • 45
0
votes
1 answer

Complicated Order of construction - involving virtual inheritance

I came across this question which has me confused. The code for this is : struct B1 { B1(){std::cout << "B1\n";} }; struct V1 : public B1 { V1(){std::cout << "V1\n";} }; struct D1 : virtual public V1 {D1(){std::cout << "D1\n";} }; struct…
Rajeshwar
  • 11,179
  • 26
  • 86
  • 158
0
votes
1 answer

Virtual Classes having more size

class abstract {double abs; }; class A : virtual public abstract { double var; }; class B : virtual public abstract { double var; }; class derived : public A, public B { double der; }; sizeof(abstract) = 8 sizeof(A) = 24 sizeof(B) =…
Level 31
  • 403
  • 3
  • 9
0
votes
2 answers

Assinging the Objects Which are Virtual Multiple Inherited

I have a problem with assigning the same objects with multiple inheritance which also have diamond problem. Here is the skeleton code of my project. H.h class H { protected: int a; int b; int c; public: …
COvayurt
  • 827
  • 2
  • 11
  • 36
0
votes
3 answers

C++ How to initilize abstract base class reference element?

My problem is based on typical diamond hierarchy, but is not a typical diamond problem. class Interface { public: int value; // SomeBigData &data; Interface(int _value = 0) : value(_value) {}; virtual void function1() = 0; …
0
votes
1 answer

Virtual Inheritance - Diamond Issue - What really happens

I understand and have read enough about the diamond problem which is solved by virtual inheritance. My question here is "What does placing virtual next to a base class that you would be inheriting from actually mean" class A { public: void Foo()…
Rajeshwar
  • 11,179
  • 26
  • 86
  • 158
0
votes
1 answer

Virtual inheritance causes multiple information about one member when debbuing

I use the virtual inheritance. I basically have the "tree" of inheritance for some abstract module: class Positionable{ public: virtual std::string getName() = 0; }; class Positionable3D : virtual public Positionable{ public: …
PolGraphic
  • 3,233
  • 11
  • 51
  • 108
0
votes
1 answer

Conceal specific function from base class in derived class interface

struct IA { virtual void Init() = 0; ..... }; struct A : public IA { void Init() {}; ..... }; struct B : public A { int Init() { return 1; }; }; With such design i got error C2555: 'B::Init': overriding virtual function…
Yola
  • 18,496
  • 11
  • 65
  • 106
0
votes
0 answers

Clang compiler error with virtual inheritance

The code below compiles without error using GCC 4.8.1 on Ubuntu 13.10 and Apple's Clang 5.0 (based on normal Clang 3.3) on Mac OS 10.9. However Clang 3.4 on the Ubuntu box spits out the following errors: oz@MobileWolf:/prog/test$ clang++ -std=c++11…
Oz.
  • 5,299
  • 2
  • 23
  • 30
0
votes
1 answer

Type casting object to child class object

I want to cast object hlaObj to HlaObject from RtiValueAggregate but it's not working...! There is no inheritance between two classes. and HlaObject is virtually derived from RtiValue. can anyone tell me whats wrong with the following…
0
votes
1 answer

How to inherit class A from B while inheriting B from A?

So I have 2 classes namely book and mainscreen, where book is publically inherited from mainscreen. Now I want to use public member functions of class book via member functions of mainscreen. Here is my code: class book; class mainscreen:virtual…
0
votes
1 answer

MinGW 4.7.0 to 4.7.2 bug: Invalid "this" pointer in member functions when using mixed virtual and non-virtual multiple inheritance

I have code with inheritance that looks like this: B / \ / \ / \ BI D (template) / \ / \ / DI (template) [B]ase and [D]erived are interfaces that both contain a static method…
hauzer
  • 258
  • 3
  • 12
0
votes
1 answer

c++ Virtual/Non-virtual Diamond inheritance

Given the following code in C++: struct A { A() { f(0); } A(int i) { f(i); } virtual void f(int i) { cout << i; } }; struct B1 : virtual A { B1(int i) : A(i) { f(i); } virtual void f(int i) { cout << i+10; } }; struct B2 :…
Wes Field
  • 3,291
  • 6
  • 23
  • 26