Does virtual inheritance and virtual function use the same vtable?
There is one little related question. But the topic is entirely different.
Now, one co开发者_如何学JAVAncept is about the function resolution and another is about class
resolution ? I am wondering that how is it possible if they are using the same vtable
(at least in gcc-4.5) ? Is this a compiler dependent terminology ?
I know that it might appear as basic silly question, but I had never thought of it.
A good reference for this sort of thing is the Itanium ABI - see eg http://mentorembedded.github.com/cxx-abi/abi.html#vtable. Despite the name it's a widely used ABI for C++ and it describes a good, working implementation (although obviously other implementations are possible).
You can solve both problems (virtual function calls and virtual inheritance) if you know the dynamic type of an object given just a pointer to it. Every (polymorphic) object in C++ has precisely one dynamic type, which is determined at the moment when it's constructed. E.g. when you write new Foo
, that object has the dynamic type Foo
even if you store just a void*
.
A vtable
is a mechanism to store information about the dynamic type of an object in such a way that it can be retrieved via a base pointer. You can store quite some things in a vtable: function pointers, cast offsets, std::type_info
objects even.
精彩评论