Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does virtual inheritance work? [closed]

  1. Does virtual inheritance use vTable? If yes or no, Then how it implemented
  2. How Virtual inheritance behave in memory?
  3. Any other alternative for virtual inheritance

Really appreciate a conceptual explanation.

like image 864
Nayana Adassuriya Avatar asked Sep 07 '12 04:09

Nayana Adassuriya


1 Answers

Virtual inheritance is implemented in different way depending on the compiler.

1) Yes, both vc and gcc use vTable pointer. But vc has another pointer ref as virtual base pointer.

2) For a classical virtual inheritance, like Left < Top; Right < Top; Bottom < Left and Right The stack should be like this:

Left.vptr  // -> to its vtable

Left::element_in_left

Right.vptr

Right::element_in_right

Bottom::element_in_bottom

Top::element_in_top

Programs use vptr to find vtable, for gcc, there is a virtual base offset value in viable. vptr + base offset will give the parent address.

3) I'm not sure what do you mean. There are different ways to implement it in C++ compilers. And other languages have their ways to bind functions.

like image 196
halfelf Avatar answered Oct 01 '22 07:10

halfelf