I’ve seen lots of code like this:
SomeType* ptr = NULL;
ptr = SomeMethod(some, params);
What’s the point? I’ve also seen it where ptr is declared somewhere else (for example in a class definition) and then in the class constructor there’d be this:
ptr = NULL;
ptr = SomeMethod(some, params);
I don’t understand why this is done. Surely the ptr = NULL line is useless?
if “SomeMethod” throws an exception your SomeType* will keep pointing to something you don’t want it to point to. Hence it is definitely a good practice to set pointer to null if you don’t want it to point to the old thing anymore.