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

Diamond issue when trying to resolve it using virtual inheritance

I am new in C++ and I am suck when resolving the diamond problem: here the code: #include using namespace std; // Base class class Base { public: virtual void getArea() { cout << "Called by Base\n"; …
Nizarkh
  • 13
  • 1
  • 4
0
votes
1 answer

Solution to diamond inheritance, but is it appropriate?

Aside from the other various things I've been working on, I've also been making an image api. One problem I've encountered though is the need for there to be an image base class that contains the dimensions of the image. From it would branch ImageIn…
user4578093
  • 231
  • 1
  • 3
  • 10
0
votes
1 answer

Move semantics in the presence of virtual bases

Consider the following program: #include #include #include #include using namespace std; struct Name { string s; Name(string s) : s(move(s)) { } }; struct A : virtual Name { A(string s) : Name(move(s)) { }…
0
votes
1 answer

C++ base class function call from last derived in diamond design

I'm learning C++ and after having read and tested a lot about multiple inheritance, virtual inheritance/methods and diamond design I still have some problems to understand it till the end. I'm in the diamond design pattern, where class B e C…
user3154898
  • 271
  • 1
  • 3
  • 13
0
votes
2 answers

How can I instantiate private data members of two base classes by defining a constructor of a derived class?

This is the problem statement: Design a base class called Student with the foll. 2 fields:- (i) Name (ii) Id. Derive 2 classes called Sports and Exam from the Student base class. Class Sports has a field called s_grade and class Exam has a field…
Suyash Shetty
  • 513
  • 3
  • 8
  • 17
0
votes
0 answers

Multiple inheritance - avoid due to diamond prob?

This arises often to me: I have a class A which is extended by class B and then there are classes which derive from either class A or both (multiple inheritance). Example: I have an object representing a mathematical model which defines an interface…
divB
  • 896
  • 1
  • 11
  • 28
0
votes
2 answers

Diamond (multiple inheritance) with no data members

Suppose we have the usual diamond-pattern: class A { public: virtual char foo() = 0; virtual ~A() {} }; class B : public A { public: virtual char foo() { return 'B';} virtual ~B() {} }; class C : public A { public: virtual…
Banex
  • 2,890
  • 3
  • 28
  • 38
0
votes
3 answers

C++ How to initilize abstract base class reference element?

My problem is based on typical diamond hierarchy, but is not a typical diamond problem. class Interface { public: int value; // SomeBigData &data; Interface(int _value = 0) : value(_value) {}; virtual void function1() = 0; …
0
votes
1 answer

unresolved external symbol with diamond structure

The following code works perfectly fine: #include "stdafx.h" class A1 { public: virtual void a1() = 0; }; class B1 : virtual public A1 { public: virtual void b1() { A1::a1(); } }; class A1_Impl : virtual public…
0
votes
1 answer

C++ standard: why are some "orders" defined and some not?

Having a class, the initialization order of its members is strongly defined in constructor (maybe to allow dependencies between members, like in this question - but I think this is more a design problem, I can imagine circular dependencies).…
Liviu
  • 1,859
  • 2
  • 22
  • 48
0
votes
2 answers

avoid specifying functions to call in diamond design

I have a diamond design currently, and I saw a related question here In that question, let me assume that class B and class C are virtual inherited from Class A, so in Class D, when we need to call virtual functions, we need to specify which one…
CJAN.LEE
  • 1,108
  • 1
  • 11
  • 20
0
votes
4 answers

Why java doesn't support multiple inheritance, though all the java method are virtual by default?

Why Java doesn't support multiple inheritance though all java method are virtual by default? Why diamond problem can arise in Java?
amritoit
  • 41
  • 9
0
votes
1 answer

diamond inheritance - Passing leaf class object as parameter in place of root class

i have a program with the following inheritance structure List / \ DoublyLinkedList CircularlyLinkedList \ / CircularlyDoublyLinkedList In…
S Raghav
  • 1,386
  • 1
  • 16
  • 26
0
votes
1 answer

c++ Virtual/Non-virtual Diamond inheritance

Given the following code in C++: struct A { A() { f(0); } A(int i) { f(i); } virtual void f(int i) { cout << i; } }; struct B1 : virtual A { B1(int i) : A(i) { f(i); } virtual void f(int i) { cout << i+10; } }; struct B2 :…
Wes Field
  • 3,291
  • 6
  • 23
  • 26
0
votes
1 answer

Deadly Diamond of Death in Coq

I'm trying to create a rather straight-forward type hierarchy. Here's a minimal working example: Record R0 : Type := { R0_S :> Type }. Record R1 : Type := { R1_S : Type; op1 : R1_S -> R1_S }. Record R2 : Type := { R2_S : Type; op2…
mhelvens
  • 4,225
  • 4
  • 31
  • 55