Alex Rivera | Logout

Does every object of virtual class have a pointer to vtable?

Asked 2009-02-18T15:47:43.583
15

Does every object of virtual class have a pointer to vtable?

Or only the object of base class with virtual function has it?

Where did the vtable stored? code section or data section of process?

Edit
Report

2 Answers

1

All virtual classes usually have a vtable, but it is not required by the C++ standard and the storage method is dependent upon the compiler.

answered 2009-02-18T15:55:04.720
0

Not necessarily

Pretty much every object that has a virtual function will have one v-table pointer. There doesn't need to be a v-table pointer for each class that has a virtual function that the object derives from.

New compilers that analyse the code sufficiently may be able to eliminate v-tables in some cases though.

For example, in a simple case: if you only have one concrete implementation of an abstract base class, the compiler knows that it can change the virtual calls to be regular function calls because whenever the virtual function is called it will always resolve to the exact same function.

Also, if there's only a couple of different concrete functions, the compiler may effectively change the call-site so that it uses an 'if' to select the right concrete function to call.

So, in cases like this the v-table isn't needed and the objects might end up not have one.

answered 2009-02-18T16:03:41.813

Your Answer