Virtual Method Table during Constrction, C++ & C#

time to read 1 min | 157 words

Note: Unless you’re a C++ programmer, this isn’t likely to mean anything to you.

One of the differences between C++ and C# is in the way virtual methods are treated during object construction. If a base class constructor makes a virtual method call, it will get the base class method in C++ and the derived class method in C#.

This means that the v table is changing between constructor calls. The way I envision it is something like:

 

This is pseudo code, of course.

This came up during a talk where we considered the possibilities of writing the v table during runtime. The way I think about it (and I know it’s an implementation detail), the v table is a static data structure per each type. Am I correct in thinking this way?