相关疑难解决方法(0)

什么是三法则?

  • 什么是抄袭的对象是什么意思?
  • 什么是复制构造函数复制赋值运算符
  • 我什么时候需要自己申报?
  • 如何防止复制对象?

c++ c++-faq copy-constructor assignment-operator rule-of-three

2067
推荐指数
8
解决办法
27万
查看次数

是否允许删除?

是否允许delete this;delete-statement是将在该类实例上执行的最后一个语句?当然,我确信this-pointer所代表的对象是newly-created.

我在考虑这样的事情:

void SomeModule::doStuff()
{
    // in the controller, "this" object of SomeModule is the "current module"
    // now, if I want to switch over to a new Module, eg:

    controller->setWorkingModule(new OtherModule());

    // since the new "OtherModule" object will take the lead, 
    // I want to get rid of this "SomeModule" object:

    delete this;
}
Run Code Online (Sandbox Code Playgroud)

我可以这样做吗?

c++ memory-management new-operator self-destruction delete-operator

225
推荐指数
8
解决办法
10万
查看次数