Class selfdestruction

Started by
12 comments, last by uncutno 21 years, 11 months ago
The technique of class deleting itself is used in COM. The one reason it is safe to do so in COM is because clients do not explicitly create the object via its constructor. Clients use a class factory interface to request that an object be created. This allows the component to create the object any way it desires. When the reference count of the object reaches 0, the object releases itself from memory (typically with delete this; in C++)

If you really want a managed "pool" of objects you should override new/delete in your class(es).

Dire Wolf
www.digitalfiends.com
[email=direwolf@digitalfiends.com]Dire Wolf[/email]
www.digitalfiends.com
Advertisement
Magmai Kai Holmlor,

I''d probably make that a protected dtor, not private, or you might have an issue when you try and inherit from that class.

Dire Wolf
www.digitalfiends.com
[email=direwolf@digitalfiends.com]Dire Wolf[/email]
www.digitalfiends.com
well.. an object should only delete itself when it can create itself...

and, can you be born from yourself? no.. needs sort of a mother..

now you say you cant be born from yourself, but you can die from yourself..

yes
but when you''re dead you''re not away.. just useless..

the collection of humans created you (actually, 2 where involfed)
when you''re dead, the collection of humans realise this at some point ("hy you? hy? HELLO? ARE YOU OKAY? WHATS UP WITH YOU?" "hm, he''s dead") and then THEY remove YOU.. not you..

what this means is:
if you create sort of a dynamic world holding your objects, then the world has to clean itself up, not the objects..

"take a look around" - limp bizkit
www.google.com
If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia

My Page davepermen.net | My Music on Bandcamp and on Soundcloud

Please take a read of the C++ FAQ Lite (search for it). It contains many reasons why you shouldn''t do things like this. Apart from it being messy and error prone, you current implementation calls the destructor twice (undefined behaviour).

This topic is closed to new replies.

Advertisement