As you know, in C ++ you can create an object as a global or local variable. In this case, the compiler itself cares about the destruction of the object (when leaving the block or at the end of the program), and it is impossible to call delete against it categorically (otherwise memory may be freed which cannot be freed). And you can create an object using new (we do not consider placement new). In this case, the entire responsibility for freeing the memory falls on the programmer, and at some point he is obliged to make delete with respect to the object, otherwise there will be a memory leak.
Suppose we create an object that knows when it becomes unnecessary and we would like it to delete this in this case. The question is: can an object somehow determine for itself whether it is necessary to make delete or is it selected not from a heap and therefore nothing can be done? Of course, you can pass a flag to the constructor, which will be stored in the object and used during deletion to decide what to do. However, in this case, the programmer becomes responsible for passing the correct value of the parameter depending on how the variable is described, and this is inconvenient. Are there any better solutions?