是否允许 delete this;
如果 delete 语句是将在该类实例上执行的最后一条语句?当然,我确信 this
指针表示的对象是 new
ly-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;
}
我可以这样做吗?
最佳答案
C++ FAQ Lite 有专门针对此的条目
我认为这句话很好地概括了它
As long as you're careful, it's OK for an object to commit suicide (delete this).
关于c++ - C++ 允许使用 "delete this"吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49593571/