c++ - Self destructing objects -
just wondering whether object can self-destruct. consider situation. an object extends thread object. session : thread { session() {} ~session() {} threadmain() { while(!done){ /* stuff ... */ ... // sets done = true; } ~client(); } }; void start_session() { session* c = new session(); session->start(); // when exit here, i've lost reference s. if object // self destructs when done, don't need right? } somewhere along way, have function called start_session starts session. session ends. in conventional approach have have sort of list of session objects placed in list after calling new. to clean objects i'd have figure out ones finished , call cleanup function later. i thought might make more sense if clean themselves. can done? why? why not? better approaches? you can "delete this" when session loop exits but see https://isocpp.org/wiki/faq/freestore-mgmt