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

Why 'virtual' inheritance is not the default behaviour?

I understand the requirement of using virtual keyword when deriving from base classes to avoid diamond inheritance related ambiguity problems. But, my question is why this is not the default behaviour in C++ when deriving classes regardless whether…
4
votes
2 answers

Clarify the concept of Multiple inheritance: Can a diamond structure issue occur?

Suppose you have two interfaces I1 and I2 both the interfaces have the same method 1)public int add(int a) throws exception e1 //(Interface 1) 2)public int add(int a) throws exception e2 //(Interface 2) now a class implements both I1 and…
4
votes
3 answers

Why does GCC give me an error: no unique final overrider?

In the code below, I get the following warning and error: test.cpp:15: warning: direct base 'B' inaccessible in 'D' due to ambiguity test.cpp:15: error: no unique final overrider for 'virtual void A::f()' in 'D' But if I remove the virtual…
Rai
  • 1,328
  • 11
  • 20
4
votes
3 answers

C++ Diamond-like inheritance

I have a class Channel with two two properties, direction and size which are fixed during construction. Direction can take only one of two values, forward (1) or backward(-1). Size can take any value, but there is a physically meaningful distinction…
4
votes
3 answers

Is this diamond inheritance UB a bug in MinGW?

#include #include class VeryBase { protected: int a_; public: VeryBase() : a_(1) {} virtual operator std::string() { return "0"; } }; class Base1 : public virtual VeryBase { protected: int…
isCasted
  • 41
  • 3
4
votes
1 answer

One-Many-One Inheritance in Python

A question about whether or not I'm going about something in the best way... I would like to have a class hierarchy in Python that looks (minimally) like the following; class Actor class Mover(Actor) class Attacker(Actor) class Human(Mover,…
unwitting
  • 3,346
  • 2
  • 19
  • 20
3
votes
2 answers

C++ Multipath Inheritance : Why the access using Base class scope is non-ambiguous?

I am studying C++ and while studying virtual inheritance, I came across following doubt: class A { public: int x; A() { x = 677; } A(int a) { cout << "A con , x= " << a << endl; x = a; } }; class B : public A…
Vivek Mangal
  • 532
  • 1
  • 8
  • 24
3
votes
1 answer

Forced to call the base constructor when using virtual inheritance although it will never be called?

I have a class Base which has a parameterized constructor and two classes Middle1 and Middle2 which virtually inherit from Base (in order to solve the diamond problem). In addition, class Foo inherits from Middle1 and Middle2. Foo now calls the Base…
3
votes
3 answers

How to avoid parallel class hierarchy in python

I've been running into a weird little smell in my python code lately and I think it has something to do with parallel inheritance. Here is a small example I concocted: class DogHabits: def __init__(self): self.habits = ['lick…
user32882
  • 5,094
  • 5
  • 43
  • 82
3
votes
2 answers

Understanding super in Python

Could you please explain to me how to write the deadly diamond in Python? I saw many examples of similar code without using constructor arguments, but once I start using arguments thigs start being messy... class A: def __init__(self, a): …
3
votes
0 answers

Implement a clone method with covariance and diamond inheritance fails with MSVC C++ compiler

I am trying to implement a clone method (deep copy) with covariance in the context of a diamond inheritance with a common base (this is why I need the virtual keyword in C1 and C2 classes declaration). It works pretty well if I have only typed…
Kouchy
  • 31
  • 1
3
votes
1 answer

Finalise Virtual inheritance

In my code, I have a basic diamond pattern: CommonBase / \ / \ DerivedA DerivedB \ / \ / Joined It's implemented like this, with the common base class having a default constructor and a…
CrushedPixel
  • 1,152
  • 2
  • 13
  • 26
3
votes
1 answer

How diamond problem in oops is solved using "shared" strategy?

Diamond problem is handled in some OOPS languages (eg. curl) by having the repeatedly inherited class as "shared"? I want to know how this works. Also, I want to know the role played by primary and secondary constructors in solving the diamond…
3
votes
3 answers

How to ensure that the assignment operator on a virtual base class is called only once?

I'm using virtual inheritance as in the typical diamond problem: A (virtual) / \ (virtual) B C \ / D I'm implementing a method named "deep_copy_from" in every class (but it could be the assignment…
e.tadeu
  • 5,024
  • 2
  • 20
  • 21
3
votes
1 answer

Why is super class empty constructor required but not called in a dreaded diamond situation?

I am trying to achieve the following design, which is a dreaded diamond situation: struct super_base { super_base(int a) { b = a; } int b; }; struct base : virtual super_base {}; struct other_base : virtual super_base {}; struct derived :…