virtual-inheritance

C++: is a class with virtual base but without virtual functions polymorphic and has VTable?

三世轮回 提交于 2019-12-07 03:13:09
问题 Consider the following code: #include <iostream> #include <typeinfo> #include <type_traits> using namespace std; struct A { int data; }; struct B1 : A {}; struct B2 : virtual A {}; struct Base1 : virtual A {}; struct Base2 : virtual A {}; struct Derived : Base1, Base2 {}; int main() { cout << sizeof(B1) << endl; cout << sizeof(B2) << endl; cout << sizeof(Derived) << endl; cout << std::is_polymorphic<B1>::value << endl; cout << std::is_polymorphic<B2>::value << endl; cout << std::is

Using CRTP with virtual inheritance

依然范特西╮ 提交于 2019-12-06 04:53:09
问题 I have a hierarchy of nodes, where "diamond" can occurred. Every node must be clonable but I don't want to write clone method to every node. So I use CRTP. class Node { public: Node(){} Node(Fill*) { } virtual ~Node() {} virtual Node * clone() const = 0; virtual void id() { std::cout << "Node\n"; } }; //==================================================================== template <typename Base, typename Derived> class NodeWrap : public Base { public: NodeWrap() { } NodeWrap(Fill * arg1) :

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

How to downcast from non-polymorphic virtual base class?

天大地大妈咪最大 提交于 2019-12-05 09:02:57
Is there a way to downcast from a virtual base class to a derived class when there are no virtual functions involved? Here's some code to demonstrate what I'm talking about: struct Base1 { int data; }; struct Base2 { char odd_size[9]; }; struct ViBase { double value; }; struct MostDerived : Base1, Base2, virtual ViBase { bool ok; }; void foo(ViBase &v) { MostDerived &md = somehow_cast<MostDerived&>(v); //but HOW? md.ok = true; } int main() { MostDerived md; foo(md); } Please note that the code is for demonstration only. My real scenario is fairly complex and involves template parameters and

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

How to call copy constructor of all base classes for copying most derived class object in diamond inheritance in C++?

我的梦境 提交于 2019-12-05 07:49:00
Consider the below code: #include<iostream> using namespace std; class A { public: A() {cout << "1";} A(const A &obj) {cout << "2";} }; class B: virtual A { public: B() {cout << "3";} B(const B & obj) {cout<< "4";} }; class C: virtual A { public: C() {cout << "5";} C(const C & obj) {cout << "6";} }; class D:B,C { public: D() {cout << "7";} D(const D & obj) {cout << "8";} }; int main() { D d1; cout << "\n"; D d(d1); } The output of the program is below: 1357 1358 So, for line D d(d1) the copy constructor of D class is bein called. During inheritance we need to explicitly call copy constructor

C++: is a class with virtual base but without virtual functions polymorphic and has VTable?

两盒软妹~` 提交于 2019-12-05 05:55:47
Consider the following code: #include <iostream> #include <typeinfo> #include <type_traits> using namespace std; struct A { int data; }; struct B1 : A {}; struct B2 : virtual A {}; struct Base1 : virtual A {}; struct Base2 : virtual A {}; struct Derived : Base1, Base2 {}; int main() { cout << sizeof(B1) << endl; cout << sizeof(B2) << endl; cout << sizeof(Derived) << endl; cout << std::is_polymorphic<B1>::value << endl; cout << std::is_polymorphic<B2>::value << endl; cout << std::is_polymorphic<Derived>::value << endl; return 0; } On my system it prints 4 8 12 0 0 0 Which means that none of

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

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

Memory layout of a class under multiple or virtual inheritance and the vtable(s)?

我只是一个虾纸丫 提交于 2019-12-04 07:53:28
问题 I am reading "Inside the C++ Object Model", trying to understand how multiple and virtual inheritance is achieved via the vtables.(I understand single polymorphism perfectly-well). I am having difficulties understand what exactly is done when a method needs to be located during virtual inheritance, or during casting, because there is a lot of offset calculation to be performed. Would somebody be able to help with understanding how the multiple vtables are used in a multiple or virtual