What are vTables?

R

Rene

I keep reading about this vTables and it's killing me because I am not sure
what they are. I have a feeling that vTables are used when you override
virtual methods or properties but I really have no idea. Below is an
explanation of what I *think* vTables are. Please consider the following two
classes:

class BaseClass
{
public virtual void MyVirtualFunctio() {}
}

class MainClass : BaseClass
{
public override void MyVirtualFunctio() {}
}

My guess is that the compiler will create a virtual table where the adress
of the function "MyVirtualFunctio" on "BaseClass" will be rerouted to the
"MyVirtualFunctio" on "MainClass".

Is this what vTables are? I know that it does't really matter if I don't
know what a vTable are and how they work since there is nothing I can do
about them but I was just curions if someone could share some links to where
this concept is explained in more detail.

Thanks.
 
N

Nick Hounsome

Rene said:
I keep reading about this vTables and it's killing me because I am not sure
what they are. I have a feeling that vTables are used when you override
virtual methods or properties but I really have no idea. Below is an
explanation of what I *think* vTables are. Please consider the following
two classes:

class BaseClass
{
public virtual void MyVirtualFunctio() {}
}

class MainClass : BaseClass
{
public override void MyVirtualFunctio() {}
}

My guess is that the compiler will create a virtual table where the adress
of the function "MyVirtualFunctio" on "BaseClass" will be rerouted to the
"MyVirtualFunctio" on "MainClass".

Is this what vTables are? I know that it does't really matter if I don't
know what a vTable are and how they work since there is nothing I can do
about them but I was just curions if someone could share some links to
where this concept is explained in more detail.

It's realy a bit of a hangover from C++ where you can actually find global
structures called MyClass_vtbl or similar in the symbol table.

Basicaly each object that has or is derived from a class with virtual
methods has a hidden field that points to a readonly static table of
function addresses (a virtual function table) that is set up in the
constructor and never changes. Calls to virtual functions are then
implemented as something like "(*(this->vtbl[3]))(args...)"

C# does not have these at the IL level as you can see with ildasm but that
doesn't mean that they aren't used by the JIT compiler (I can't think of any
other way to do it that is efficient)

Sometimes rather than pointing to the actual methods they point to small
bits of code called thunks that manipulate the "this" pointer before calling
the real function but this (or an alternative involving another table) is
only actually required for multiple inheritance in C++ which C# doesn't have
although interfaces are probably handled the same way.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top