C++ 如何通过析构函数方法删除对象(类)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11456161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 15:09:37  来源:igfitidea点击:

How to delete an object (class) through destructor method

c++classdestructor

提问by Sergey K.

I'm wonder to know is it possible to delete an object through destructor method?

我想知道是否可以通过析构函数方法删除对象?

Constructor and destructor of my class:

我的类的构造函数和析构函数:

class cal
{
    public:
        cal()
        {
            days = 0;
            day = 1;
            month = 1;
            year = 1300;
            leap = true;
        };
        ~cal()
        {
            delete this;
        }
}*calendar = new cal;

How can I delete this pointer through class?

如何通过类删除此指针?

P.S

聚苯乙烯

I forgot to write the following code

我忘了写下面的代码

cal *calandar = new cal[];

I want to use it in heap not stack

我想在堆而不是堆栈中使用它

I want to use this class (objects) frequently (many of that object) imagine how many time should I write delete and it make hard understanding, troubleshooting and tracing codes I want them to been destroyed automatically (in heap)

我想经常使用这个类(对象)(很多那个对象)想象一下我应该写多少次 delete 并且它很难理解,故障排除和跟踪代码我希望它们被自动销毁(在堆中)

I used following code in my class when I exec "delete[] calendar" it reduce my rams occupied (amount of ram that is used) does it work properly (destroy all objects) by exiting program? Because I use GNU/Linus and it destructs all objects with or without that lines I'm concern about leaking in windows

当我执行“delete[]日历”时,我在我的班级中使用了以下代码,它减少了我占用的内存(使用的内存量)是否通过退出程序正常工作(销毁所有对象)?因为我使用 GNU/Linus 并且它会破坏所有带有或不带有该行的对象,我担心在 Windows 中泄漏

void DisposeObject() { delete this; }

回答by Mike Seymour

No. By the time the destructor is called, the object is already being destroyed, so delete thisis not valid.

不。在调用析构函数时,对象已经被销毁,因此delete this无效。

The behaviour is undefined, but most likely it will call the destructor recursively, leading to a stack overflow.

行为未定义,但很可能会递归调用析构函数,导致堆栈溢出。

回答by Sergey K.

You can write the code like this:

你可以这样写代码:

class cal
{
    public:
        cal()
        {
        };
        ~cal()
        {
        }
        void DisposeObject()
        {
           delete this;
        }
}

And it will call your destructor.

它会调用你的析构函数。

You should not call delete thisfrom your destructor since it will call the destructor recursively, possibly leading to a stack overflow. Anyway, the code you wrote suffers from undefined behavior.

您不应该delete this从析构函数中调用,因为它会递归调用析构函数,可能会导致堆栈溢出。无论如何,您编写的代码存在未定义行为

Refer to this question for in-depth discussion: Is delete this allowed?

深入讨论参考这个问题:Is delete this allowed?

回答by Reed Copsey

I`m wonder to know is it possible to delete an object through destructor method

我想知道是否可以通过析构函数方法删除对象

Even supposing this was valid - The problem is that nothing would ever callthe destructor, so this would never have an effect. The destructor is called when the calling code deletes the object, which in turn can delete any internal objects as needed.

即使假设这是有效的 - 问题是没有任何东西会调用析构函数,所以这永远不会产生效果。当调用代码删除对象时会调用析构函数,从而可以根据需要删除任何内部对象。

In your case, it doesn't look like you need to do anything in your destructor, however, as you don't have any resources being allocated that need to be explicitly deleted (at least in what you're showing).

在您的情况下,您似乎不需要在析构函数中执行任何操作,但是,因为您没有分配任何需要显式删除的资源(至少在您显示的内容中)。