diamond-problem

What is the proper approach to swap and copy idiom in virtual inheritance?

妖精的绣舞 提交于 2019-12-07 07:32:41
问题 Consider classic virtual inheritance diamond hierarchy. I wonder to know what is the right implementation of copy and swap idiom in such hierarchy. The example is a little artificial - and it is not very smart - as it would play good with default copy semantic for A,B,D classes. But just to illustrate the problem - please forget about the example weaknesses and provide the solution. So I have class D derived from 2 base classes (B<1>,B<2>) - each of B classes inherits virtually from A class.

g++ “because the following virtual functions are pure” with abstract base class

我的未来我决定 提交于 2019-12-06 01:16:36
问题 Here is my example code which produces the error: struct Impl { int data_size_; int find(int var){return 0;} int get(int rowid){return 0;} }; class Container { public: Container() {} virtual ~Container() {} virtual int get_size() = 0; virtual int get(int rowid) = 0; }; class SortedContainer : virtual public Container { public: virtual int find(int var) = 0; }; class ContainerImpl : public Container { protected: Impl impl_; public: int get_size() {return impl_.data_size_;} int get(int rowid)

Overcoming diamond ambiguity in different way

江枫思渺然 提交于 2019-12-05 17:59:41
I know the diamond problem and method to solve it using virtual base class. I tried to solve diamond problem in a different way but did not succeed. I don't know why. #include <iostream> using namespace std; class A { public: void display() { cout << "successfully printed"; } }; class B: public A { }; class C: private A // display() of A will become private member of C { }; class D: public B, public C // private member display() of C should not be inherited { }; int main() { D d; d.display(); return 0; } As the private members are not inherited the class D will not inherit any function from C,

What is the proper approach to swap and copy idiom in virtual inheritance?

瘦欲@ 提交于 2019-12-05 16:26:49
Consider classic virtual inheritance diamond hierarchy. I wonder to know what is the right implementation of copy and swap idiom in such hierarchy. The example is a little artificial - and it is not very smart - as it would play good with default copy semantic for A,B,D classes. But just to illustrate the problem - please forget about the example weaknesses and provide the solution. So I have class D derived from 2 base classes (B<1>,B<2>) - each of B classes inherits virtually from A class. Each class has non trivial copy semantics with using of copy and swap idiom. The most derived D class

Fixing C++ Multiple Inheritance Ambiguous Call

穿精又带淫゛_ 提交于 2019-12-05 07:58:41
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 main() { AddressType *a = new AddressType(); a->GetValue(); return 0; } I am getting the following: In

Java: how do you call this multiple inheritance ambiguity?

安稳与你 提交于 2019-12-04 19:17:30
问题 Here's an example using multiple interface inheritance in Java and there's an issue. Note that I fully know why there's an issue and this is not the point of my question. The question is about how you name this particular multiple interface inheritance ambiguity, if there's a name for it. For example, in C++, the ambiguity that arises when you use multiple implementation inheritance and cannot determine which overridden method to use is called the "diamond problem": http://en.wikipedia.org

Virtual Inheritance and dreaded diamond

家住魔仙堡 提交于 2019-12-04 18:35:18
问题 I am having a hard time with a dreaded diamond problem. For a reminder, here is the classical class hierarchy of this problem: B / \ C1 C2 \ / D To solve it, the standard solution is to make C1 and C2 use virtual inheritance to inherit from B. My problem is that B and C1 are from an SDK that I cannot modify. Example below where I cannot make SubClassB inherit virtually from Base . Classes: PureVirtualBase, Base and SubClassB are from the SDK I use. I cannot modify them. SubClassA and Leaf are

Multiple inheritance without virtual functions in c++

左心房为你撑大大i 提交于 2019-12-04 17:56:48
问题 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

Inheritance by dominance - is it really bad?

喜你入骨 提交于 2019-12-04 15:56:50
问题 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

Downcast in a diamond hierarchy

99封情书 提交于 2019-12-04 15:31:23
问题 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