Virtual function should not be called from constructor and distructor why ?

As we know when derived class is created, First base class object is constructed if we are going to call a virtual function from the base class constructor then till now the derived class object is not created, So it it will be unable to call the derived class function. In the same manner if we are calling the virtual function from the base class destructor, So what happened the derived class object will be destroyed first and when it will come to the derived class destructor there is nothing like derived class. There is a downside of the virtual function we can’t create all the function as virtual. When we create a function as virtual it resolved at running time it means it take time to resolve and we have to keep record of pointer which points to these functions in the Virtual Table so it will be more expensive to make all the function as virtual.

2,036 total views, 10 views today

Leave a Reply