What is happening during `delete this;` statement?

前端 未结 7 2725
余生分开走
余生分开走 2021-02-20 08:47

Please consider the following code:

class foo
{
public:
    foo(){}
    ~foo(){}
    void done() { delete this;}
private:
    int x;
};

What is

7条回答
  •  忘掉有多难
    2021-02-20 09:11

    Both would cause an error.

    The first pointer is deleted twice, with the second delete causing the error whereas the second one is allocated on the stack and cannot be implicitly deleted (error caused by first invocation of destructor).

提交回复
热议问题