What is happening during `delete this;` statement?

前端 未结 7 2834
余生分开走
余生分开走 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.

提交回复
热议问题