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

g++ "because the following virtual functions are pure" with abstract base class

Here is my example code which produces the error: struct Impl { int data_size_; int find(int var){return 0;} int get(int rowid){return 0;} }; class Container { public: Container() {} virtual ~Container() {} virtual int get_size() = 0; …
tdihp
  • 2,329
  • 2
  • 23
  • 40
11
votes
2 answers

Virtual Inheritance and dreaded diamond

I am having a hard time with a dreaded diamond problem. For a reminder, here is the classical class hierarchy of this problem: B / \ C1 C2 \ / D To solve it, the standard solution is to make C1 and C2 use virtual inheritance to…
10
votes
8 answers

Multiple Inheritance in java

Java is not allowing inheritance from multiple classes (still it allows inheritance from multiple interfaces.), I know it is very much inline with classic diamond problem. But my questions is why java is not allowing multiple inheritance like C++…
Silent Warrior
  • 5,069
  • 8
  • 41
  • 52
9
votes
4 answers

Consequences of changing inheritance to virtual?

I'm working on a huge project that I didn't start. My task is to add some additional functionality to what already is there. I'm in a situation where I have to use virtual inheritance because I have a diamond model. The situation is depicted in the…
9
votes
7 answers

Java: how do you call this multiple inheritance ambiguity?

Here's an example using multiple interface inheritance in Java and there's an issue. Note that I fully know why there's an issue and this is not the point of my question. The question is about how you name this particular multiple interface…
SyntaxT3rr0r
  • 27,745
  • 21
  • 87
  • 120
9
votes
3 answers

Fixing C++ Multiple Inheritance Ambiguous Call

I have three classes structured like this: #include using namespace std; class Keyword { public: virtual float GetValue() = 0; }; class CharacterKeyword : public Keyword { public: virtual float GetValue(){return…
larrylampco
  • 597
  • 1
  • 9
  • 33
9
votes
1 answer

Diamond Inheritance Lowest Base Class Constructor

The Code is as follow : The Code : #include using namespace std; class Animal{ int a; public: Animal(int a) : a(a){} int geta(){return a;} }; class Bird : virtual public Animal{ string b; public: Bird(int a…
caramel1995
  • 2,968
  • 10
  • 44
  • 57
8
votes
2 answers

Why can't I use `using` in a multiple inheritance c++?

I tried to implement some interfaces and their children. This is my idea: Interface / \ Interface2 InterfaceDefination | / Interface2Defination And this is my code: #include class…
VN VNA
  • 117
  • 1
  • 7
8
votes
7 answers

Question about multi-inheritance in C++?

I have the following code: #include "stdafx.h" #include #include using namespace std; #define MNAME 30 class Person { public: char name[MNAME + 1]; }; class Student : public Person { }; class Staff : public Person { }; class…
ipkiss
  • 13,311
  • 33
  • 88
  • 123
8
votes
2 answers

C++ Multiple Inheritance - why you no work?

I am trying to figure out an interesting multiple inheritance issue. The grandparent is an interface class with multiple methods: class A { public: virtual int foo() = 0; virtual int bar() = 0; }; Then there are abstract classes that are…
8
votes
1 answer

Is invocable and ambiguous call: bug in either g++ or clang

Consider the following code: // Preamble #include #include // A base class template struct base {void operator()(T){};}; // Two derived classes inheriting from the same base classes template struct…
Vincent
  • 57,703
  • 61
  • 205
  • 388
8
votes
1 answer

Explanation of Stroustrup Linearizing Class Hierarchies example

In Stroustrup's C++ Programming Language (4th ed), section 27.4.2 shows a technique to "linearize" a diamond class hierarchy to avoid the overhead of virtual base classes. He starts with the diamond pattern from a real project (Pivot code analyzer…
xan
  • 7,511
  • 2
  • 32
  • 45
8
votes
7 answers

Calling same method name from two different interface - Java

Java doesn't allow the multiple inheritance to protect diamond problem. It uses interface to take care of this problem. Then the case of using interface, let's say interface A{ run(); } interface B{ run(); } class C implements A, B{ run() {} …
Taewan
  • 1,167
  • 4
  • 15
  • 25
8
votes
3 answers

References to the same base classes must have separate offsets in memory

I've discovered some inconsistencies between compilers with this program, struct A { }; struct B : public A { float m; }; struct C : public A { B b; float n; }; struct D : public A { float n; B b; }; static_assert(sizeof(A)…
coderdave
  • 895
  • 2
  • 6
  • 15
7
votes
2 answers

Clojure - What's the benefit of Using Records Over Maps

I'm having a hard time deciding when using defrecord is the right choice and more broadly if my use of protocols on my records is semantic clojure and functional. In my current project I'm building a game that has different types of enemies that all…
newBieDev
  • 484
  • 3
  • 11
1 2
3
18 19