virtual-inheritance

Class sizes with virtual inheritance in C++

混江龙づ霸主 提交于 2019-11-30 14:15:28
#include<iostream> using namespace std; class abc { int a; }; class xyz : public virtual abc { int b; }; int main() { abc obj; xyz obj1; cout<<endl<<sizeof(obj); cout<<endl<<sizeof(obj1); return 0; } The answers would be compiler dependent but I'm surprized when I saw this as the result ~/Documents/workspace/tmp ‹.rvm-› $ ./class_sizes 4 16 If I remove the virtual keyword then the size allocated is 4 and 8 respectively which is what I expected. Why is the extra space being taken up exactly? I suspect it is for the vptr table or something of that sorts but don't know for certain. A good article

How C++ virtual inheritance is implemented in compilers?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-30 11:45:46
问题 How the compilers implement the virtual inheritance? In the following code: class A { public: A(int) {} }; class B : public virtual A { public: B() : A(1) {} }; class C : public B { public: C() : A(3), B() {} }; Does a compiler generate two instance of B::ctor function, one without A(1) call, and one with it? So when B::constructor is called from derived class's constructor the first instance is used, otherwise the second. 回答1: It's implementation-dependent. GCC (see this question), for

C++ Multiple Virtual Inheritance vs. COM

不问归期 提交于 2019-11-30 09:50:54
The net is overflowing with explanations of the "dreaded diamond problem" . So is StackOverflow. I think I understand that bit, but I fail to translate that knowledge into comprehending something similar yet different. My question begins as a pure C++ question, but the answer might well branch over into MS-COM specifics. The general problem question goes: class Base { /* pure virtual stuff */ }; class Der1 : Base /* Non-virtual! */ { /* pure virtual stuff */ }; class Der2 : Base /* Non-virtual! */ { /* pure virtual stuff */ }; class Join : virtual Der1, virtual Der2 { /* implementation stuff *

final class in c++

我的梦境 提交于 2019-11-30 08:40:30
class Temp { private: ~Temp() {} friend class Final; }; class Final : virtual public Temp { public: void fun() { cout<<"In base"; } }; class Derived : public Final { }; void main() { Derived obj; obj.fun(); } The above code tries to achieve non-inheritable class (final). But using above code the object of derived can still be created, why? The desired functionality is achieved only if ctor made private, my question is why it is not achievable in case of dtor private? Well, for this program (pleasse provide correct, compilable examples) #include <iostream> class Temp { private: ~Temp() {}

Destructor in virtual inheritance

天大地大妈咪最大 提交于 2019-11-29 16:37:12
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? From the C++11 specification (ISO/IEC 14882:2011(E)), section 12.4 Destructors [class.dtor]: Sub-section 4: If a

C++ Multiple Virtual Inheritance vs. COM

家住魔仙堡 提交于 2019-11-29 14:50:03
问题 The net is overflowing with explanations of the "dreaded diamond problem". So is StackOverflow. I think I understand that bit, but I fail to translate that knowledge into comprehending something similar yet different. My question begins as a pure C++ question, but the answer might well branch over into MS-COM specifics. The general problem question goes: class Base { /* pure virtual stuff */ }; class Der1 : Base /* Non-virtual! */ { /* pure virtual stuff */ }; class Der2 : Base /* Non-virtual

final class in c++

爱⌒轻易说出口 提交于 2019-11-29 12:08:42
问题 class Temp { private: ~Temp() {} friend class Final; }; class Final : virtual public Temp { public: void fun() { cout<<"In base"; } }; class Derived : public Final { }; void main() { Derived obj; obj.fun(); } The above code tries to achieve non-inheritable class (final). But using above code the object of derived can still be created, why? The desired functionality is achieved only if ctor made private, my question is why it is not achievable in case of dtor private? 回答1: Well, for this

C++ abstract base class constructors/destructors - general correctness

我的梦境 提交于 2019-11-29 11:40:00
问题 Recently I have dumb as a developer, so I took the plunge, got a C++ book and learning how to do things properly. In my head, I know what I would like to do. I effectively want an Interface that when inherited, must be overridden (if this is possible?). So far, I have the following: class ICommand{ public: // Virtual constructor. Needs to take a name as parameter //virtual ICommand(char*) =0; // Virtual destructor, prevents memory leaks by forcing clean up on derived classes? //virtual

Is there any penalty/cost of virtual inheritance in C++, when calling non-virtual base method?

核能气质少年 提交于 2019-11-29 09:07:15
Does using virtual inheritance in C++ have a runtime penalty in compiled code, when we call a regular function member from its base class? Sample code: class A { public: void foo(void) {} }; class B : virtual public A {}; class C : virtual public A {}; class D : public B, public C {}; // ... D bar; bar.foo (); There may be, yes, if you call the member function via a pointer or reference and the compiler can't determine with absolute certainty what type of object that pointer or reference points or refers to. For example, consider: void f(B* p) { p->foo(); } void g() { D bar; f(&bar); }

Virtual multiple inheritance - final overrider

佐手、 提交于 2019-11-29 08:46:28
while trying to analyse in greater depth inheritance mechanism of C++ I stumbled upon the following example: #include<iostream> using namespace std; class Base { public: virtual void f(){ cout << "Base.f" << endl; } }; class Left : public virtual Base { }; class Right : public virtual Base{ public: virtual void f(){ cout << "Right.f" << endl; } }; class Bottom : public Left, public Right{ }; int main(int argc,char **argv) { Bottom* b = new Bottom(); b->f(); } The above, somehow, compiles and calls Right::f(). I see what might be going on in the compiler, that it understands that there is one