Self terminate

Started by
8 comments, last by The C modest god 19 years, 8 months ago
I have the following code: class A { public: void Terminate(); A * pSelf; A(); } A::A() { this->pSelf = this; } void A::Terminate () { delete this->pSelf; } A * pObj = new A; pObj->Terminate(); Will it work? or will it be problematic?
It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower.Quote:Original post by Toolmaker Quote:Original post by The C modest godHow is my improoved signature?It sucks, just like you.
Advertisement
well, i hope you'll be far away when it will explode (-:
just imagine you need an array of objects A
That'll work fine, as long as after you delete an object you don't attempt to access any member variables, call member functions that access member variables, or call any virtual member functions.
Oh yeah, and you don't need the A *pSelf member, you can do

delete this;


directly.
It should work fine, although you don't actually need to store a pSelf value. Just use this. A common example of deleting this is in reference-counted classes:
class c_RefCounted{  public:    c_RefCounted() : mRefCount(1) {}    void Attach();    void Detach();  private:    ~c_RefCounted() {}    unsigned long mRefCount;};void c_RefCounted::Attach(){  ++mRefCount;}void c_RefCounted::Detach(){  --mRefCount;  if (mRefCount == 0)    delete this;}

[edit]...I am slow...[/edit]
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
just remember that delete[] must be used when alocated with new[], and delete when alocated with new.
Quote:Original post by Agony
It should work fine, although you don't actually need to store a pSelf value. Just use this. A common example of deleting this is in reference-counted classes:
*** Source Snippet Removed ***
[edit]...I am slow...[/edit]


As you can see from Agony example, if your going to do such things make the destructor private otherwise you may end-up calling delete twice then bang.
What if the Destructor is virtual?
It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower.Quote:Original post by Toolmaker Quote:Original post by The C modest godHow is my improoved signature?It sucks, just like you.
As others have said, delete this; is legit, but dangerous outside of a tightly controlled environment. And yeah, if you're planning on deriving from that class, you'd better make your destructor virtual, otherwise you're in deep doo-doo.

If your outside code is going to call foo->Terminate(), it might as well just call delete foo.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Ok, thanks for the replies.
It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower.Quote:Original post by Toolmaker Quote:Original post by The C modest godHow is my improoved signature?It sucks, just like you.

This topic is closed to new replies.

Advertisement