diamond-problem

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

烂漫一生 提交于 2019-12-04 06:00:40
问题 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 , such that only one of A , or B , can be called at a time, not both (which is what is needed). To get around this, this is used: class Last: public A, public B { private: std::unique_ptr<Base> m_p; public: Last(int i) { if (i) m_p = std::unique_ptr

Why is the diamond case with its common ancestor used to explain Java multiple inheritance issue, instead of two unrelated parent classes?

北慕城南 提交于 2019-12-03 23:34:11
This question might sound weird to Java people but if you try to explain this, it would be great. In these days I am clearing some of Java's very basic concept. So I come to Inheritance and Interface topic of Java. While reading this I found that Java does not support Multiple Inheritance and also understood that, what I am not able to understand that why everywhere Diamond figure issue(At least 4 class to create diamond) is discussed to explain this behavior, Can't we understand this issue using 3 classes only. Say, I have class A and class B, these two classes are different (they are not

Multiple inheritance without virtual functions in c++

旧时模样 提交于 2019-12-03 11:39:23
I came across the diamond problem and found different solutions for different cases with a single diamond. However I couldn't find a solution for 'chained' diamonds. According to the structure: yes, I want to have multiple baseclasses everytime, so virtual inheritance isn't a solution (is it even called diamond then?). I also wanted to avoid get/set-functions for every middle-layer of a diamond. p p | | k k \ / s class parent { int val; }; class kid1 : public parent {}; class kid2 : public parent {}; class school : public kid1, public kid2 {}; Accessing val in the parent class works now like

What is multiple re-inheritance?

人走茶凉 提交于 2019-12-03 10:43:52
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 class. Is it true? Given a GrandParent and Parent , which extends GrandParent , VC12 and g++ allows a

Downcast in a diamond hierarchy

我只是一个虾纸丫 提交于 2019-12-03 10:39:42
Why static_cast cannot downcast from a virtual base ? struct A {}; struct B : public virtual A {}; struct C : public virtual A {}; struct D : public B, public C {}; int main() { D d; A& a = d; D* p = static_cast<D*>(&a); //error } g++ 4.5 says: error: cannot convert from base ‘A’ to derived type ‘D’ via virtual base ‘A’ The solution is to use dynamic_cast ? but why. What is the rational ? -- edit -- Very good answers below. No answers detail exactly how sub objects and vtables end up to be ordered though. The following article gives some good examples for gcc: http://www.phpcompiler.org

Inheritance by dominance - is it really bad?

蓝咒 提交于 2019-12-03 09:58:40
I'm one of those people that has to get their code to compile with 0 warnings. Normally I respect the compiler and if it issues me a warning I take it as a sign that I should touch up my code a little. If I have to tell a compiler to ignore a given warning, I twitch a little. But this one I can't seem to get around, and from what I can tell I haven't done anything "bad". Does anyone think that this is a poor design? I can't see anything particularly nasty about it (except for the "evil diamond") but it's perfectly valid and useful code. But it generates (in MSVC) a level 2 warning! class IFoo

Multiple Inheritance Ambiguity with Interface

瘦欲@ 提交于 2019-12-03 04:52:37
问题 We all know about the diamond problem regarding multiple inheritance - A / \ B C \ / D This problem describe an ambiguous situation for class D . If class A has a method and both/either of B and/or C override the method then which version of method does D override? Is this problem also applicable for interfaces in Java? If not, how do Java interfaces overcome this problem? 回答1: The diamond problem only applies to implementation inheritance ( extends in all versions of Java prior to Java 8).

How to implement interfaces with homographic methods in Java?

只谈情不闲聊 提交于 2019-12-03 02:12:42
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 hack. How can it be done with reflection/bytecode tricks/etc most reliably (i.e it doesn't have to be a

Multiple Inheritance Ambiguity with Interface

回眸只為那壹抹淺笑 提交于 2019-12-02 18:13:16
We all know about the diamond problem regarding multiple inheritance - A / \ B C \ / D This problem describe an ambiguous situation for class D . If class A has a method and both/either of B and/or C override the method then which version of method does D override? Is this problem also applicable for interfaces in Java? If not, how do Java interfaces overcome this problem? The diamond problem only applies to implementation inheritance ( extends in all versions of Java prior to Java 8). It doesn't apply to API inheritance ( implements in all versions of Java prior to Java 8). Since interface

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

给你一囗甜甜゛ 提交于 2019-12-02 12:17:46
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 , such that only one of A , or B , can be called at a time, not both (which is what is needed). To get around this, this is used: class Last: public A, public B { private: std::unique_ptr<Base> m_p; public: Last(int i) { if (i) m_p = std::unique_ptr<Base>(new A()); else m_p = std::unique_ptr<Base>(new B()); } }; This is fine, but now m_p->m_x cannot