C + +: Virtual Inheritance and Cross delegation

Source: Internet
Author: User

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 would merge the virtual T Able of sisters for their multi-inheritanted son.

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

Quote:

What's happening behind the scene.

The simple explanation are, because inheritance from are Base virtual in both Der1 Der2 and, there are a single Insta nCE of the object in the most derived object Join . At compile time, and assuming (which was the common case) virtual tables as dispatch mechanism, when compiling Der1::foo it wil L REDIRECT the call to bar() through the vtable.

Now the question are how the compiler generates vtables for each of the objects and the vtable for would Base contain both null Pointers, the vtable for would Der1 contain and Der1::foo a null pointer and the vtable for would Der2 contain a null point ER and Der2::bar [*]

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

So the code in would Der1::foo dispatch through Join ' s vtable to the final Overrider, which in this case was in a different B Ranch of the virtual inheritance hierarchy.

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

[*] Most compilers won't write null pointers here, but rather a pointer to a generic function that'll print an error mess Age terminate and the application, allowing for better diagnostics than a plain segmentation fault.

C + +: Virtual Inheritance and Cross delegation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.