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

Multiple virtual inheritance C++

I have a problem with multiple inheritance and it would be great if someone could help me out. I am programming a situation which ultimately boils down to something similar to this class A { public: A(){} virtual ~A() = 0; virtual…
0
votes
1 answer

Shortcode Not working in wordpress main content fild

I use a shortcode and by this shortcode i want The main title , A thumbnail ,some content and then a permalink of my post . But using this short code Wordpress show the full content no Title ,Thumbnail, and permalink. What should i do now. The photo…
Runa
  • 5
  • 3
0
votes
1 answer

multiple vtables, ending up in the wrong one when making a call

I have the following setup: class CRpCat : public CQueryDataBase, public IRpCat { public: CRpCat(); virtual ~CRpCat(); // IRpCat public: virtual HRESULT Initialize(); ..... blah blah ....... class CQueryDataBase :…
alernerdev
  • 2,014
  • 3
  • 19
  • 35
0
votes
0 answers

Will this multiple inheritance code work with all compiler?

I have this code that works fine when compiled with Visual Studio. #include "stdafx.h" class base { public: virtual void FOO() = 0; }; class D1:public base { public: virtual void bar() =0; }; class D2:public base { public: virtual…
user7966835
0
votes
1 answer

Memory allocation for multiple inheritance with non-default constructors

I'm struggling a bit to understand multiple-inheritance. Apparently I chose to solve a very complicated problem that has multiple inheritance and also the diamond problem. Even if I I have found a couple of cases that are similar to my problem, for…
rodrigolece
  • 1,039
  • 2
  • 13
  • 19
0
votes
1 answer

c++ Writing to Binary files from multiple inheritance class

I have a case similar to "The diamond of death" I have class B and C which Virtually Inherit Class A, and also Class D which Inherit Classes B and C. A / \ B C \ / D B & C has inherited members, and also their own members. D has…
0
votes
1 answer

Is it correct to assume that diamond inheritance breaks encapsulation in C++?

Look at the following code: class Base { int a; public: Base(int b){a =b;} }; class M1: public virtual Base{ public: M1(int a): Base(a+10){} // Expect a is increased by 10 }; class M2: public virtual Base{ public: M1(int a):…
CoffeDeveloper
  • 7,961
  • 3
  • 35
  • 69
0
votes
0 answers

Share variables with classes multiple inheritance in C++

I'm trying to design class hierarchy for my physical simulations. I really want to share variables (e.g. Vec3d pos, Quat4d rot ) between classes which depend on each other by more complex graph of multiple-inheritance. I extracted simple example…
Prokop Hapala
  • 2,424
  • 2
  • 30
  • 59
0
votes
2 answers

Can I use the implementation of a virtual method from another parent class

I have a kind of "triangle inheritance" problem, if such a thing exists. Basically, I have an abstract base class the defines an interface, and a policy class that defines the implementation to part of that interface, like so: class…
Stefan Sullivan
  • 1,517
  • 2
  • 10
  • 9
0
votes
2 answers

Can't access protected member variables of the most base class through std::unique_ptr in diamond

I am not an advanced programmer. Suppose there is a classic diamond inheritance: class Base class A: virtual public Base class B: virtual public Base class Last: public A, public B Suppose Base has a variable, m_x, that is common to both A and B,…
a concerned citizen
  • 787
  • 2
  • 9
  • 25
0
votes
2 answers

C++ Diamond of Doom with external SDK

I have this annoying multiple-inheritance diamond of doom with a complicated twist (We're talking about MS COM objects, a detail which will be relevant later) - Assume an abstract class (interface) A which has some pure virtual methods. Another…
Dan
  • 261
  • 2
  • 8
0
votes
1 answer

Prefer variables from one class in diamond inheritance

I want to prefer variables from one class in diamond inheritance. This is the code: class DiamondBase { protected: int x; }; class DiamondSonA: public DiamondBase {}; class DiamondSonB: public DiamondBase {}; class DiamondGS: public DiamondSonA,…
hudac
  • 2,584
  • 6
  • 34
  • 57
0
votes
0 answers

Implementation of a derived interface

In my code I had an interface and its implementation: class Interface { public: virtual ~Interface() {} virtual void f() = 0; }; class Impl : public Interface { public: virtual ~Impl() {} virtual void f() { // ... …
grześ
  • 467
  • 3
  • 21
0
votes
2 answers

Virtual Inheritance: what happens if the keyword is at some point forgotten?

What happens if, in a large chain of inheritances, the virtual keyword is at some point forgotten? For example: struct I {}; struct A : virtual I {}; struct B : A, virtual I {}; struct C : B, /* virtual */ I {}; // ooops, distraction error Is…
nyarlathotep108
  • 5,275
  • 2
  • 26
  • 64
0
votes
1 answer

C++ Multiple Inheritance With Proprietary Base Class

I am working with SFML (Simple Fast Media Library) on a hobbyist game design project, and ran into quite an annoying conundrum working with sf::Sprite, sf::Drawable, and cloning. Here's the issue: As a major fan of composite and factory design…
Jordan
  • 3
  • 1