Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Corrail

delete this

This topic is 5260 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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;

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 5260 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Guest
This topic is now closed to further replies.

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!