使用向上转换时的虚拟析构函数
每个人都说,当至少有一个类方法是虚拟的时,析构函数应该是虚拟的。
我的问题是,使用向上转换时析构函数应该是虚拟的,这不是正确的说法吗?
class A {
public:
~A(){
cout << "Destructor 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;
}
我在这段代码中没有任何虚函数,但是如果我不将我的基析构函数设为虚函数,它就不会调用 B 析构函数。是的,我知道如果我没有虚拟函数,那么使用 ucpasting 是没有意义的。
谢谢。
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 << "Destructor 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您通过基指针删除派生对象,则需要一个虚拟析构函数。简而言之,就是这样。
You need a virtual destructor if you ever delete a derived object via a base pointer. That's it, in a nutshell.
这是一条经验法则,它源于这样一个事实:当您使用虚拟函数时,您正在使用运行时多态性,并且更有可能遇到需要销毁一个析构函数的情况。当您拥有的只是指向其基类子对象的指针时,该类可能属于派生类型。
当您通过对指向基类的指针使用
delete
来销毁派生对象时,基类中需要一个虚拟析构函数以避免未定义的行为。这是唯一一次需要虚拟析构函数的情况,该指南旨在帮助避免出现这种情况。Herb Sutter 提倡基类析构函数(即设计为继承自的类的析构函数)的指导方针) 应该是
public
和virtual
或protected
和非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
andvirtual
orprotected
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 avirtual
method.如果您要删除指向派生类的指针,但该指针类型是基类,则当其中一个类方法为虚拟时,您应该始终声明虚拟析构函数,那么在这种情况下,基类必须具有虚拟析构函数,否则编译器不会不(或不应该)知道它应该调用什么析构函数(并且它是 UB)。
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).
这是不正确的:
正确地向上转换并删除
B
。只需遵循@Neil Butterworth 的回答即可。It's incorrect:
does up cast and deletes
B
correctly. Just follow @Neil Butterworth's answer.