只是想知道一个物体是否可以自毁。
考虑一下这种情况。
扩展线程对象的对象。
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 cleanup function later.
我想,如果他们能自己清理一下,可能会更有意义。能做到吗?
为什么?为什么不呢?更好的方法?