David
Mon Mar 01 16:58:02 CST 2004
"Vicent Soler" <anonymous@discussions.microsoft.com> wrote in
message news:0D8AFB3E-CE40-470D-8F9A-3B0954AAAB51@microsoft.com...
> I'm developing a program with Visual C++ a I'm wondering if I can
write this sentence in Visual C++: 'delete this'. The main objective
of this sentence is to the delete the object that I'm in, does
anyone know if this can cause some errors??
As others have said, use it carefully. But there are some cases
where it is very useful indeed. A useful way of getting rid of
(eg) a modeless dialogue splash panel (derived from CDialog) can
involve something like:
void CDgSplashPanel::PostNcDestroy()
{
CDialog::PostNcDestroy();
delete this;
// Don't try anything else here!
return;
}
[ To quote Microsoft on the PostNcDestroy method:
Called by the default OnNcDestroy member function after the window
has been destroyed.
virtual void PostNcDestroy( );Remarks
Derived classes can use this function for custom cleanup such as the
deletion of the this pointer.]
Dave
--
David Webber
Author MOZART the music processor for Windows -
http://www.mozart.co.uk
For discussion/support see
http://www.mozart.co.uk/mzusers/mailinglist.htm