diamond-problem

Diamond Inheritance Lowest Base Class Constructor

泄露秘密 提交于 2019-11-27 14:48:34
The Code is as follow : The Code : #include <iostream> 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 , string b) : Animal(a) , b(b){} }; class Fish : virtual public Animal{ int f; public: Fish(int a , int f) : Animal(a) , f(f){} }; class Unknown : public Bird, public Fish{ char u; public: Unknown(int a , int f , string b , char u ) : Bird(a , b) , Fish(a , f) , u(u){} //Problem }; The Question : 1.)How am I going to initialize all the superclass if the Unknown class is

C++ - downcasting a diamond shape inherited object without RTTI/dynamic_cast

随声附和 提交于 2019-11-27 13:07:02
问题 I'm currently working on integrating a third-party package that uses lots of RTTI stuff on a non-RTTI platform (Android). Basically, I did my own RTTI implementation but I'm stuck on a problem. The issue is that a lot of classes are having the diamond inheritance problem since all the classes derive from the same base class (object).. and so, if I want to downcast from the base class to the derived class, I have to use a dynamic_cast - but RTTI is not available! How do I convert an object

Multiple inheritance + virtual function mess

﹥>﹥吖頭↗ 提交于 2019-11-27 12:50:15
I have a diamond multiple inheritance scenario like this: A / \ B C \ / D The common parent, A, defines a virtual function fn(). Is it possible for both B and C to define fn() ? If it is, then the next question is - can D access both B and C's fn() without disambiguation? I'm assuming there is some syntax for this.. And is it possible for D to do that without knowing specifically who are B and C? B and C can be replaces by some other classes and I want the code in D to be generic. What I'm trying to do is to have D somehow enumerate all of the instances of fn() it has in its ancestry. Is this

C++ Inheritance via dominance warning

巧了我就是萌 提交于 2019-11-27 11:19:42
问题 I'm trying to implement a rather large object that implements many interfaces. Some of these interfaces are pure virtual. I may have a problem in diamond inheritance. Visual Studio is reporting a warning of C4250 ('class1' : inherits 'class2::member' via dominance) . First of all these classes are inherited virtually as it should be. The following is the partial class design that causes this problem. A B C \ / \ / \ / \ / AB BC | | | BC2 | | \ D: Implementation of B, C, BC, BC2 \ / Big In

Diamond Problem

情到浓时终转凉″ 提交于 2019-11-27 04:22:42
Wikipedia on the diamond problem: "... 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. If a method in D calls a method defined in A (and does not override the method), and B and C have overridden that method differently, then from which class does it inherit: B, or C?" So the diamond looks like this: A / \ B C \ / D My question is, what happens if there is no such class A, but again B and C declare the same method, say foo(). Isn't this the same problem? Why is it then called diamond problem? Example: class B {

Diamond inheritance (C++)

隐身守侯 提交于 2019-11-27 00:58:38
I know that having diamond inheritance is considered bad practice. However, I have 2 cases in which I feel that diamond inheritance could fit very nicely. I want to ask, would you recommend me to use diamond inheritance in these cases, or is there another design that could be better. Case 1: I want to create classes that represent different kinds of "Actions" in my system. The actions are classified by several parameters: The action can be "Read" or "Write". The action can be with delay or without delay (It is not just 1 parameter. It changes the behavior significantly). The action's "flow

Multiple Inheritance in java

ⅰ亾dé卋堺 提交于 2019-11-26 20:48:13
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++ when there is no ambiguity (and hence no chances of diamond problem) while inheriting from multiple base class ? It was a design decision of Java. You'll never get it, so don't worry too much about it. Although MI might help you make Mixins, that's the only good MI will ever do you. I have read that most programmers don't use multiple inheritance in a

C++ virtual override functions with same name

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-26 16:58:47
问题 I have something like that (simplified) class A { public: virtual void Function () = 0; }; class B { public: virtual void Function () = 0; }; class Impl : public A , public B { public: ???? }; How can I implement the Function () for A and the Function() for B ? Visual C++ lets you only define the specific function inline (i.e. not in the cpp file), but I suppose it's an extension. GCC complains about this. Is there a standard C++ way to tell the compiler which function I want to override?

Diamond Inheritance Lowest Base Class Constructor

你说的曾经没有我的故事 提交于 2019-11-26 16:55:41
问题 The Code is as follow : The Code : #include <iostream> 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 , string b) : Animal(a) , b(b){} }; class Fish : virtual public Animal{ int f; public: Fish(int a , int f) : Animal(a) , f(f){} }; class Unknown : public Bird, public Fish{ char u; public: Unknown(int a , int f , string b , char u ) : Bird(a , b) , Fish(a , f) , u(u){} //Problem

What are the differences between abstract classes and interfaces in Java 8?

你。 提交于 2019-11-26 16:01:37
In Java there used to be a subtle but important difference between abstract classes and interfaces: default implementations . Abstract classes could have them, interfaces could not. Java 8 though introduces default implementations for interfaces, meaning this is no longer the critical difference between an interface and an abstract class. So what is? As best as I can tell, the only remaining difference (besides perhaps some under the hood efficiency stuff) is that abstract classes follow traditional Java single-inheritance, whereas interfaces can have multiple-inheritance (or multiple