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

Rules for Diamond prob resolution or Multiple Inheritance in Java8

In Java a class can extend only one parent class but can implement multiple interfaces. With the introduction of default methods in Java 8 interface, there’s the possibility of a class inheriting more than one method with the same signature by…
Ankur Singh
  • 339
  • 3
  • 16
3
votes
1 answer

DIAMOND situation when I want to extend the functionality of my current application

Suppose I have an application which has multiple classes (inheritance used) Now one day, I have to add new specification to my application and that would require me to use multiple inheritance. for example this code: class A{}; class B : public…
3
votes
1 answer

Configure buildbot for builders with diamond dependencies

Say, I have a project with 4 different jobs (builders): Library Binary A (dependent on Library) Binary B (dependent on Library) Package (dependent on Binary A and Binary B) and I would like to setup Continuous Integration using BuildBot. At the…
3
votes
3 answers

C++ Weird Diamond inheritance issue

I have this A / \ B C \ / D A has a pure virtual function, prototyped as: virtual A* clone(void) const = 0; B and C virtually inherit from A ( class B: public virtual A, class C: public virtual A) B has the virtual function,…
snoofkin
  • 8,725
  • 14
  • 49
  • 86
3
votes
4 answers

Any established practices on overcoming the lack of multiple inheritance in Java?

I have a classic diamond inheritance problem where A / \ B C \ / D are all interfaces, and I have AImpl(A) | \ | \ BImpl(B) CImpl(C) | \ | \ DImpl(B,C) \ | F(C) | E(B,C) where class E…
wrongusername
  • 18,564
  • 40
  • 130
  • 214
3
votes
1 answer

Should we always use `override` in Trait

Should we always use override in Trait to solve preemptively the diamond inheritance issue ? Let's see an example to explain the point : trait S { def get : String } trait A extends S { override def get = "A" } trait B extends S { override def get =…
Yann Moisan
  • 8,161
  • 8
  • 47
  • 91
3
votes
3 answers

Diamond problem with only 1 virtual inheritance

Does this still solve the diamond problem? class A {}; class B : virtual A {}; class C : A {}; class D : B, C {}; Edit: If not, what is it then? Is it the same as this? class A {}; class B : A {}; class C : A {}; class D : B, C {}; Or is it…
NPS
  • 6,003
  • 11
  • 53
  • 90
3
votes
3 answers

Virtual inheritance in a non Diamond Type

I have trouble understanding why virtual inheritance is useful in a situation where we do not meet a problem similar to the Diamond Problem when a class inherits from 2 classes derived from the same base class. Could someone give me an example or…
CubSqared
  • 195
  • 1
  • 5
3
votes
1 answer

Inheriting from multiple/diamond Inheritance

i have the following scenario: class A { public: A(std::string id); }; class B : public virtual A { public: B(); }; class C : public virtual A { public: C(); }; class D : public B, public C { public: D(std::string…
3
votes
1 answer

Got diamond inheritance working, but Eclipse still complains

thanks for looking. I'm going through the software patterns in c++ to become familiar with it, and am having a problem with interface-based programming - namely the diamond problem. Here's the situation: there's a Widget class from which is derived…
2
votes
2 answers

diamond inheritance of classes with same members in python and with super

I find myself in the strange situation of diamond inheritance, even worse is that the classes in the middle of the diamond share a member. Below I've shown a cut down piece of code which highlights my problem. The method I used in writing classes I…
scruffyDog
  • 721
  • 3
  • 7
  • 17
2
votes
1 answer

diamond shaped multiple inheritance pattern

Below is a diamond problem faced in multiple inheritance, class Base { public: Base() { cout << "Empty Base constructor " << endl; } Base(const string & strVar) { m_strVar = strVar; cout << m_strVar << endl; } virtual…
mu_sa
  • 2,685
  • 10
  • 39
  • 58
2
votes
1 answer

constructor not Inheriting

I was tryiing to inherit a comstructor from base class to the a derived class which was derived from another derived class of the base class. but it says " no deafault constructor exists for class Waiverstudents " after inheriting the base class…
Siddik_911
  • 23
  • 4
2
votes
0 answers

How to solve the Diamond Problem without using virtual function

#include using namespace std; class student{ public: int a; get_a(){ cout<<"Give value of a"; cin>>a; } }; class student1: public student{ public: int b; get_b(){ …
2
votes
1 answer

C++ Member and vtable order in diamond (multiple) virtual inheritance

I wanted to know the ordering of member variables and vtable pointers in C++ on a diamond virtual inheritance. Consider the below inheritance: class Base { int b; }; class Derived: public virtual Base { int d; }; class Derived2: public…