Question about delete pointer

This is a discussion on Question about delete pointer within the C++ Programming forums, part of the General Programming Boards category; Hi everyone..
How can i delete a pointer in a member function which return that pointer
for example:
Code:
Node<T>* ...

Are you sure you should delete the pointer? Because you're deleting "head"; which isn't allocated in this function.
If you're sure, simply write "delete ptr;", where ptr is the pointer you want to delete. What exactly are you having trouble with?

The trouble is where can i type delete ptr
after return t11 or before it??
I think the both cases are wrong, I tried in the second case but the program hung maybe because it delete the ptr then retutn it...?

The trouble is where can i type delete ptr
after return t11 or before it??
I think the both cases are wrong, I tried in the second case but the program hung maybe because it delete the ptr then retutn it...?

It is definitely wrong to delete t11 after the return statement since that statement would then be unreachable. It would definitely be wrong to delete t11 just before the return statement since the caller would then be returned a pointer to an object that no longer exists.

What is this member function supposed to do? I gather that it is supposed to find an object that is equal to item. Perhaps what you want to do is to save a pointer to the next element, then delete t11, and then cause t11 to point to that next element so as to return t11.