virtual-inheritance

Object layout in case of virtual functions and multiple inheritance

三世轮回 提交于 2019-11-26 12:57:01
问题 I was recently asked in an interview about object layout with virtual functions and multiple inheritance involved. I explained it in context of how it is implemented without multiple inheritance involved (i.e. how the compiler generated the virtual table, insert a secret pointer to the virtual table in each object and so on). It seemed to me that there was something missing in my explanation. So here are questions (see example below) What is the exact memory layout of the object of class C.

Order of constructor call in virtual inheritance

荒凉一梦 提交于 2019-11-26 11:28:54
问题 class A { int i; public: A() {cout<<\"in A\'s def const\\n\";}; A(int k) {cout<<\"In A const\\n\"; i = k; } }; class B : virtual public A { public: B(){cout<<\"in B\'s def const\\n\";}; B(int i) : A(i) {cout<<\"in B const\\n\";} }; class C : public B { public: C() {cout<<\"in C def cstr\\n\";} C(int i) : B(i) {cout<<\"in C const\\n\";} }; int main() { C c(2); return 0; } The output in this case is in A\'s def const in B const in C const Why is this not entering into in A const `It should

Why can&#39;t static_cast be used to down-cast when virtual inheritance is involved?

蹲街弑〆低调 提交于 2019-11-26 09:36:34
问题 Consider the following code: struct Base {}; struct Derived : public virtual Base {}; void f() { Base* b = new Derived; Derived* d = static_cast<Derived*>(b); } This is prohibited by the standard ( [n3290: 5.2.9/2] ) so the code does not compile, because Derived virtually inherits from Base . Removing the virtual from the inheritance makes the code valid. What\'s the technical reason for this rule to exist? 回答1: The technical problem is that there's no way to work out from a Base* what the

Virtual tables and memory layout in multiple virtual inheritance

限于喜欢 提交于 2019-11-26 08:25:09
问题 Consider following hierarchy: struct A { int a; A() { f(0); } A(int i) { f(i); } virtual void f(int i) { cout << i; } }; struct B1 : virtual A { int b1; B1(int i) : A(i) { f(i); } virtual void f(int i) { cout << i+10; } }; struct B2 : virtual A { int b2; B2(int i) : A(i) { f(i); } virtual void f(int i) { cout << i+20; } }; struct C : B1, virtual B2 { int c; C() : B1(6),B2(3),A(1){} virtual void f(int i) { cout << i+30; } }; What\'s the exact memory layout of C instance? How many vptrs it

Why is Default constructor called in virtual inheritance?

江枫思渺然 提交于 2019-11-26 03:07:28
问题 I don\'t understand why in the following code, when I instanciate an object of type daughter , the default grandmother() constructor is called ? I thought that either the grandmother(int) constructor should be called (to follow the specification of my mother class constructor), or this code shouldn\'t compile at all because of the virtual inheritance. Here compiler silently calls grandmother default constructor in my back, whereas I never asked for it. #include <iostream> class grandmother {

How does virtual inheritance solve the “diamond” (multiple inheritance) ambiguity?

瘦欲@ 提交于 2019-11-26 00:58:05
问题 class A { public: void eat(){ cout<<\"A\";} }; class B: virtual public A { public: void eat(){ cout<<\"B\";} }; class C: virtual public A { public: void eat(){ cout<<\"C\";} }; class D: public B,C { public: void eat(){ cout<<\"D\";} }; int main(){ A *a = new D(); a->eat(); } I understand the diamond problem, and above piece of code does not have that problem. How exactly does virtual inheritance solve the problem? What I understand: When I say A *a = new D(); , the compiler wants to know if

In C++, what is a virtual base class?

Deadly 提交于 2019-11-25 21:58:26
问题 I want to know what a \" virtual base class \" is and what it means. Let me show an example: class Foo { public: void DoSomething() { /* ... */ } }; class Bar : public virtual Foo { public: void DoSpecific() { /* ... */ } }; 回答1: Virtual base classes, used in virtual inheritance, is a way of preventing multiple "instances" of a given class appearing in an inheritance hierarchy when using multiple inheritance. Consider the following scenario: class A { public: void Foo() {} }; class B : public