A
Anamika
Hello friends....
can anyone tell me what will happen when we do..."delete this"...
can anyone tell me what will happen when we do..."delete this"...
Hello friends....
can anyone tell me what will happen when we do..."delete this"...
Anamika said:can anyone tell me what will happen when we do..."delete this"...
* Anamika:
The same as happens when you do "delete p" where p is a pointer.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Anamika said:no will it give any error message? or it will act madly?
Victor said:If the object wasn't allocated using 'new', the behaviour is
undefined. If any of object's data members or virtual functions
are accessed after 'delete this', the behaviour is undefined.
Does a regular "delete p" do all that?
P.J. Plauger said:No, but it's not necessarily executed within the object being
deleted. The simple answer is that "delete this" works okay,
provided you sneak away carefully after doing so. Do *not*
execute any virtual functions on the way out the door, for
example -- the v-table ain't there anymore, nor are any of
the member objects.
Juha said:From a technical point of view it's not dangerous to execute code
of a member function of a deleted object as long as no member variable
of the object is accessed.
However, what does the C++ standard say?
Hello friends....
can anyone tell me what will happen when we do..."delete this"...
Victor said:Nothing special about deleting 'this'. The lifetime of the object
ends when its destructor is invoked. What else do you expect it to
say?
Juha said:That code after a "delete this;" is guaranteed to work correctly
as long as 'this' is not accessed (implicitly or explicitly).
I bet it doesn't say that, though, so if we want to ensure complete
compatibility, what should we assume?
Victor said:Correct, unless it's a virtual function.
Default said:Is that guaranteed?
A non-static member function gets a pointer to the
object passed in to it, said pointer now deleted. I think just that
use of the pointer as a function argument causes UB.
function argument causes UB.Default said:Is that guaranteed?
No.
A non-static member function gets a pointer to the object passed in to it,
said pointer now deleted. I think just that use of the pointer as a
Rolf said:
function argument causes UB.
How member functions are implemented by the compiler doesn't matter.
Calling a non-static member function of a deleted object invokes
undefined behavor. The standard explicitly says that.
That code after a "delete this;" is guaranteed to work correctly
as long as 'this' is not accessed (implicitly or explicitly).
I bet it doesn't say that, though, so if we want to ensure complete
compatibility, what should we assume?
No, but it's not necessarily executed within the object being
deleted.
The simple answer is that "delete this" works okay,
provided you sneak away carefully after doing so.
Do *not*
execute any virtual functions on the way out the door, for
example -- the v-table ain't there anymore, nor are any of
the member objects.
From a technical point of view it's not dangerous to execute code
of a member function of a deleted object as long as no member variable
of the object is accessed.
However, what does the C++ standard say?
That code after a "delete this;" is guaranteed to work correctly
as long as 'this' is not accessed (implicitly or explicitly).
I bet it doesn't say that, though, so if we want to ensure complete
compatibility, what should we assume?
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.