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
7
votes
1 answer

How does Raku deal with the diamond problem (multiple inheritance)?

So it's no secret that Raku has multiple inheritance, so that got me wondering: "how does Raku deal with that in any reasonable manner?" Some preliminary testing reveals that default behaviour is inherited from the first class in the inheritance…
Electric Coffee
  • 11,733
  • 9
  • 70
  • 131
7
votes
6 answers

Why is there ambiguity in this diamond pattern?

#include using namespace std; class A { public: void eat(){ cout<<"A";} }; class B: public A { public: void eat(){ cout<<"B";} }; class C: public A { public: void eat(){ cout<<"C";} }; class D: public B,C { public: void…
Moeb
  • 10,527
  • 31
  • 84
  • 110
7
votes
3 answers

Preferences of abstract classes over interfaces in Java 8

Now, we know that Java 8 has introduced default and static methods in interfaces. Interfaces were originally introduced in Java to avoid the diamond problem that occurred in C++, in multiple inheritance. But along with the introduction of default…
Aditya Singh
  • 2,343
  • 1
  • 23
  • 42
7
votes
2 answers

Size of polymorphic class derived virtually

I am having hard time to undertsand what constitutes the size of following classes? I am using MSVS 2008 (VC 9.0 compiler). I have read that if I do not declare virtual functions(in below example) then Class D will contain 2 extra pointer(1 from B…
7
votes
7 answers

How do interfaces solve the diamond problem?

I need to discuss one thing with you. I have been reading about the interface that it is a contract between the class the interface that the class will provide implementation of all the methods of the interface. To solve the famous diamond problem,…
Usman Khalid
  • 3,032
  • 9
  • 41
  • 66
6
votes
6 answers

Diamond problem when using MixIns in Python

Please consider the following code implementing a simple MixIn: class Story(object): def __init__(self, name, content): self.name = name self.content = content class StoryHTMLMixin(object): def render(self): return…
Escualo
  • 40,844
  • 23
  • 87
  • 135
6
votes
2 answers

What is the proper approach to swap and copy idiom in virtual inheritance?

Consider classic virtual inheritance diamond hierarchy. I wonder to know what is the right implementation of copy and swap idiom in such hierarchy. The example is a little artificial - and it is not very smart - as it would play good with default…
6
votes
2 answers

The process of creating a class that involves virtual inheritance

In many tutorials describing the usage of virtual base classes (usually used to solve the diamond problem), they often have a code similar to the design of this structure: class Animal { public: Animal() { cout << "Creating…
ZERO
  • 316
  • 7
  • 16
5
votes
0 answers

How do I remove this ambiguity in hybrid inheritance?

I have a case in project where I need to inherit an abstract class virtually into two different classes which are again inherited into final class but the compiler keeps showing me this error error C2250: 'D': ambiguous inheritance of 'B…
5
votes
1 answer

Diamond Inheritance in Python

A question about diamond shape inheritance in python: A / \ B C \ / D I first created this class A: def __init__(self): print('a') class B(A): def __init__(self): print('b') A.__init__(self) class C(A): …
Sekomer
  • 688
  • 1
  • 6
  • 24
5
votes
2 answers

How should I call parent move constructor in diamond pattern?

Consider following diamond-like multiple inheritance: class base; class d1 : virtual public base; class d2 : virtual public base class d3 : public d1, public d2; base is a move-only class (having a large move-only buffer). So are d1, d2 and d3.…
sorush-r
  • 10,490
  • 17
  • 89
  • 173
5
votes
1 answer

C++ multiple diamonds inheritance and pure virtual functions

Consider the following architecture: class A //abstract interface { public: virtual void f() = 0; }; class AA : public A //abstract interface { public: virtual void g() = 0; }; class AAA : public AA //abstract interface { …
5
votes
3 answers

c++ virtual inheritance difference

Given two classes with a common virtual base class: class Base {}; class Derived1 : public virtual Base {}; class Derived2 : public virtual Base {}; Is there any difference between these two further derived classes?: class Derived3 : public…
quazeeee
  • 323
  • 2
  • 14
5
votes
1 answer

Field diamond pattern in multiple abstract model inheritance in Python/Django

I am having the following model class hierarchy: from django.db import models class Entity(models.Model): createTS = models.DateTimeField(auto_now=False, auto_now_add=True) class Meta: abstract = True class Car(Entity): pass …
stf
  • 591
  • 1
  • 4
  • 19
5
votes
3 answers

Diamond inheritance and the Common Lisp Object System

I am trying to find a solution to typical diamond inheritance problem in Common Lisp CLOS. The code : (defclass C1.0 () ... ) (defclass C2.1 (C1.0) ...) (defclass C2.2 (C1.0) ...) (defclass C3.0 (C2.1 C2.2) ...) (defmethod m1 ((obj C1.0))…
RKS
  • 309
  • 1
  • 6