C++: virtual inheritance and Cross Delegation

Link1: Give an example

Note: I think the Storable::Write method should also be pure virtual.

http://www.cprogramming.com/tutorial/virtual_inheritance.html

Link2: explained from the vritual table point of view, Key point: as virual inheritance, complier will merge the virtual table of sisters for their multi-inheritanted son.

http://stackoverflow.com/questions/4487914/which-function-to-call-delegate-to-a-sister-class

Quote:

 

What is happening behind the scene.

The simple explanation is that, because inheritance from Base is virtual in both Der1 and Der2, there is a single instance of the object in the most derived object Join. At compile time, and assuming (which is the common case) virtual tables as dispatch mechanism, when compiling Der1::foo it will redirect the call to bar() through the vtable.

Now the question is how the compiler generates vtables for each of the objects, the vtable for Base will contain two null pointers, the vtable for Der1 will contain Der1::foo and a null pointer and the vtable for Der2 will contain a null pointer and Der2::bar [*]

Now, because of virtual inheritance in the previous level, when the compiler processes Join it will create a single Base object, and thus a single vtable for the Base subojbect of Join. It effectively merges the vtables of Der1 and Der2 and produces a vtable that contains pointers to Der1::foo and Der2::bar.

So the code in Der1::foo will dispatch through Join‘s vtable to the final overrider, which in this case is in a different branch of the virtual inheritance hierarchy.

If you add a Der3 class, and that class defines either of the virtual functions, the compiler will not be able to cleanly merge the three vtables and will complain, with some error relating to the ambiguity of the multiply defined method (none of the overriders can be considered to be the final overrider). If you add the same method to Join, then the ambiguity will no longer be a problem, as the final overrider will be the member function defined in Join, so the compiler is able to generate the virtual table.

[*] Most compilers will not write null pointers here, but rather a pointer to a generic function that will print an error message and terminate the application, allowing for better diagnostics than a plain segmentation fault.

时间: 2024-12-20 01:18:57

C++: virtual inheritance and Cross Delegation的相关文章

Memory Layout for Multiple and Virtual Inheritance

2016/6/7 phc ­­ Memory Layout for Multiple and Virtual Inheritance ­­ Edsko de Vrieshttp://www.phpcompiler.org/articles/virtualinheritance.html 1/10Home | Download phc | Documentation | Developers and Contributors | Mailing List |Memory Layout for Mu

Memory Layout for Multiple and Virtual Inheritance (一) (部分翻译)

Memory Layout for Multiple and Virtual Inheritance  Sources: http://www.phpcompiler.org/articles/virtualinheritance.html 本文主要介绍了gcc编译器中multiple 和 virtual inheritance中的对象内存布局.虽然C++程序员不需要关心编译器的内部细节,但多重继承(特别是虚拟继承)在C++代码实现中有不同的结果(特别是用pointers to pointers

C++ 多继承和虚继承的内存布局(Memory Layout for Multiple and Virtual Inheritance)

警告. 本文有点技术难度,需要读者了解C++和一些汇编语言知识. 在本文中,我们解释由gcc编译器实现多继承和虚继承的对象的布局.虽然在理想的C++程序中不需要知道这些编译器内部细节,但不幸的是多重继承(特别是虚拟继承)的实现方式有各种各样的不太明确的结论(尤其是,关于向下转型指针,使用指向指针的指针,还有虚拟基类的构造方法的调用命令). 如果你了解多重继承是如何实现的,你就能预见到这些结论并运用到你的代码中.而且,如果你关心性能,理解虚拟继承的开销也是非常有用的.最后,这很有趣. :-) 多重

C++ virtual inheritance ZZ

虚继承 是面向对象编程中的一种技术,是指一个指定的基类,在继承体系结构中,将其成员数据实例共享给也从这个基类型直接或间接派生的其它类. 举例来说:假如类A和类B各自从类X派生(非虚继承且假设类X包含一些数据成员),且类C同时多继承自类A和B,那么C的对象就会拥有两套X的实例数据(可分别独立访问,一般要用适当的消歧义限定符).但是如果类A与B各自虚继承了类X,那么C的对象就只包含一套类X的实例数据.对于这一概念典型实现的编程语言是C++. 这一特性在多重继承应用中非常有用,可以使得虚基类对于由它直

C++: Virtual Table and Shared Memory

See at:补充栏3: C++对象和共享内存 (叙述内容和Link1的内容基本一致) <C++网络编程 卷1:运用ACE和模式消除复杂性> <C++ Network Programming Volume 1 Mastering Complexity with ACE and Patterns> -Douglas C.Schmidt, Stephen D. Huston -叶斌译 Link1: Is it possible to store polymorphic class in

C++ Virtual详解

Virtual是C++ OO机制中很重要的一个关键字.只要是学过C++的人都知道在类Base中加了Virtual关键字的函数就是虚拟函数(例如函数print),于是在Base的派生类Derived中就可以通过重写虚拟函数来实现对基类虚拟函数的覆盖.当基类Base的指针point指向派生类Derived的对象时,对point的print函数的调用实际上是调用了Derived的print函数而不是Base的print函数.这是面向对象中的多态性的体现.(关于虚拟机制是如何实现的,参见Inside t

OOP in JS - Inheritance

Summary You cause a class to inherit using ChildClassName.prototype = new ParentClass();. You need to remember to reset the constructor property for the class using ChildClassName.prototype.constructor=ChildClassName. You can call ancestor class meth

[转]C++ Virtual详解

Virtual是C++ OO机制中很重要的一个关键字.只要是学过C++的人都知道在类Base中加了Virtual关键字的函数就是虚拟函数(例如函数print),于是在 Base的派生类Derived中就可以通过重写虚拟函数来实现对基类虚拟函数的覆盖.当基类Base的指针point指向派生类Derived的对象 时,对point的print函数的调用实际上是调用了Derived的print函数而不是Base的print函数.这是面向对象中的多态性的体现. (关于虚拟机制是如何实现的,参见Insid

Learning JavaScript Design Patterns -- A book by Addy Osmani

Learning JavaScript Design Patterns A book by Addy Osmani Volume 1.6.2 Tweet Copyright © Addy Osmani 2015. Learning JavaScript Design Patterns is released under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 unported license. It