virtual-inheritance

c++ virtual inheritance

孤街醉人 提交于 2019-12-16 21:01:33
问题 Problem: class Base { public: Base(Base* pParent); /* implements basic stuff */ }; class A : virtual public Base { public: A(A* pParent) : Base(pParent) {} /* ... */ }; class B : virtual public Base { public: B(B* pParent) : Base(pParent) {} /* ... */ }; class C : public A, public B { public: C(C* pParent) : A(pParent), B(pParent) {} // - Compilation error here /* ... */ }; At the position given, gcc complains that it cannot match function call to Base(), i.e. the default constructor. But C

c++ virtual inheritance

雨燕双飞 提交于 2019-12-16 21:01:24
问题 Problem: class Base { public: Base(Base* pParent); /* implements basic stuff */ }; class A : virtual public Base { public: A(A* pParent) : Base(pParent) {} /* ... */ }; class B : virtual public Base { public: B(B* pParent) : Base(pParent) {} /* ... */ }; class C : public A, public B { public: C(C* pParent) : A(pParent), B(pParent) {} // - Compilation error here /* ... */ }; At the position given, gcc complains that it cannot match function call to Base(), i.e. the default constructor. But C

Diamond problem with only 1 virtual inheritance

淺唱寂寞╮ 提交于 2019-12-12 15:04:00
问题 Does this still solve the diamond problem? class A {}; class B : virtual A {}; class C : A {}; class D : B, C {}; Edit: If not, what is it then? Is it the same as this? class A {}; class B : A {}; class C : A {}; class D : B, C {}; Or is it something even else? 回答1: Diamond problem and ambiguous call to common base members can be best described through the following pictorial equivalent which also give an insight of the memory model Example 1 class A {void foo(){};}; class B :public A {};

Does virtual inheritance increase the size of derived class? [duplicate]

僤鯓⒐⒋嵵緔 提交于 2019-12-12 12:35:49
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: object size with virtual Does virtual inheritance change the size of the derived class? I executed the following code, where I have two derived classes one virtually inherited and the other non virtually inherited: class A { public: int a; virtual void a1(); }; class non_vir_der: public A{ public: int c; virtual void aa(); }; class vir_der: public virtual A{ public: int d; virtual void bb(); }; int main() { cout

Misaligned address using virtual inheritance

女生的网名这么多〃 提交于 2019-12-12 07:15:42
问题 The following apparently valid code produces a misaligned address runtime error using the UndefinedBehaviorSanitizer sanitiser. #include <memory> #include <functional> struct A{ std::function<void()> data; // seems to occur only if data is a std::function } ; struct B{ char data; // occurs only if B contains a member variable }; struct C:public virtual A,public B{ }; struct D:public virtual C{ }; void test(){ std::make_shared<D>(); } int main(){ test(); return 0; } Compiling and executing on

call overwritten child function within parent function

喜你入骨 提交于 2019-12-11 14:14:47
问题 is it possible in c++ to call a child function from a parent function. Let's take an example: The parent class defines in a function (parse) the general workflow. The workflow then calls different methods which represent part of the flow (parseElementA). These functions can be overwritten by the child class, if not the standart function, which is part of the parent shall be used. My issue is: I create a child object and execute the workflow function (parse). When the overwritten function

about virtual base class and virtual inheritance in C++ [duplicate]

隐身守侯 提交于 2019-12-11 05:25:42
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: gcc c++ virtual inheritance problem Hi All, I am reading Effective C++ by scott myers books. It was mentioned about virtual base class and virtual inheritance as follows. The rules governing the initialization of virtual base classes are more complicated and less intuitive than are those for non-virtual bases. The responsobility for initializing a virtual base is borne by the most derived class in the hierarchy.

Why sizeof(Derived4) is 8 byte? I think it should be 5 bytes

一笑奈何 提交于 2019-12-10 20:14:26
问题 This is the output of the given program: sizeof(Empty) 1 sizeof(Derived1) 1 sizeof(Derived2) 4 sizeof(Derived3) 1 sizeof(Derived4) 8 sizeof(Dummy) 1 This is the program: #include <iostream> using namespace std; class Empty {}; class Derived1 : public Empty {}; class Derived2 : virtual public Empty {}; class Derived3 : public Empty { char c; }; class Derived4 : virtual public Empty { char c; }; class Dummy { char c; }; int main() { cout << "sizeof(Empty) " << sizeof(Empty) << endl; cout <<

Virtual class inheritance object size issue

扶醉桌前 提交于 2019-12-10 19:05:41
问题 Here, in this code, the size of ob1 is 16 which is fine(because of the virtual pointer) but I can't understand why the size of ob2 is 24. #include <iostream> using namespace std; class A { int x; }; class B { int y, z; }; class C : virtual public A { int a; }; class D : virtual public B { int b; }; int main() { C ob1; D ob2; cout << sizeof(ob1) << sizeof(ob2) << "\n"; } I expect the size of ob2 as 20, but the output is 24 回答1: One possible layout for objects of type D is: +----------+ | y |

Does “virtual base class in the case of multilevel inheritance” have significance

痞子三分冷 提交于 2019-12-10 16:32:33
问题 Consider the following sample codes which shows multilevel inheritance: Case1 : Here the class derived1 is derived from the class base through virtual inheritance and the class derived2 is derived from the class derived1 directly. class base { }; class derived1 : virtual public base { }; class derived2 : public derived1 { }; Case2 : Same as Case1 except that no virtual inheritance is involved class base { }; class derived1 : public base // no virtual inheritance { }; class derived2 : public