使用向上转换时的虚拟析构函数

发布于 2024-11-06 20:30:01 字数 505 浏览 5 评论 0原文

每个人都说,当至少有一个类方法是虚拟的时,析构函数应该是虚拟的。
我的问题是,使用向上转换时析构函数应该是虚拟的,这不是正确的说法吗?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

今天小雨转甜 2024-11-13 20:30:01

如果您通过基指针删除派生对象,则需要一个虚拟析构函数。简而言之,就是这样。

You need a virtual destructor if you ever delete a derived object via a base pointer. That's it, in a nutshell.

怪我入戏太深 2024-11-13 20:30:01

当至少一个类方法是虚拟的时,析构函数应该是虚拟的

这是一条经验法则,它源于这样一个事实:当您使用虚拟函数时,您正在使用运行时多态性,并且更有可能遇到需要销毁一个析构函数的情况。当您拥有的只是指向其基类子对象的指针时,该类可能属于派生类型。

当您通过对指向基类的指针使用delete来销毁派生对象时,基类中需要一个虚拟析构函数以避免未定义的行为。这是唯一一次需要虚拟析构函数的情况,该指南旨在帮助避免出现这种情况。

Herb Sutter 提倡基类析构函数(即设计为继承自的类的析构函数)的指导方针) 应该是 publicvirtualprotected 和非virtual。这使得您可能不希望基类成为继承层次结构中用于删除派生对象的点,并且您希望强制这不会无意中发生。

当然,如果您有一个不是基类的纯值类,那么您几乎无法阻止人们从它派生,然后通过指向基类的指针删除派生类。

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.

心奴独伤 2024-11-13 20:30:01

如果您在派生类析构函数中执行一些有用的操作(内存释放等),则应将析构函数设为虚拟。

顺便说一句,当包含虚拟方法时,建议(非强制)使用虚拟析构函数。

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.

眼中杀气 2024-11-13 20:30:01

如果您要删除指向派生类的指针,但该指针类型是基类,则当其中一个类方法为虚拟时,您应该始终声明虚拟析构函数,那么在这种情况下,基类必须具有虚拟析构函数,否则编译器不会不(或不应该)知道它应该调用什么析构函数(并且它是 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).

暮年慕年 2024-11-13 20:30:01

这是不正确的:

shared_ptr<A> ptr = make_shared<B>();

正确地向上转换并删除 B 。只需遵循@Neil Butterworth 的回答即可。

It's incorrect:

shared_ptr<A> ptr = make_shared<B>();

does up cast and deletes B correctly. Just follow @Neil Butterworth's answer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文