Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
If in derived class I do not override a virtual function defined in base class. Then how many virtual table will be created if I create object of the base and derived classes?
C++
base
{
   virtual xyz()
   {
   }
};

derived: public base
{

};

main()
{
   base bobj;
   derived dobj;

}


Here will the virtual table for derived class be created?
Posted

Two.But in this case The virtual table of derived class will contain the base::xyz function pointer.
 
Share this answer
 
Comments
rupeshkp728 15-Nov-11 6:45am    
Thanks.
I suspect in this case the compiler will be clever enough to detect that no* VFT is actually required:

1. the base class never* needs one as it will always use it's own methods.*
2. The derived class doesn't override any virtual function, so doesn't need a VFT either.*(see below)

If you added an override in derived, only then a VFT for class derived would have to be added. Also, any instance created of this class will require additional memory for a pointer to the VFT (then again, if you use run-time type information or program in managed C++, there may already be a pointer to class-related information, so you may not notice a difference in memory consumption)

*P.S. - a correction:
The class derived must have a virtual function table! The reason is that if in another compilation unit, or even another library, a class derived2 is derived from derived and overrides xyz() virtually, then any code accessing an object through a pointer of type derived* will have to decide at runtime what class this object really is, and whether the version in base, or another has to be called.

So, what Emilio posted is correct. I wasn't even aware of the possibility of sharing a vtable, but it makes sense.
 
Share this answer
 
v3
Comments
rupeshkp728 15-Nov-11 6:47am    
Thanks Stefan for the correction.
The way a C++ compiler implements the dynamic method dispatching is ... implementation dependent, and can even be not base on the concept of v-tables.

What the C++ specs says is that:
* all methods are inherited
* every method can be overridden
* the override of a virtual method is virtual
* an implicit or -indirect call to a virtual method always result in a call the most derived override.

How the compiler does this is not a problem for a C++ programmer, but for the compiler developer.

The use of V-tables is just a very common technique. If this is adopted, in your case, since your derived has a virtual method (it inherits that from base) it must have a vtable pointer (just like base has).

Note that the objects don't have vtables, but just a pointers that point to a vtable that is shared among all the object of the same type.

But since you derived adds nothing to base, a compiler can optimize making both sharing a same vtable, and since no overrides are defined for xyz, a further compiler optimization may be to make that function non virtual at all.

In any case you shouldn't care. Whether a vtable is required, the compiler will generate it and all the class instances will have a vtable pointer.
 
Share this answer
 
Comments
Stefan_Lang 15-Nov-11 6:29am    
You got it right. I had to correct my posting and added an example to counter my first version, but I'm afraid now it looks less clear than your post. My 5.
rupeshkp728 15-Nov-11 6:45am    
Thanks Emilio for the reply.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900