c++ - Check if a pointer is NULL after it's object is deleted in other pointer -
i have simple problem this!
class1 *a = new class1(); class1 *b = a; delete a; a= null; now want check if b null (or deleted) also, b point point previously. problematic when want use b, deleted before!
if (b){ //do here } thanks reading!
as have suggested, using shared pointer make easier.
if want raw way, here why *b still points original value of a
when class1 *b=a taking copy of pointer value of a. regardless of a itself, b hangs on original value.
if wanted change along a, need assign reference a, or pointer a pointer
class1 **b = &a; so now, think happens when dereference b, value pointing to?
it point original class1 *. so, once set a=null, have pointer b points value of a, or null
Comments
Post a Comment