What are the various reasons to use virtual destructor in base class?
1. Without a virtual destructor the proper destructor may not be
called:
struct B {~B();};
struct D : B {~D();};
B* b new D;
delete b; // <--------- Will not call D::~D() !!!!!
2. Without a virtual destructor operator delete(void* size_t) may
not be called with the correct size.
struct B {~B(); operator delete(void* size_t);};
struct D : B {~D();};
B* b new D;
delete b; // <--------- Will call operator delete(void* size_t) with
// the size of B not the size of D!!!
3. Without a virtual destructor and when MI is used operator
delete(void*) or operator delete (void* size_t) may be called with
the wrong address.
struct B {~B();};
struct A {};
struct D : A B {~D();};
B* b new D;
delete b; // <--------- May not pass to operator delete the address
// that was returned by operator new!!!
No comments:
Post a Comment