“delete this” in constructor

前端 未结 3 2057
广开言路
广开言路 2020-12-15 04:19

What actually happen when I execute this code?

class MyClass
{
    MyClass()
    {
        //do something
        delete this;   
    }
}
3条回答
  •  旧时难觅i
    2020-12-15 04:39

    Assuming your object is never inherited by anything this should work fine. Your constructor runs and then the destructor is immediately called. If anything inherits this object it will break since this constructor will be called before the inheriting constructor.

提交回复
热议问题