C ++中虚拟表的结构是什么? [英] What is the structure of virtual tables in C++?

查看:192
本文介绍了C ++中虚拟表的结构是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



例如,我有两个intefaces和类类型:


For Example I have two "intefaces" and class type:

class IPlugin
{
  public:
    virtual void Load(void) = 0;
    virtual void Free(void) = 0;
};

class IFoo
{
  public:
    virtual void Foo(void) = 0;
};


class Tester: public IPlugin, public IFoo
{
   public:
           Tester() {};
          ~Tester() {};

           virtual void Load()
           {
              // Some code here
           }

           virtual void Free()
           {
              // Some code here
           }

           virtual void Foo(void)
           {
              // Some code here
           }
 };

什么结构vtab实际上具有类型 Tester的实例?如何在表达式中使用 dynamic_cast operator act(我的意思是 dynamic_cast 操作符会扫描vtab以获得有效的引用类型转换) :

What structure vtab actually has for instance of type Tester? And how would be dynamic_cast operator act ( I mean how dynamic_cast operator would scan vtab for valid reference type convertion) in expression:

Tester* t = new Tester();
IPlugin* plg = dynamic_cast<IPlugin*>(t);
IFoo* f = dynamic_cast<IFoo*>(plg);  

提前感谢!

推荐答案

C ++中的虚拟表是一个实现细节。一种可能的实现如下图所示。

Virtual tables in C++ is an implementation detail. One possible implementation is shown on the diagram below.

存在类(A和B)的两个实例。每个实例都有两个vtbl指针,vtbl包含指向实际代码的指针。

Two instances of the class (A and B) exists. Each instance has two vtbl pointers and the vtbl contains pointers to actual code.

在你的例子中没有实例数据,但是为了说明的目的,假设每个类包含一些实例数据。

In your example there is no instance data, but I have for illustrative purposes assumed that each class contains some instance data.

当指向 Tester 的指针被转换为 IFoo 指针如图所示进行调整。而不是指向实例数据的开始,它指向实例数据的 IFoo 部分。

When a pointer to Tester is cast to a pointer to IFoo the pointer is adjusted as shown on the diagram. Instead of pointing to the start of the instance data it points to the IFoo part of the instance data.

整洁的事情是,使用 IFoo 指针的调用者不具有关于 IFoo 部分周围的数据的任何知识班上。对于使用 IPlugin 指针的调用者,也可以这么说。这个指针恰好指向一个 Tester 指针所指向的实例数据的开始,但只有一个使用 Tester

The neat thing is that a caller using an IFoo pointer doesn't have any knowledge about the data surrounding the IFoo part of the class. The same can be said of for a caller using an IPlugin pointer. This pointer happens to point to the start of the instance data also pointed to by a Tester pointer but only a caller using a Tester pointer knows the entire layout of the instance data.

使用 dynamic_cast 需要RTTI(运行时类型信息),它不在图表上。 vtbl将包含额外的类型信息,给出一个 IFoo 指向 Tester 实例的指针允许run-时间来发现指针指向的对象的实际类型,并使用它来向下转换指针。

Using dynamic_cast requires RTTI (Run-Time Type Information) which is not on the diagram. The vtbl will contain additional type information that given a say IFoo pointer to an instance of Tester allows the code at run-time to discover the actual type of object pointed by the pointer and use that to downcast the pointer.

这篇关于C ++中虚拟表的结构是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆