It's important to make a distinction between:
1. "Pointer" as a variable residing on the stack. The name tempb refers to one such variable.
2. "Pointer" as an integer value that refers to a memory location. The variable tempb refers to holds such a value.
One should delete every newed pointer of the second meaning. This is incorrect:
1 2 3 4
|
int *foo=new int;
int *bar=foo;
delete foo;
delete bar;
|
foo and bar hold the same pointer. Deleting them both deletes the same pointer twice.
This is why the concept of pointer ownership is also important. An object is said to own a pointer if it assumes the responsibility of freeing it. Pointer ownership is implicitly implemented in the way you structure your code. For example, here:
1 2 3
|
int *foo=new int;
int *bar=foo;
delete bar;
|
foo transfers the ownership of its pointer to bar, and here:
1 2 3
|
int *foo=new int;
int *bar=foo;
delete foo;
|
the pointer is copied without ownership transfer.
Since bottom_ is a variable that will eventually get deleted (in CStack::~CStack(), presumably), it would be wrong to delete tempb. There's an ownership transfer on line 19.