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
0
votes
0 answers

Question about deadly diamond problem (multiple inheritance)

I know in stackoverflow it has many people asked deadly diamond problem already, but the question that they asked is not what I want to ask, so could anyone please tell me what is the solution when I encounter this kind of problem. The following…
Saxon
  • 1
0
votes
1 answer

What is the proper way to use mix-ins in C++ to fulfill interface contracts?

I apologize in advance for the highly specific example. Let's say I have an "interface" (purely abstract class) Drawable which requires an implementation of virtual void draw(), as well as virtual float compute_z(). Let's say draw() is supposed to…
Alexander Guyer
  • 2,063
  • 1
  • 14
  • 20
0
votes
0 answers

How to make the following multiple inheritance work in C++

The idea is for the users to create a new class and inherit only the base class and few special classes whose special functions they want. class Base { Data data; public: void init(Data d) data(d) {} // other basic functions } class…
maverick
  • 119
  • 4
0
votes
1 answer

Cout Overload Operating for multiple inheritance

I have 4 classes, Vehicle, PassengerVehicle, CargoVehicle, and CrewHauler. I will draw a diagram and put data members inside the parenthesizes. Vehicle (model, brand, year) …
0
votes
0 answers

Diamond problem with four interfaces, no code duplication

What if I have 4 given interfaces A, B, C and D and the inheritance is like this: B and C extend A D extends B and C The goal is to implement those interfaces without any code repeating. I think I understand it's a kind of Diamond Problem, but I…
anon
  • 3
  • 3
0
votes
1 answer

Virtual classes in diamond inheritance with multiple classes in between base class and most derived class

Okay so in the classic diamond inheritance example you have a base class, two classes that inherit from that base class and then another class that inherits from those two, making a nice symmetrical diamond. In that case the two classes inherit…
0
votes
2 answers

Need Clarity on the Implementation Part of the Diamond Problem

Answers for Why multiple inheritances are not possible? If there is a method in A that B and C have overridden, and D does not override it, then which version of the method does D inherit: that of B, or that of C? Okay. Fine Why it does not apply to…
AjithKumar
  • 21
  • 6
0
votes
1 answer

Solve diamond problem without virtual inheritance

Here's the situation: class A { public: A(); void actionA(); private: int a; }; class B1 : public A { ... }; class B2 : public A { ... }; class OrthogonalFeature : public A { void orthogonalFeature(); }; class C1 : public B1 …
DevShark
  • 8,558
  • 9
  • 32
  • 56
0
votes
1 answer

Virtual class in diamond inheritance

From my understanding, using virtual suppresses the constructor of the base class, and therefore is able to avoid multiple objects of the base class being instantiated. If the constructor of both the derived class is suppressed, how is that one…
0
votes
2 answers

How to have multiple traits of same base trait implement the same method

I have a scenario with multiple traits inheriting from a fixed base trait. The base trait has an abstract method that each needs to implement. The class using these traits also needs to implement this method: trait A { def f: Any } trait B1…
user79074
  • 4,937
  • 5
  • 29
  • 57
0
votes
1 answer

Solving this specific C++ diamond problem for Qt classes

I'm using QT's QQuickFramebufferObject class which inherits from QQuickItem in Qt library. I have the following user-defined class: class OpenGlBufferItem: public QQuickFramebufferObject And I need my OpenGlBufferItem class to also derive from…
PPP
  • 1,279
  • 1
  • 28
  • 71
0
votes
1 answer

Diamond Problem| Parmeterized constructor is not called of grandparent class

Can someone please explain the multiple inheritance of below ,why parameterize constructor of d class is not called even though vehicle class is being constructed using parameterize constructor. #include using namespace std; class d{ …
0
votes
4 answers

C++ / Diamond inheritance / Static Variables

I am facing some design problems, I would like to write: class A { ... }; class B : public A { static string type_; ... }; class C : public A { static string type_; ... }; class D : public B, public C { static string type_; ... }; I think up to the…
CLO
  • 1
  • 3
0
votes
1 answer

Why do I have to explicitly define a method provided by an inhereted class?

Consider the following: #include struct animal { public: virtual std::string speak() = 0; }; struct bird : public animal { public: std::string speak() { return "SQUAK!"; } }; struct landAnimal : public animal { …
Carbon
  • 3,828
  • 3
  • 24
  • 51
0
votes
1 answer

c++ diamond inheritance construct only by base constructor

I have trouble to construct my queen class from using the piece, which is the base class, i had virtual my rook and bishop class, to prevent duplicate problem. right now i got the error stated the Rook and Bishop is virtual and it don't have a…