Virtual destructor when using upcasting
Everyone says that a destructor should be virtual when at least one of class methods is virtual.
My questions is, isn't it correct to say that a destructor should be virtual when using upcasting ?class A {
public:
~A(){
cout << "Destr开发者_JAVA技巧uctor in A" << endl;
}
};
class B: public A
{
public:
~B(){
cout << "Destructor in B" << endl;
}
};
int main()
{
A* a = new B;
cout << "Exiting main" << endl;
delete a;
}
I don't have any virtual functions in this code, but if I don't make my base destructor virtual, it will not call B destructor. And yes I know that is pointless to use ucpasting if I don't have virtual functions.
Thank you.
You need a virtual destructor if you ever delete a derived object via a base pointer. That's it, in a nutshell.
a destructor should be virtual when at least one of class methods is virtual
This is a rule of thumb which arises from the fact that when you use virtual functions you are using runtime polymorphism and are more likely to run into situations were you need to destroy a class that may be of a derived type when all you have is a pointer to its base class subobject.
When you destroy a derived object by using delete
on a pointer to the base class a virtual destructor is necessary in the base class to avoid undefined behavior. This is the only time a virtual destructor is necessary and the guideline is intended to help avoid this situation arising.
Herb Sutter advocated the guideline that base class destructors (i.e. destructors for classes designed to be inherited from) should be either public
and virtual
or protected
and non-virtual
. This allows the possibility that you don't intend a base class to be a point in the inheritance hierarchy which is used for deletion of derived objects and you want to enforce that this doesn't occur unintentionally.
Of course, if you have a pure value class which isn't to be a base class, there is little you can do to stop people deriving from it anyway and then deleting derived class via pointer to base.
The destructor should be made virtual
if you are doing something useful (memory deallocation etc.) in the derived class destructor.
By the way, It's advisable (not mandatory) to have virtual destructor when a class
contains a virtual
method.
You should always declare a virtual destructor when one of the class methods is virtual if you are deleting a pointer to a derived class, but the pointer type is the base class then base class must have a virtual destructor in this case or the compiler doesn't (or isn't supposed to) know what destructor it should call (and it's UB).
It's incorrect:
shared_ptr<A> ptr = make_shared<B>();
does up cast and deletes B
correctly. Just follow @Neil Butterworth's answer.
精彩评论