How Virtual functions are implemented C++?

by | Views: 805

can you please tell me that how virtual functions are implemented C++



Tags:  C++  Virtual functions  Oops

Answers (1)
 
jack Said..

Virtual functions are implemented using a table of function pointers, called the vtable. There
is one entry in the table per virtual function in the class. This table is created by the constructor of the class. When a derived class is constructed, its base class is constructed _rst which creates the vtable. If the derived class overrides any of the base classes virtual functions, those entries in the vtable are overwritten by the derived class constructor. This is why you should never call virtual functions from a constructor: because the vtable entries for the object may not have been set up by the derived class constructor yet, so you might end up calling base class implementations of those virtual functions.



Register or Login to Post Your Opinion/Answer