virtual-inheritance

Object layout in case of virtual functions and multiple inheritance

久未见 提交于 2019-11-27 06:50:55
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. Virtual tables entries for class C. Sizes (as returned by sizeof) of object of classes A, B and C. (8, 8

When virtual inheritance IS a good design?

亡梦爱人 提交于 2019-11-27 06:45:31
EDIT3: Please be sure to clearly understand what I am asking before answering (there are EDIT2 and lots of comments around). There are (or were) many answers which clearly show misunderstanding of the question (I know that's also my fault, sorry for that) Hi, I've looked over the questions on virtual inheritance ( class B: public virtual A {...} ) in C++, but did not find an answer to my question. I know that there are some issues with virtual inheritance, but what I'd like to know is in which cases virtual inheritance would be considered a good design. I saw people mentioning interfaces like

What is the VTT for a class?

最后都变了- 提交于 2019-11-27 06:07:52
Recently ran across a C++ linker error that was new to me. libfoo.so: undefined reference to `VTT for Foo' libfoo.so: undefined reference to `vtable for Foo' I recognized the error and fixed my problem, but I still have a nagging question: what exactly is a VTT? Aside: For those interested, the problem occurs when you forget to define the first virtual function declared in a class. The vtable goes into the compilation unit of the class's first virtual function. If you forget to define that function, you get a linker error that it can't find the vtable rather than the much more developer

Order of constructor call in virtual inheritance

こ雲淡風輕ζ 提交于 2019-11-27 05:14:46
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 follow the order of 1 arg constructor call. But what actually is happening on deriving B from A using virtual keyword. There are

C++ cannot convert from base A to derived type B via virtual base A

笑着哭i 提交于 2019-11-27 04:14:19
问题 I have three classes: class A {}; class B : virtual public A {}; class C : virtual public A {}; class D: public B, public C {}; Attempting a static cast from A* to B* I get the below error: cannot convert from base A to derived type B via virtual base A 回答1: In order to understand the cast system, you need to dive into the object model. The classic representation of a simple hierarchy model is containment: if B derives from A then the B object will, in fact, contain an A subobject alongside

In C++, should I almost always use virtual inheritance?

牧云@^-^@ 提交于 2019-11-27 03:04:57
问题 I see from this entry that virtual inheritance adds sizeof(pointer) to an object's memory footprint. Other than that, are there any drawbacks to me just using virtual inheritance by default, and conventional inheritance only when needed? It seems like it'd lead to more future-proof class design, but maybe I'm missing some pitfall. 回答1: The drawbacks are that All classes will have to initialize all its virtual bases all the time (e.g. if A is virtual base of B, and C derives from B, it also

Mixing virtual and non-virtual inheritance of a base class

╄→гoц情女王★ 提交于 2019-11-27 02:04:53
问题 This is the code: struct Biology { Biology() { cout << "Biology CTOR" << endl; } }; struct Human : Biology { Human() { cout << "Human CTOR" << endl; } }; struct Animal : virtual Biology { Animal() { cout << "Animal CTOR" << endl; } }; struct Centaur : Human, Animal { Centaur() { cout << "Centaur CTOR" << endl; } }; int main() { Centaur c; return 0; } This code prints: Biology CTOR Biology CTOR Human CTOR Animal CTOR Centaur CTOR Why? Since we create a Centaur object, we start from building

Virtual tables and memory layout in multiple virtual inheritance

半世苍凉 提交于 2019-11-26 22:41:50
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 contains, where exactly each of them is placed? Which of virtual tables are shared with virtual table of C?

Virtual inheritance in C++

老子叫甜甜 提交于 2019-11-26 21:33:57
问题 I found this in a website while reading about virtual inheritance in c++ When multiple inheritance is used, it is sometimes necessary to use virtual inheritance. A good example for this is the standard iostream class hierarchy: //Note: this is a simplified description of iostream classes class ostream: virtual public ios { /*..*/ } class istream: virtual public ios { /*..*/ } class iostream : public istream, public ostream { /*..*/ } //a single ios inherited How does C++ ensure that only a

What is the VTT for a class?

拈花ヽ惹草 提交于 2019-11-26 17:34:55
问题 Recently ran across a C++ linker error that was new to me. libfoo.so: undefined reference to `VTT for Foo' libfoo.so: undefined reference to `vtable for Foo' I recognized the error and fixed my problem, but I still have a nagging question: what exactly is a VTT? Aside: For those interested, the problem occurs when you forget to define the first virtual function declared in a class. The vtable goes into the compilation unit of the class's first virtual function. If you forget to define that