virtual-inheritance

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

你。 提交于 2019-12-02 23:50:38
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? Doesn't this behavior is wrong? We want that object of type A to execute A::f() method and not B::f() . Are

Memory layout of a class under multiple or virtual inheritance and the vtable(s)?

家住魔仙堡 提交于 2019-12-02 18:23:17
I am reading "Inside the C++ Object Model", trying to understand how multiple and virtual inheritance is achieved via the vtables.(I understand single polymorphism perfectly-well). I am having difficulties understand what exactly is done when a method needs to be located during virtual inheritance, or during casting, because there is a lot of offset calculation to be performed. Would somebody be able to help with understanding how the multiple vtables are used in a multiple or virtual inheritance example? If I could understand the layout and the problem, I could probably understand this issue

understanding vptr in multiple inheritance?

时光毁灭记忆、已成空白 提交于 2019-12-02 16:22:11
I am trying to make sense of the statement in book effective c++. Following is the inheritance diagram for multiple inheritance. Now the book says separate memory in each class is required for vptr. Also it makes following statement An oddity in the above diagram is that there are only three vptrs even though four classes are involved. Implementations are free to generate four vptrs if they like, but three suffice (it turns out that B and D can share a vptr), and most implementations take advantage of this opportunity to reduce the compiler-generated overhead. I could not see any reason why

Where is the “virtual” keyword necessary in a complex multiple inheritance hierarchy?

回眸只為那壹抹淺笑 提交于 2019-12-02 15:59:46
I understand the basics of C++ virtual inheritance. However, I'm confused about where exactly I need to use the virtual keyword with a complex class hierarchy. For example, suppose I have the following classes: A / \ B C / \ / \ D E F \ / \ / G H \ / I If I want to ensure that none of the classes appear more than once in any of the subclasses, which base classes need to be marked virtual ? All of them? Or is it sufficient to use it only on those classes that derive directly from a class that may otherwise have multiple instances (i.e. B, C, D, E and F; and G and H (but only with the base class

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

邮差的信 提交于 2019-12-01 18:45:54
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(a) { std::cerr << "A(a) - me:" << ((void*)this) << std::endl; } virtual ~A() { std::cerr << "~A" << (

Virtual base classes order of creation

狂风中的少年 提交于 2019-12-01 05:18:42
问题 I have the following problem: struct A1 { A1() { std::cout << "A1, "; } }; struct A2 { A2() { std::cout << "A2, "; } }; struct AA1 : virtual A1, A2 { AA1() { std::cout << "AA1, "; } }; struct AA2 : A1, virtual A2 { AA2(){ std::cout << "AA2, "; } }; struct B : AA1, virtual AA2 { B() { std::cout << "B "; } }; int main() { B b; } When you run this code, the answer is: A1 A2 A1 AA2 A2 AA1 B I want to understand where is the first A1 created. I know the rule that the virtual classes are called

Force deriving from a class virtually

☆樱花仙子☆ 提交于 2019-12-01 03:51:18
We have a special framework for interfaces in our project, and part of the requirements is that classes which represent an interface may only be used as virtual base classes, not as non-virtual ones. Is there a way to enforce this in code? That is, produce a compilation error if the class is derived from non-virtually. I have access to C++11 as implemented by VS 2010: this means static_assert , enable_if and <type_traits> are available. IMO, there is no clean and platform independent solution available to this problem. The best way is to manually go and change each and every inheritance to

Pure Virtual Class and Collections (vector?)

社会主义新天地 提交于 2019-12-01 03:48:18
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 currently using a Vector to store shapes, however, it's apparent that this is the wrong decision since

C++ virtual inheritance initializer list

可紊 提交于 2019-11-30 19:12:44
in the following code: class A { public: int x; A(int x):x(x){} }; class B: public virtual A { public: B(int x):A(x){} }; class C: public virtual A { public: C(int x):A(x){} }; class D: public B, public C { public: D(int x):B(x++), C(x++), A(x++){} }; two questions: Why do I need to add A(...) in D's initializer list? D(int x):B(x++), C(x++), A(x++){} and D(int x):A(x++), B(x++), C(x++){} both give the same result with cout<<D(10).x , why? Why do I need to add A(...) in D's initializer list? That's because virtual base subobjects must be initialized before all other subobjects. Since A does

Misaligned address using virtual inheritance

我怕爱的太早我们不能终老 提交于 2019-11-30 14:31:07
The following apparently valid code produces a misaligned address runtime error using the UndefinedBehaviorSanitizer sanitiser. #include <memory> #include <functional> struct A{ std::function<void()> data; // seems to occur only if data is a std::function } ; struct B{ char data; // occurs only if B contains a member variable }; struct C:public virtual A,public B{ }; struct D:public virtual C{ }; void test(){ std::make_shared<D>(); } int main(){ test(); return 0; } Compiling and executing on a macbook with clang++ -fsanitize=undefined --std=c++11 ./test.cpp && ./a.out produces the output