"Daniel T." <po********@eat hlink.net> wrote in message news:<po******* *************** ********@news03 .east.earthlink .net>...
"solosnake" <solosnake@solo snake._remove_t his_.fsnet.co.u k> wrote:
Whilst browsing Flipcode I noticed this code:
class CConsole: public I_TextOutput
{
public:
...
void Release() { delete this; }
...
};
The 'delete this;' caught my eye. Is such code safe?
Can you guarantee that no CConsole object will ever be allocated on the
stack?
It also can't be an element in array, ie., can't have:
a = new CConsole()
and then use Release() on a member of the array. Ie.,
dangerous to say:
a[0].Release()
You also shouldn't use Release() on a CConsole instance where the
instance is a static variable, global, function or class scope. Finally,
can't use it where the CConsole was a member variable. Ie.,
class Foo
{
CConsole o;
};
Anyway, use of delete this is common with reference counting
schemes where the count is maintained with the actual object.
Use of auto_ptr is not always a replacement for reference
counting.