虚继承和虚函数使用同一个vtable吗?
有一个相关的小问题。但主题完全不同。
现在,一个概念是关于函数解析,另一个概念是关于类解析?我想知道如果他们使用相同的 vtable
(至少在gcc-4.5) ?这是编译器相关的术语吗?
我知道这可能看起来是一个基本的愚蠢问题,但我从未想过。
There is one little related question. But the topic is entirely different.
Now, one concept 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
此类事情的一个很好的参考是 Itanium ABI - 请参阅 http:// Mentorembedded.github.com/cxx-abi/abi.html#vtable。尽管有这个名字,它是一个广泛使用的 C++ ABI,并且它描述了一个良好的、有效的实现(尽管显然其他实现也是可能的)。
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).
如果您知道仅给定指向对象的指针的动态类型,则可以解决这两个问题(虚拟函数调用和虚拟继承)。 C++ 中的每个(多态)对象都精确地具有一种动态类型,该类型在构造时确定。例如,当您编写
new Foo
时,即使您只存储void*
,该对象也具有动态类型Foo
。vtable
是一种存储有关对象动态类型的信息的机制,以便可以通过基指针检索该信息。您可以在 vtable 中存储很多东西:函数指针、转换偏移量、甚至 std::type_info 对象。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 typeFoo
even if you store just avoid*
.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.