suppose a class der is derived from a class base . if we now create a der class object by saying
base *p;
p = new der;
then firstly the base class constructor would be called followed by a call to the derived class constructor if we now say delete p, then destruction should proceed from der to base . This can be ensured by declaring the base class destructor as virtual . when we declare virtual destructor time is spent in building the vtable if we don't intend to create the objects through new then there is no need to create unnecessarily time would have been spent for building vtable enven if you don't need it, hence destructor are not made virtual by default .
base *p;
p = new der;
then firstly the base class constructor would be called followed by a call to the derived class constructor if we now say delete p, then destruction should proceed from der to base . This can be ensured by declaring the base class destructor as virtual . when we declare virtual destructor time is spent in building the vtable if we don't intend to create the objects through new then there is no need to create unnecessarily time would have been spent for building vtable enven if you don't need it, hence destructor are not made virtual by default .

0 comments:
Post a Comment