开发者

What is the structure of virtual tables in C++?

开发者 https://www.devze.com 2023-03-02 02:51 出处:网络
For Example I have two \"intefaces\" and class type: class IPlugin { public: virtual void Load(void) = 0;

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
           }
 };

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:开发者_C百科

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

Thanks in advance!


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

What is the structure of virtual tables in C++?

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.

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.

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.

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.


What structure vtab actually has for instance of type Tester?

The mechanism of virtual dispatching is implementation-defined. vtable and vptr are not required by the C++ Standard and that knowledge isn't even necessary for programmers to program in C++, because you cannot access the virtual table (even if your compiler implements this); its generated by the compiler and added to your code, like it does lots of things to your code before it converts it into machine code.


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

Here dynamic_cast is not needed in the second line. The following is enough:

Tester* t = new Tester();
IPlugin* plg = t;                 //upcast          - dynamic_cast not needed
IFoo* f=dynamic_cast<IFoo*>(plg); //horizontal-cast - dynamic_cast needed

dynamic_cast is not needed in upcast; its needed in downcast and horizontal-cast only.

Tester* tester1 = dynamic_cast<Tester*>(plg); //downcast - dynamic_cast needed
Tester* tester2 = dynamic_cast<Tester*>(f);   //downcast - dynamic_cast needed


In ISO/IEC 14882 Second edition 2003-10-15 there is not exist such terms like vptr, virtual table, so it is completely up to compiler implementators.

Info about impl. in microsoft's visual C++: http://www.openrce.org/articles/files/jangrayhood.pdf

Article about impl. virtual tables in g++: http://phpcompiler.org/articles/virtualinheritance.html


Virtual Mechanism(Virtual Pointer & Virtual Table) is not defined by the C++ standard. It is left out for the compiler to implement the mechanism in its own choosen way. It is an implementation detail of the compiler. Given that, the details of how the compiler will implement the virtual mechanism is abstracted from the user. What should matter is only the beahviors that are expected out of the virtual mechanism.

In your case:

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

plg & f both will point to a valid object of their respective types because t is derives from both of them.

Ofcourse this does not answer the specific question you asked but just wanted to clear the detail on virtual mechanism being a implementation detail of the compilers.

0

精彩评论

暂无评论...
验证码 换一张
取 消