delete(this)



 DEVELOP > c-Plus-Plus > delete(this)

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Rahul"
Date: 03 Feb 2008 07:27:35 AM
Object: delete(this)
Hi Everyone,
I was wondering what the standard says about the following,
class test
{
public: test()
{
delete(this);
}
~test()
{
delete(this);
}
};
int main()
{
test object;
}
Thanks in advance!!!
.

User: "peter koch"

Title: Re: delete(this) 03 Feb 2008 07:35:09 AM
On 3 Feb., 14:27, Rahul <sam_...@yahoo.co.in> wrote:

Hi Everyone,

=A0I was wondering what the standard says about the following,

class test
{
=A0public: test()
=A0 =A0 =A0 =A0 =A0 =A0 {
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0delete(this);
=A0 =A0 =A0 =A0 =A0 =A0 }
=A0 =A0 =A0 =A0 =A0 =A0~test()
=A0 =A0 =A0 =A0 =A0 =A0 {
=A0 =A0 =A0 =A0 =A0 =A0 =A0 delete(this);
=A0 =A0 =A0 =A0 =A0 =A0}

};

int main()
{
=A0test object;

}

=A0Thanks in advance!!!

This is full of undefined behavior, the first one being destruction of
an object that had not yet been fully constructed, the second being
calling delete on something not allocated by new, and the third being
calling the destructor on the same object twice. There might be more,
but technically the first one is all you can talk about,
/Peter
.
User: "Juha Nieminen"

Title: Re: delete(this) 04 Feb 2008 07:48:27 AM
peter koch wrote:

~test()
{
delete(this);
}

and the third being
calling the destructor on the same object twice.

I wonder if that could cause an infinite loop with some compilers.
.
User: "James Kanze"

Title: Re: delete(this) 05 Feb 2008 03:40:33 AM
On Feb 4, 2:48 pm, Juha Nieminen <nos...@thanks.invalid> wrote:

peter koch wrote:

~test()
{
delete(this);
}

and the third being
calling the destructor on the same object twice.

I wonder if that could cause an infinite loop with some compilers.

An infinite loop, only if the compiler does tail recursion
optimization, but an infinite recursion does seem likely, since
the first thing a delete expression does is call the destructor.
Well spotted.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34
.



User: "Rolf Magnus"

Title: Re: delete(this) 03 Feb 2008 07:33:52 AM
Rahul wrote:

Hi Everyone,

I was wondering what the standard says about the following,

class test
{
public: test()
{
delete(this);
}
~test()
{
delete(this);
}
};

int main()
{
test object;
}

It says that this code has undefined behavior. You must not delete objects
that haven't been created using new.
.
User: "James Kanze"

Title: Re: delete(this) 03 Feb 2008 11:13:06 AM
On Feb 3, 2:33 pm, Rolf Magnus <ramag...@t-online.de> wrote:

Rahul wrote:

I was wondering what the standard says about the following,
class test
{
public: test()
{
delete(this);
}
~test()
{
delete(this);
}
};
int main()
{
test object;
}

It says that this code has undefined behavior. You must not
delete objects that haven't been created using new.

Amongst other things. Think of what will happen in a new
expresssion: "new test". I don't think deleting the return
value of new is really a good idea either. And of course, if
you call delete on a pointer to test, you'll end up double
deleting.
Delete this is pretty much a standard idiom, but not from the
constructor or the destructor.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34
.



  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER