Questions tagged [diamond-problem]

In object-oriented programming languages with multiple inheritance and knowledge organization, the diamond problem is an ambiguity that arises when two classes B and C inherit from A, and class D inherits from both B and C.

The diamond problem is an ambiguity that arises when two classes B and C inherit from A, and class D inherits from both B and C.

enter image description here

Example In C++:

/*
The Animal class below corresponds to class 
A in our graphic above
*/
class Animal { /* ... */ }; // base class
{
int weight;

public:

int getWeight() { return weight;};

};

class Tiger : public Animal { /* ... */ };

class Lion : public Animal { /* ... */ }    

class Liger : public Tiger, public Lion { /* ... */ };  


int main( )
{
Liger lg ;

/*COMPILE ERROR, the code below will not get past
any C++ compiler */

int weight = lg.getWeight();  
}
276 questions
171
votes
17 answers

Java Multiple Inheritance

In an attempt to fully understand how to solve Java's multiple inheritance problems I have a classic question that I need clarified. Lets say I have class Animal this has sub classes Bird and Horse and I need to make a class Pegasus that extends…
120
votes
5 answers

How does virtual inheritance solve the "diamond" (multiple inheritance) ambiguity?

class A { public: void eat(){ cout<<"A";} }; class B: virtual public A { public: void eat(){ cout<<"B";} }; class C: virtual public A { public: void eat(){ cout<<"C";} }; class D: public B,C { public: void eat(){…
Moeb
  • 10,527
  • 31
  • 84
  • 110
101
votes
4 answers

python multiple inheritance passing arguments to constructors using super

Consider the following snippet of python code class A(object): def __init__(self, a): self.a = a class B(A): def __init__(self, a, b): super(B, self).__init__(a) self.b = b class C(A): def __init__(self, a, c): …
Lin
  • 1,547
  • 2
  • 12
  • 26
68
votes
5 answers

What are the differences between abstract classes and interfaces in Java 8?

In Java there used to be a subtle but important difference between abstract classes and interfaces: default implementations. Abstract classes could have them, interfaces could not. Java 8 though introduces default implementations for interfaces,…
53
votes
3 answers

Why doesn't a using-declaration work to solve the diamond problem?

Please consider the following code: struct A { void f() { } }; struct B1 : A { }; struct B2 : A { }; struct C : B1, B2 { void f() // works { B1::f(); } //using B1::f; // does not work //using B1::A::f; //…
gd1
  • 11,300
  • 7
  • 49
  • 88
44
votes
1 answer

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 warning of C4250 ('class1' : inherits…
Cem Kalyoncu
  • 14,120
  • 4
  • 40
  • 62
39
votes
7 answers

C++ diamond problem - How to call base method only once

I'm using multiple inheritance in C++ and extending base methods by calling their base explicitly. Assume the following hierarchy: Creature / \ Swimmer Flier \ / Duck Which corresponds to class Creature { …
O. Aroesti
  • 1,016
  • 11
  • 32
34
votes
4 answers

Diamond Problem

Wikipedia on the diamond problem: "... the diamond problem is an ambiguity that arises when two classes B and C inherit from A, and class D inherits from both B and C. If a method in D calls a method defined in A (and does not override the method),…
cretzel
  • 19,864
  • 19
  • 58
  • 71
33
votes
6 answers

Multiple Inheritance Ambiguity with Interface

We all know about the diamond problem regarding multiple inheritance - A / \ B C \ / D This problem describe an ambiguous situation for class D. If class A has a method and both/either of B and/or C override the method then which…
Razib
  • 10,965
  • 11
  • 53
  • 80
28
votes
6 answers

Why is the diamond case with its common ancestor used to explain Java multiple inheritance issue, instead of two unrelated parent classes?

This question might sound weird to Java people but if you try to explain this, it would be great. In these days I am clearing some of Java's very basic concept. So I come to Inheritance and Interface topic of Java. While reading this I found that…
Roshan Jha
  • 2,091
  • 1
  • 21
  • 30
25
votes
4 answers

C++ virtual override functions with same name

I have something like that (simplified) class A { public: virtual void Function () = 0; }; class B { public: virtual void Function () = 0; }; class Impl : public A , public B { public: ???? }; How can I implement the…
QbProg
  • 1,675
  • 1
  • 14
  • 18
23
votes
2 answers

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 = d; D* p = static_cast(&a); //error } g++ 4.5…
log0
  • 10,489
  • 4
  • 28
  • 62
22
votes
3 answers

Inheritance by dominance - is it really bad?

I'm one of those people that has to get their code to compile with 0 warnings. Normally I respect the compiler and if it issues me a warning I take it as a sign that I should touch up my code a little. If I have to tell a compiler to ignore a given…
Shirik
  • 3,631
  • 1
  • 23
  • 27
20
votes
7 answers

Diamond inheritance (C++)

I know that having diamond inheritance is considered bad practice. However, I have 2 cases in which I feel that diamond inheritance could fit very nicely. I want to ask, would you recommend me to use diamond inheritance in these cases, or is there…
Igor
  • 26,650
  • 27
  • 89
  • 114
19
votes
6 answers

Multiple inheritance + virtual function mess

I have a diamond multiple inheritance scenario like this: A / \ B C \ / D The common parent, A, defines a virtual function fn(). Is it possible for both B and C to define fn()? If it is, then the next question is - can D access…
shoosh
  • 76,898
  • 55
  • 205
  • 325
1
2 3
18 19