What is happening during `delete this;` statement?

前端 未结 7 2814
余生分开走
余生分开走 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:28

    Calling delete this is a bad idea. Whoever calls new should call the delete. Hence the problems as highlighted in the other responses.

    Also you can have memory leaks/undefined behaviour when construcing an array of objects.

    0 讨论(0)
提交回复
热议问题