Imagine I have a class that allocates memory (forget about smart pointers for now):
class Foo
{
public:
Foo() : bar(new Bar)
{
}
~Foo()
{
delete bar;
}
void doSomething()
{
bar->doSomething();
}
private:
Bar* bar;
};
As well as deleting the objects in the destructor is it also worth setting them to NULL?
I’m assuming that setting the pointer to NULL in the destructor of the example above is a waste of time.
Since the destructor is the last thing that is called on an object before it “dies,” I would say there is no need to set it to
NULLafterwards.In any other case, I always set a pointer to
NULLafter callingdeleteon it.