virtual-inheritance

How can I check the types of derived classes in C++?

拜拜、爱过 提交于 2020-03-03 08:25:40
问题 Let's say I have some base abstract class and three different classes that derive and implement its methods. Is there is a 'Type' object like as in C#? Or in other words, how do I get instances of all these classes? #ModuleBase.cpp class ModuleBase { }; #Module1.cpp class Module1 : public virtual ModuleBase { }; #Module2.cpp class Module2 : public virtual ModuleBase { }; #Module3.cpp class Module3 : public virtual ModuleBase { }; 回答1: You can create instanceof like methods that can detect the

Destructor in virtual inheritance

谁都会走 提交于 2020-01-20 08:41:27
问题 class Base{}; class D1:virtual public Base{}; class D2:virtual public Base{}; class DD:public D1,public D2{}; int main(){ Base *pBase=new DD; delete pBase; } This leads to crash, but I modify as below: class Base{ public: virtual ~Base(){}; }; class D1:virtual public Base{ public: virtual ~D1(){} }; class D2:virtual public Base{ public: virtual ~D2(){} }; class DD:public D1,public D2{ }; Then, it passes, but the default destructor should be the virtual dummy function, isn't it? 回答1: From the

C++ Virtual Inheritance Memory Layout

泪湿孤枕 提交于 2020-01-12 08:23:17
问题 Virtual Inheritance Memory Layouts I am trying to fully understand what is happening under the hood in the memory with virtual inheritance and vTables/vPtrs and what not. I have two examples of code I have written and I understand exactly why they work however I just want to make sure I have the right idea in my mind, of the object memory layouts. Here are the two examples in a picture, and I just wish to know if my idea of the memory layouts involved are correct. Example 1: class Top {

Pure Virtual Class and Collections (vector?)

谁说胖子不能爱 提交于 2020-01-11 04:52:07
问题 I'm working on a graphics application that is using virtual classes fairly extensively. It has: A picture class, which is essentially a collection of shapes. A shapes class, which is purely virtual and has a few classes that inherit from it: Circle Polygon Rectangle A Figure shape, which is any graphical figure (also virtual), shape inherits from this. Essentially, my problem comes down to implementing the picture class, which is basically being used to store a collection of shapes. I'm

Is there any extra cost of calling non-virtual base methods in virtual inheritance?

牧云@^-^@ 提交于 2020-01-03 02:41:12
问题 I had referred this question (I changed its title). I am aware that code generation related to virtual ness are implementation specific. However, earlier question suggests that, there is an additional cost related to virtual inheritance, when calling non-virtual base method. I wrote following test codes and checked its assembly in g++ (with -O4 ): Common part struct Base { int t_size; Base (int i) : t_size(i) {} virtual ~Base () {} int size () const { return t_size; }; }; struct D1 : virtual

Eliminate duplicate entries from C++11 variadic template arguments

99封情书 提交于 2019-12-29 03:40:31
问题 I'm using variadic templates with multiple virtual inheritance in C++ to aggregate types into a single structure definition. Here is a sample set of structures: struct meas { int i; }; struct meas2 : public virtual meas { int j; }; struct meas3 : public virtual meas { int k; }; I then aggregate these using multiple virtual inheritance: template <typename... Args> struct zipper : public virtual Args... {}; I can then do: typedef zipper<meas, meas2> meas_type; meas* m = new meas_type; These can

Multiple (diamond) inheritance compiles without “virtual”, but doesn't with

浪子不回头ぞ 提交于 2019-12-28 06:47:11
问题 Given the following code (without virtual inheritance) : class A { public: virtual void f() = 0; }; class B : public A { public: virtual void f() {} }; class C : public A { public: virtual void f() {} }; class D : public B, public C { /* some code */ }; int main() { D d; return 0; } the code compile. On the other hand , here : class A { public: virtual void f() = 0; }; class B : virtual public A { virtual void f() {} }; class C : virtual public A { virtual void f() {} }; class D : public B,

Multiple (diamond) inheritance compiles without “virtual”, but doesn't with

依然范特西╮ 提交于 2019-12-28 06:46:43
问题 Given the following code (without virtual inheritance) : class A { public: virtual void f() = 0; }; class B : public A { public: virtual void f() {} }; class C : public A { public: virtual void f() {} }; class D : public B, public C { /* some code */ }; int main() { D d; return 0; } the code compile. On the other hand , here : class A { public: virtual void f() = 0; }; class B : virtual public A { virtual void f() {} }; class C : virtual public A { virtual void f() {} }; class D : public B,

Why must virtual base classes be constructed by the most derived class?

本小妞迷上赌 提交于 2019-12-28 03:05:25
问题 The following code won't compile: class A { public: A(int) {} }; class B: virtual public A { public: B(): A(0) {} }; // most derived class class C: public B { public: C() {} // wrong!!! }; If I call A 's constructor in C 's constructor initialization list, that is: // most derived class class C: public B { public: C(): A(0) {} // OK!!! }; it does work. Apparently, the reason is because virtual base classes must always be constructed by the most derived classes . I don't understand the reason

Why must virtual base classes be constructed by the most derived class?

强颜欢笑 提交于 2019-12-28 03:05:22
问题 The following code won't compile: class A { public: A(int) {} }; class B: virtual public A { public: B(): A(0) {} }; // most derived class class C: public B { public: C() {} // wrong!!! }; If I call A 's constructor in C 's constructor initialization list, that is: // most derived class class C: public B { public: C(): A(0) {} // OK!!! }; it does work. Apparently, the reason is because virtual base classes must always be constructed by the most derived classes . I don't understand the reason