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

How to make Doxygen show diamond inheritance graphs

Doxygen has a really neat feature whereby it will generate inheritance graphs from code. However, when using multiple inheritance from classes with a common base, the plot shows two separate base classes (even though I'm using virtual inheritance as…
learnvst
  • 15,455
  • 16
  • 74
  • 121
4
votes
1 answer

Diamond inheritance with mixed inheritance modifers (protected / private / public)

let's say we have class A,B,C,D where A is base, B,C are between and D is derived in diamond model. NOTE: class B inherits virtualy class A in private mode, class C inherita virtualy class A in protected mode. class A { public: int member; //…
codekiddy
  • 5,897
  • 9
  • 50
  • 80
4
votes
3 answers

Why is single virtual inheritance not enough to resolve the dreaded diamond problem?

struct B { int i; }; struct D1 : virtual B {}; struct D2 : B {}; // <-- not virtual struct DD : D1, D2 {}; Having coded above, still the compiler demands D2 also to be virtual: DD d; d.i = 0; // error: request for member `i' is ambiguous What I…
iammilind
  • 68,093
  • 33
  • 169
  • 336
4
votes
4 answers

How to inherit generic trait multiple times in Scala?

I have a trait that looks like this: trait Ingredient[T] { def foo(t: T): Unit = { // Some complex logic } } And types for which I want to have methods: class Cheese class Pepperoni class Oregano How can I make another trait that has…
Ava
  • 818
  • 10
  • 18
4
votes
1 answer

How to make it so parent classes don't repeat a grandfather method that was already executed

We have a child class practicante who inherits from 2 classes: estudiante and empleado and both inherits from a grandfather-class persona. All of them have the method que_eres() which writes what class is the object (just for an example): #include…
4
votes
2 answers

Flora-2 diamond inheritance

Flora-2 is an eccentric language and I know this is a long shot but I haven't found any active resources devoted to it so I'm trying here. Its so popular... there is no stackoverflow tag for it. If you know anything about the status and future of…
Dave
  • 7,589
  • 12
  • 36
  • 42
4
votes
4 answers

Diamond shaped polymorphic Inheritance: sizeof Most derived Class

I understand that the Diamond shaped inheritance causes ambiguity and it can be avoided by using inheritance through virtual Base Classes, the question is not about it. The question is about sizeof the most derived class in a diamond shaped…
Alok Save
  • 202,538
  • 53
  • 430
  • 533
4
votes
1 answer

super() strange behavior in diamond inheritance in python

As the following example shows, super() has some strange (at least to me) behavior when used in diamond inheritance. class Vehicle: def start(self): print("engine has been started") class LandVehicle(Vehicle): def start(self): …
user3738870
  • 1,415
  • 2
  • 12
  • 24
4
votes
1 answer

Is my diamond inheritance compiler error impossible to solve?

Structure I have created a diamond inheritance problem. It looks like this I thought I understood virtual inheritance fairly well, however I now think that I have slightly missunderstood it. It was my understanding that virtual inheritance tells…
FreelanceConsultant
  • 13,167
  • 27
  • 115
  • 225
4
votes
1 answer

"Code in Interfaces" Kotlin, how do they avoid the "deadly diamond of death"?

I was reading this article and it says that you can write code in Kotlin interfaces. Java did not allow writing code in interface to avoid diamond problem as of this answer. If Kotlin allows code in interface and multiple interfaces can be…
AwaisMajeed
  • 2,254
  • 2
  • 10
  • 25
4
votes
2 answers

Avoid multiple inheritance induced ambiguity by using scope resolution

Here is an example of multiple inheritance. I used the scope resolution operator to resolve the ambiguity instead of a virtual class. struct A { int i; }; struct B : A {}; struct C : A {}; struct D: B, C { void f() { B::i =…
msc
  • 33,420
  • 29
  • 119
  • 214
4
votes
3 answers

Do you need to call virtual base class constructor from all derived classes? Even if they're not the most derived?

I am having trouble with multiple inheritance and the diamond problem. The problem occurs because my base class constructor requires a parameter. The compiler tries to generate a default constructor for my two abstract classes, but that fails…
4
votes
1 answer

Why not diamond inheritance with only one virtual inheritance?

Say we have the classic multiple inheritance schema: class Base { int baseMember; }; class A : public Base { int aMember; }; class B : public Base { int bMember; }; class Derived : public A, public B { int derivedMember; }; This…
Black Mantha
  • 1,147
  • 8
  • 11
4
votes
1 answer

How does "override" work when inherited traits are combined?

I'm experimenting with multiple inheritance in Scala. I get that there is a right-to-left resolution, but I don't understand the role of the override keyword. Let's consider the following snippet : trait A { def method …
Dici
  • 25,226
  • 7
  • 41
  • 82
4
votes
1 answer

Overcoming diamond ambiguity in different way

I know the diamond problem and method to solve it using virtual base class. I tried to solve diamond problem in a different way but did not succeed. I don't know why. #include using namespace std; class A { public: void…
Rohit Negi
  • 435
  • 1
  • 4
  • 10