Can I use "delete this"?

Asked 1 years ago, Updated 1 years ago, 62 views

Can I write delete this; when I delete an instance of the class I created with new at the end?

Write it like this.

void doStuff()
{
    // This object is a working module
    // When you want to switch to another module,

    controller->setWorkingModule(new OtherModule());

    // The other module calculates it
    // Organize this
    delete this;
}

Or can I make otherClass kill this instead?

void doStuff()
{
    otherClass.deleteMe(this); // void deleteMe(void *obj) { delete obj; }
}

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

2022-09-21 17:20

1 Answers

delete this;Writing together is called suicide The suicide of the object should be written with caution, but to summarize the contents of The C++ FAQ Lite simply,

You should only write if you are 100% sure of something.


2022-09-21 17:20

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.