C++: virtual inheritance and Cross Delegation

Posted 鱼悠游

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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.

以上是关于C++: virtual inheritance and Cross Delegation的主要内容,如果未能解决你的问题,请参考以下文章

Memory Layout for Multiple and Virtual Inheritance

C++ virtual 关键字总结

c++ virtual总结

C++ 虚函数(virtual) 和纯虚函数(pure virtual) 的区别

C++ 风格:为覆盖方法添加前缀 virtual 关键字

C++ Virtual详解