What is happening during `delete this;` statement?

前端 未结 7 2837
余生分开走
余生分开走 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条回答
  •  Happy的楠姐
    2021-02-20 09:06

    This question has been answered but I will add a new point that if your class does call delete this then you should also make the destructor private.

    This ensures that only the class can delete itself.

    If you make your destructor private above, both of your code samples will fail to compile.

提交回复
热议问题