Skip to main content
gamedev.net
🔒 Locked

delete this

Started by Corrail Aug 6, 2003 at 3:21 PM 13 replies 2.3k views
Original Post
Corrail
Corrail
Hi all! Just wanted to know if this command is legal in a method of a class of struct: delete this; Thanks a lot Corrail corrail@gmx.at ICQ#59184081
--------------------------------------------------------There is a theory which states that if ever anybody discovers exactly what the Universe is for and why it is here, it will instantly disappear and be replaced by something even more bizarre and inexplicable.There is another theory which states that this has already happened...
OrangyTang
OrangyTang
I can''t remember the exact details, but it is legal - providing you don''t touch any member variables or functions after the call. You''ve got to be careful that the caller knows that its about to happen as well usually so it doesn''t assume the object still exists.

In MFC for example, there is a couple of points where its valid to do this if you are going to since the object will be discarded afterwards.
Corrail
Corrail
Okay, thanks. I''ll try it.

Corrail
corrail@gmx.at
ICQ#59184081
--------------------------------------------------------There is a theory which states that if ever anybody discovers exactly what the Universe is for and why it is here, it will instantly disappear and be replaced by something even more bizarre and inexplicable.There is another theory which states that this has already happened...
antareus
antareus
Object-oriented suicide as I like to call it.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
Russell
Russell
I used delete this; to write a binary tree class once, and it worked fine. If you did delete this; it would call the destructor, which called the child node destructors, which contained delete this;, and so on, so you could deallocate a node and all of it''s sub-nodes with one statement: delete this;
Sneftel
Sneftel
Remember that you should only ever do this if you KNOW the object was allocated with new, rather than being allocated on the stack or statically/globally.


How appropriate. You fight like a cow.
Fredrik Dahlberg
Fredrik Dahlberg
And how do you know how the object was allocated?

My opinion: C++ should be more strict. Doing stuff like this is not really OO and it doesn''t really serve a purpose.
antareus
antareus
C++ is *NOT* merely an OO language! It supports it, but it doesn''t force you into it!

Its an important distinction. If it was an OO language you''d be forced to use classes everywhere.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
Sneftel
Sneftel
quote:
Original post by Fredrik Dahlberg
And how do you know how the object was allocated?

If the constructor is private and creation methods only ever do dynamic allocation, you know.

quote:
My opinion: C++ should be more strict. Doing stuff like this is not really OO and it doesn''t really serve a purpose.

It is really OO, and it does really serve a purpose. You just haven''t seen any situations where it''s useful yet.


How appropriate. You fight like a cow.
RenderTarget
RenderTarget
Refcounted classes, like COM classes, are typically implemented this way. Since they''re always created on the heap, it''s guaranteed. The Release() method basically looks like


if( -- m_cRefCount == 0 )
delete this;
return m_cRefCount;


Perfectly valid, and good OOP if you know how things have to be allocated (via CoCreateInstance, or whatever), IMO.

I like pie.
[sub]My spoon is too big.[/sub]
superpig
superpig
quote:
Original post by RenderTarget

if( -- m_cRefCount == 0 )
delete this;
return m_cRefCount;




Though of course, if you take that fallthrough approach, you''d probably want to save m_cRefCount to a temporary variable; who knows whether it will still be valid after deletion...

Superpig
- saving pigs from untimely fates, and when he''s not doing that, runs The Binary Refinery.
Enginuity1 | Enginuity2 | Enginuity3 | Enginuity4
Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse
Fredrik Dahlberg
Fredrik Dahlberg
Sneftel: No, suicide is never a solution

Having a private constructor? Ok, give me an example of this.

Ok, it might be useful but there must be better solutions and c++ is not really OO. But it''s like when I see a printf-statement in c++ code, I still go bezerk.
JohnBolton
JohnBolton
quote:
Original post by Corrail
Just wanted to know if this command is legal in a method of a class of struct:

delete this;



It is legal, but it is rarely a good thing to do.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Queasy
Queasy
sorry, i know this is an old thread but someone asked why one would ever use a private constructor. one good example of this is when implementing the singleton design pattern.

-j
Jonathan Makqueasy gamesgate 88[email=jon.mak@utoronto.ca]email[/email]
Sneftel
Sneftel
They''ll be happy to hear that.


"Sneftel is correct, if rather vulgar." --Flarelocke

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.