virtual-inheritance

why are virtual base non-default constructors not called unless most-derived base explicitly invokes them?

倖福魔咒の 提交于 2019-12-04 03:33:09
问题 I would like to understand WHY C++ standard mandates that virtual base non-default constructors cannot be invoked by an intermediate NOT most-derived class, as in this code, when compiled with '-D_WITH_BUG_' : /* A virtual base's non-default constructor is NOT called UNLESS * the MOST DERIVED class explicitly invokes it */ #include <type_traits> #include <string> #include <iostream> class A { public: int _a; A(): _a(1) { std::cerr << "A() - me: " << ((void*)this) << std::endl; } A(int a): _a

Why does virtual inheritance need to be specified in the middle of a diamond hierarchy?

夙愿已清 提交于 2019-12-04 01:44:37
I have diamond hierarchy of classes: A / \ B C \ / D To avoid two copies of A in D, we need to use virtual inheritance at B and C. class A { }; class B: virtual public A {}; class C: virtual public A { }; class D: public B, public C { }; Question: Why does virtual inheritance needs to be performed at B and C, even though the ambiguity is at D? It would have been more intuitive if it is at D. Why is this feature designed like this by standards committee? What can we do if B and C classes are coming from 3rd party library ? EDIT: My answer was to indicate B and C classes that they should not

Virtual inheritance and empty vtable in base class

雨燕双飞 提交于 2019-12-03 20:29:58
There is this code: #include <iostream> class Base { int x; }; class Derived : virtual public Base { int y; }; int main() { std::cout << sizeof(Derived) << std::endl; // prints 12 return 0; } I have read that when some class is virtually inherited then there is created empty vtable for class Derived, so memory layout is as follows: Derived::ptr to empty vtable Derived::y Base::x and it is 12 bytes. The question is - what is purpose of this empty vtable if there are not any virtual methods and how is it used? Derived needs some way to know where the Base subobject is. With virtual inheritance,

Equivalent of Java interfaces in C++? [duplicate]

不羁的心 提交于 2019-12-03 19:48:02
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: How do you declare an interface in C++? Interface as in java in c++? I am a Java programmer learning C++, and I was wondering if there is something like Java interfaces in C++, i.e. classes that another class can implement/extend more than one of. Thanks. p.s. New here so tell me if I did anything wrong. 回答1: In C++ a class containing only pure virtual methods denotes an interface. Example: // Define the

Inherit from multiple partial implementations of an abstract base class?

半腔热情 提交于 2019-12-03 16:23:25
问题 Is it possible to have a number of partial implementations of an abstract interface, and then collect these partial implementations into a single concrete class by using multiple inheritence? I have the following example code: #include <iostream> struct Base { virtual void F1() = 0; virtual void F2() = 0; }; struct D1 : Base { void F1() override { std::cout << __func__ << std::endl; } }; struct D2 : Base { void F2() override { std::cout << __func__ << std::endl; } }; // collection of the two

C++ Virtual Inheritance Memory Layout

☆樱花仙子☆ 提交于 2019-12-03 15:20:59
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 { public: int a; }; class Left : public virtual Top { public: int b; }; class Right : public virtual Top {

Static Virtual functions in c++

末鹿安然 提交于 2019-12-03 11:07:51
I have a base class and a derived one and I want to change base functions while keeping them static as they should be passed to other functions as static. How can I do that? The ATL framework gets around the limitation of no virtual statics by making the base class be a template, and then having derived classes pass their class type as a template parameter. The base class can then call derived class statics when needed, eg: template< class DerivedType > class Base { public: static void DoSomething() { DerivedType::DoSomethingElse(); } }; class Derived1 : public Base<Derived1> { public: static

Downcast in a diamond hierarchy

我只是一个虾纸丫 提交于 2019-12-03 10:39:42
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 ordered though. The following article gives some good examples for gcc: http://www.phpcompiler.org

Virtual tables and virtual pointers for multiple virtual inheritance and type casting

雨燕双飞 提交于 2019-12-03 10:29:30
问题 I am little confused about vptr and representation of objects in the memory, and hope you can help me understand the matter better. Consider B inherits from A and both define virtual functions f() . From what I learned the representation of an object of class B in the memory looks like this: [ vptr | A | B ] and the vtbl that vptr points to contains B::f() . I also understood that casting the object from B to A does nothing except ignoring the B part at the end of the object. Is it true?

Inherit from multiple partial implementations of an abstract base class?

拟墨画扇 提交于 2019-12-03 05:44:54
Is it possible to have a number of partial implementations of an abstract interface, and then collect these partial implementations into a single concrete class by using multiple inheritence? I have the following example code: #include <iostream> struct Base { virtual void F1() = 0; virtual void F2() = 0; }; struct D1 : Base { void F1() override { std::cout << __func__ << std::endl; } }; struct D2 : Base { void F2() override { std::cout << __func__ << std::endl; } }; // collection of the two partial implementations to form the concrete implementation struct Deriv : D1, D2 { using D1::F1; // I