Virtual Inheritance is used to solve the Dreaded Diamond Problem associated with multiple inheritance in C++.

learn more… | top users | synonyms

3
votes
4answers
1k views

c++ virtual inheritance

Problem: class Base { public: Base(Base* pParent); /* implements basic stuff */ }; class A : virtual public Base { public: A(A* pParent) : Base(pParent) {} /* ... */ }; class B : virtual ...
13
votes
8answers
1k views

When virtual inheritance IS a good design?

EDIT3: Please be sure to clearly understand what I am asking before answering (there are EDIT2 and lots of comments around). There are (or were) many answers which clearly show misunderstanding of the ...
6
votes
4answers
972 views

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; ...
13
votes
5answers
791 views

How C++ virtual inheritance is implemented in compilers?

How the compilers implement the virtual inheritance? In the following code: class A { public: A(int) {} }; class B : public virtual A { public: B() : A(1) {} }; class C : public B { ...
6
votes
6answers
2k views

C++ Multiple Virtual Inheritance vs. COM

The net is overflowing with explanations of the "dreaded diamond problem". So is StackOverflow. I think I understand that bit, but I fail to translate that knowledge into comprehending something ...
9
votes
2answers
550 views

In C++, should I almost always use virtual inheritance?

I see from this entry that virtual inheritance adds sizeof(pointer) to an object's memory footprint. Other than that, are there any drawbacks to me just using virtual inheritance by default, and ...
8
votes
6answers
685 views

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 ...
14
votes
1answer
2k views

C++ Inheritance via dominance warning

I'm trying to implement a rather large object that implements many interfaces. Some of these interfaces are pure virtual. I may have a problem in diamond inheritance. Visual Studio is reporting a ...
21
votes
2answers
665 views

Downcast in a diamond hierarchy

Why static_cast cannot downcast from a virtual base ? struct A {}; struct B : public virtual A {}; struct C : public virtual A {}; struct D : public B, public C {}; int main() { D d; A& a = ...
9
votes
7answers
6k views

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 ...
15
votes
5answers
659 views

Is there any penalty/cost of virtual inheritance in C++, when calling non-virtual base method?

Is using virtual inheritance in C++ has a runtime penalty in compiled code, when we call regular function member from base class. Sample code: class A { public: void foo(void) {} }; class ...
15
votes
4answers
1k views

C++ private virtual inheritance problem

In the following code, it seems class C does not have access to A's constructor, which is required because of the virtual inheritance. Yet, the code still compiles and runs. Why does it work? class A ...
6
votes
4answers
1k views

Performance impact of virtual inheritence

I am considering using virtual inheritence in a real-time aplpication. Does using virtual inheritence have a performance impact similar to that of calling a virtual function? The objects in question ...
3
votes
4answers
334 views

Class sizes with virtual inheritance in C++

#include<iostream> using namespace std; class abc { int a; }; class xyz : public virtual abc { int b; }; int main() { abc obj; xyz obj1; ...
2
votes
3answers
3k views

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(). ...

1 2
15 30 50 per page