c++ - Deleting object from its member function -
we have container
object , item
object. item
part of container
. item
member function calls container
function deletes item
.
what happens when container
function deleted item
object returns item
member function? sounds leads undefined behaviour. more elaborate case of delete this;
?
edit:
class item { container* itemcontainer; std::string itemname; void check(void) { bool condition = false; // check condition (not relevant question) if (!condition) { itemcontainer->checkfailed(itemname); } } } class container { std::vector<item*> itemlist; void checkfailed(std::string) { item* targetitem; //find item name delete targetitem; } }
so question was: happens if condition
false , checkfailed
container
called (targetitem
item
check()
function called).
the behaviour defined (or not undefined).
more specifically, behaviour well-defined if object created operator new
, , not used after being delete
d (e.g. [now dangling] pointer dereferenced, non-static member accessed, non-static member function called, etc).
if object used after being deleted, behaviour undefined.
Comments
Post a Comment