diamond-problem

C++ Resolving the diamond problem

喜欢而已 提交于 2019-12-02 04:56:04
问题 Couldn't the diamond problem be resolved just by using the first inherited declaration found? I mean, public class A { public virtual int getInt(); }; public class B : public A { public int getInt() {return 6;} }; public class C : public A { public int getInt() {return 7;} }; public class D: public B, public C {}; for class D , since B is listed first, couldn't we just by default (when it's ambiguous) use B::getInt() if D::getInt() is called? Kind of how the PATH environment variable works in

Triads not showing up to fight? (Java Set missing an item)

◇◆丶佛笑我妖孽 提交于 2019-12-02 03:27:43
问题 I have code from two companies asoft and bsoft. I cannot change either. This is a simplified version of my situation which I'm pretty sure has enough information to the find what's causing the problem. bsoft provides IGang , which represents a gang that can battle other gangs. package bsoft; public interface IGang { /** @return negative, 0, or positive, respectively * if this gang is weaker than, equal to, or stronger * than the other */ public int compareTo(IGang g); public int getStrength()

C++ Resolving the diamond problem

雨燕双飞 提交于 2019-12-02 02:41:18
Couldn't the diamond problem be resolved just by using the first inherited declaration found? I mean, public class A { public virtual int getInt(); }; public class B : public A { public int getInt() {return 6;} }; public class C : public A { public int getInt() {return 7;} }; public class D: public B, public C {}; for class D , since B is listed first, couldn't we just by default (when it's ambiguous) use B::getInt() if D::getInt() is called? Kind of how the PATH environment variable works in UNIX and other OS's; if two things exist with the same name in different locations in the PATH

Multiple inheritance and pure virtual functions

百般思念 提交于 2019-12-01 02:29:06
The following code: struct interface_base { virtual void foo() = 0; }; struct interface : public interface_base { virtual void bar() = 0; }; struct implementation_base : public interface_base { void foo(); }; struct implementation : public implementation_base, public interface { void bar(); }; int main() { implementation x; } fails to compile with the following errors: test.cpp: In function 'int main()': test.cpp:23:20: error: cannot declare variable 'x' to be of abstract type 'implementation' test.cpp:16:8: note: because the following virtual functions are pure within 'implementation': test

C++ Multiple Inheritance - why you no work?

邮差的信 提交于 2019-12-01 00:33:32
问题 I am trying to figure out an interesting multiple inheritance issue. The grandparent is an interface class with multiple methods: class A { public: virtual int foo() = 0; virtual int bar() = 0; }; Then there are abstract classes that are partially completing this interface. class B : public A { public: int foo() { return 0;} }; class C : public A { public: int bar() { return 1;} }; The class I want to use inherits from both of the parents and specifies what method should come from where via

Multiple inheritance and pure virtual functions

自作多情 提交于 2019-11-30 22:05:13
问题 The following code: struct interface_base { virtual void foo() = 0; }; struct interface : public interface_base { virtual void bar() = 0; }; struct implementation_base : public interface_base { void foo(); }; struct implementation : public implementation_base, public interface { void bar(); }; int main() { implementation x; } fails to compile with the following errors: test.cpp: In function 'int main()': test.cpp:23:20: error: cannot declare variable 'x' to be of abstract type 'implementation

python multiple inheritance passing arguments to constructors using super

旧街凉风 提交于 2019-11-30 10:54:50
问题 Consider the following snippet of python code class A(object): def __init__(self, a): self.a = a class B(A): def __init__(self, a, b): super(B, self).__init__(a) self.b = b class C(A): def __init__(self, a, c): super(C, self).__init__(a) self.c = c class D(B, C): def __init__(self, a, b, c, d): #super(D,self).__init__(a, b, c) ??? self.d = d I am wondering how can I pass a , b and c to corresponding base classes' constructors. 回答1: Well, when dealing with multiple inheritance in general, your

Consequences of changing inheritance to virtual?

[亡魂溺海] 提交于 2019-11-30 09:05:47
I'm working on a huge project that I didn't start. My task is to add some additional functionality to what already is there. I'm in a situation where I have to use virtual inheritance because I have a diamond model. The situation is depicted in the following illustration: Base class / \ / \ My new class A class that was there before (OldClass) \ / \ / \ / \ / My other new class For this to work, both the classes in the middle have to inherit from the base through public virtual instead of just public . So: class OldClass: public BaseClass {} has to become: class OldClass: public virtual

C++ Inheritance via dominance warning

孤者浪人 提交于 2019-11-28 18:10:29
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 this entire tree only D implements virtual methods, there is no other definition of the method in question

C++ virtual override functions with same name

末鹿安然 提交于 2019-11-27 15:00: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? (visual c++ 2008) class Impl : public A , public B { public: void A::Function () { cout << "A::Function" <