Just wondering whether an object can self-destruct.
Consider this situation.
An object that extends a thread object.
Session : Thread
{
Session() {}
~Session() {}
ThreadMain()
{
while(!done){
/* do stuff ... */
...
// something sets done = true;
}
~Client();
}
};
void start_session()
{
Session* c = new Session();
Session->Start();
// when I exit here, I've lost my reference to s. But if the object
// self destructs when done, I don't need it right?
}
Somewhere along the way, we have a function called start_session which starts a session. Eventually the session ends.
In the conventional approach I would have to have some sort of list of Session objects placed in that list after calling new.
To clean up the objects I'd have to figure out which ones are finished and call a clea开发者_运维百科nup function later.
I thought it might make more sense if they could just clean up themselves. Can that be done?
Why? why not? better approaches?
you can do "delete this" when the session loop exits
but see https://isocpp.org/wiki/faq/freestore-mgmt
精彩评论