diamond-problem

python multiple inheritance: avoid calling the constructors twice in diamond shape

三世轮回 提交于 2019-12-24 18:12:44
问题 Consider the following code: class A(object): def __init__(self): print("A.__init__") super(A, self).__init__() # 1 print("A.__init__ finished") class B(A): def __init__(self): print("B.__init__") super(B, self).__init__() # 2 print("B.__init__ finished") class C(A): def __init__(self): print("C.__init__") super(C, self).__init__() print("C.__init__ finished") class D(B, C): def __init__(self): print("D.__init__") print("Initializing B") B.__init__(self) # 3 print("B initialized") print(

C++ multiple inheritance

孤者浪人 提交于 2019-12-23 04:25:04
问题 Please don't question the really odd hierarchy of workers in this code here, I have no idea why anyone would want something like this, but I decided to give myself an exercise in Multiple Inheritance, just to be sure I fully understood it. So here's the result. using namespace std; class Employee { protected: string name; public: string getname() { return name; } void setname(string name2) { name = name2; } Employee(string name2) { name = name2; } Employee(){} }; class Manager : public

Avoid multiple inheritance induced ambiguity by using scope resolution

两盒软妹~` 提交于 2019-12-22 08:13:06
问题 Here is an example of multiple inheritance. I used the scope resolution operator to resolve the ambiguity instead of a virtual class. struct A { int i; }; struct B : A {}; struct C : A {}; struct D: B, C { void f() { B::i = 10; } void g() { std::cout << B::i <<std::endl; } }; int main() { D d1; d1.f(); d1.g(); return 0; } Is B::i well-formed? 回答1: Is B::i well-formed? Yes, it is. The most pertinent reference is [class.qual]/1: If the nested-name-specifier of a qualified-id nominates a class,

Fixing C++ Multiple Inheritance Ambiguous Call

試著忘記壹切 提交于 2019-12-22 05:38:28
问题 I have three classes structured like this: #include <iostream> using namespace std; class Keyword { public: virtual float GetValue() = 0; }; class CharacterKeyword : public Keyword { public: virtual float GetValue(){return _value;} private: float _value; }; class MeasurementKeyword : public Keyword { public: virtual float GetValue(){return _value;} private: float _value; }; class AddressType : public CharacterKeyword, public MeasurementKeyword { private: float address; float addresExt; }; int

Why is single virtual inheritance not enough to resolve the dreaded diamond problem?

只谈情不闲聊 提交于 2019-12-21 17:45:14
问题 struct B { int i; }; struct D1 : virtual B {}; struct D2 : B {}; // <-- not virtual struct DD : D1, D2 {}; Having coded above, still the compiler demands D2 also to be virtual : DD d; d.i = 0; // error: request for member `i' is ambiguous What I don't understand is, once you have prompted compiler that B is virtual with respect to DD (via D1 ) then why it still i is ambiguous ? (If my memory serves correct, the older VC++ (in 2006), was capable enough to make out this just with single virtual

What is multiple re-inheritance?

≯℡__Kan透↙ 提交于 2019-12-21 03:33:14
问题 I refer to the following as “multiple re-inheritance”: inheriting a class once directly and one or more times indirectly by inheriting one or more of its descendants inheriting a class indirectly two or more times by inheriting two or more of its descendants I want to know if it exists and how to unambiguously access embedded subobjects. 1.) [ Professional C++ , 2 nd ed.] † states a compilable program can't have a class that directly inherits both its immediate parent and said parent's parent

How to implement interfaces with homographic methods in Java?

家住魔仙堡 提交于 2019-12-20 12:09:25
问题 In English, a homograph pair is two words that have the same spelling but different meanings. In software engineering, a pair of homographic methods is two methods with the same name but different requirements. Let's see a contrived example to make the question as clear as possible: interface I1 { /** return 1 */ int f() } interface I2 { /** return 2*/ int f() } interface I12 extends I1, I2 {} How can I implement I12 ? C# has a way to do this, but Java doesn't. So the only way around is a

Diamond of death and Scope resolution operator (c++)

荒凉一梦 提交于 2019-12-19 05:16:08
问题 I have this code (diamond problem): #include <iostream> using namespace std; struct Top { void print() { cout << "Top::print()" << endl; } }; struct Right : Top { void print() { cout << "Right::print()" << endl; } }; struct Left : Top { void print() { cout << "Left::print()" << endl; } }; struct Bottom: Right, Left{}; int main() { Bottom b; b.Right::Top::print(); } I want to call print() in Top class. When I try to compile it I get error: 'Top' is an ambiguous base of 'Bottom' on this line: b

Multiple inheritance + virtual function mess

此生再无相见时 提交于 2019-12-17 10:45:30
问题 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

Diamond Problem

折月煮酒 提交于 2019-12-17 07:25:26
问题 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