"delete this" question

W

WaterWalk

Hello. The question about "deleting this inside the class's member
function" is discussed many times in this group and in the "C++
FAQs". But I still have two more questions.

1. Take the following class as an example:
class Test
{
public:
static void print_message(char *s) { printf("%s\n", s); }
void delete_me()
{
delete this;
print_message("deleted!"); // safe and legal?
}
};

The question is: after "delete this" inside a member function, is it
legal to call a static member function of that class? And what does
the standard says about this?

2. Where in the standard can I find information about the "delete
this" thing? When viewing discussion about "delete this", I often
found people saying "it is legal" or "it conforms to the standard".
But after some quick reading in C++ standard 98, I still can't find
any clue. So I'm just curious where the relevant information can be
found in the standard.
 
A

Alf P. Steinbach

* WaterWalk:
Hello. The question about "deleting this inside the class's member
function" is discussed many times in this group and in the "C++
FAQs". But I still have two more questions.

1. Take the following class as an example:
class Test
{
public:
static void print_message(char *s) { printf("%s\n", s); }
void delete_me()
{
delete this;
print_message("deleted!"); // safe and legal?
}
};

The question is: after "delete this" inside a member function, is it
legal to call a static member function of that class?

Legal, depends on the country. Valid, yes. A static member function
requires no '*this' object, so it doesn't matter that some object has
been destroyed -- objects are destroyed all the time.

And what does
the standard says about this?

"It's OK." §666.42/3.14.

2. Where in the standard can I find information about the "delete
this" thing?

Not directly, but that it's Undefined Behavior to use an object after
it's been destroyed. Look for discussion about lifetime.

When viewing discussion about "delete this", I often
found people saying "it is legal" or "it conforms to the standard".
But after some quick reading in C++ standard 98, I still can't find
any clue. So I'm just curious where the relevant information can be
found in the standard.

See above. And perhaps other will also invest the time to seek out the
relevent paragraphs for you.

Cheers, & hth.,

- Alf
 
A

Alf P. Steinbach

* WaterWalk:
Thanks. But what is "§666.42/3.14". I can't find that section(if it
is) in my copy of ISO/IEC 14882:1998.

It's the "Alf's too lazy to look it up, but just follow hints given
below" paragraph. :)

Cheers,

- Alf
 
W

WaterWalk

And one more question: What about "delete this" in a class's
constructor even if "delete this" is the last statement in that
constructor? Like:
class Test
{
public:
Test() { m=1; delete this;} // is this ok?
private:
int m;
};

Does the "delete obj" expression require the obj to be fully
constructed?
 
A

Alf P. Steinbach

* WaterWalk:
And one more question: What about "delete this" in a class's
constructor even if "delete this" is the last statement in that
constructor? Like:
class Test
{
public:
Test() { m=1; delete this;} // is this ok?

Consider how this constructor would be used. The formal side of things
isn't important here.

private:
int m;
};

Does the "delete obj" expression require the obj to be fully
constructed?

When you have figured out the above you also have the answer to this
question.

Hm, this starts to look even more like a HOMEWORK assignment... If it
isn't homework, what is the actual problem you're trying to solve?

Cheers,

- Alf
 
W

WaterWalk

* WaterWalk:


Consider how this constructor would be used. The formal side of things
isn't important here.



When you have figured out the above you also have the answer to this
question.

Hm, this starts to look even more like a HOMEWORK assignment... If it
isn't homework, what is the actual problem you're trying to solve?

I encountered "delete this" several times, so I decided to take a
close look at it. I don't think it's useful in practice. I asked it
just to gain a whole view of it. Sometimes it can be fun to
investigate those trivial pieces. Maybe this question is so naive that
it starts to look like a homework assignment. (a joke)
 
O

owebeeone

I encountered "delete this" several times, so I decided to take a
close look at it. I don't think it's useful in practice....

"delete this" is commonly used in reference counted objects (where the
reference count is part of the object - sometimes called intrusive
reference counting). The Austria C++ reference counted base classes
use "delete this" as well as a number of other libraries.
 
J

James Kanze

Formally, it's undefined behavior to call delete on an object
before the constructor has terminated. Practically, that won't
be a problem here, but...

This will be.
I encountered "delete this" several times, so I decided to
take a close look at it. I don't think it's useful in
practice.

I don't know what you mean about it not being useful in
practice. In many application domains, most deletes are of
this; if you have an entity object which manages its own
lifetime (which is the case of most entity objects), then delete
this is the most natural way of doing it.
 
J

James Kanze

"delete this" is commonly used in reference counted objects
(where the reference count is part of the object - sometimes
called intrusive reference counting).

I don't know that it's that common in this idiom. In this case,
you do have a natural owner outside of the class who can also
call delete; depending on how the class is designed, there can
even be advantages to doing so. (My RefCntPtr used a delete
this until I modified it to support conversions between derived
and base. This meant that the base having the counter couldn't
be a template, and thus didn't know the type of the class for
which it was reference counting. Since I didn't want to impose
a virtual destructor when inheritance wasn't involved, I moved
the delete out to the pointer, which did know the derived type.)
 
S

subramanian100in

I don't know what you mean about it not being useful in
practice. In many application domains, most deletes are of
this; if you have an entity object which manages its own
lifetime (which is the case of most entity objects), then delete
this is the most natural way of doing it.

Kindly explain what is an entity object. If possible give an example.

Thanks
V.Subramanian
 
J

James Kanze

Kindly explain what is an entity object. If possible give an example.

Don't you have Google. It's a standard design concept. The
classical definition that I find on the net is: "Entity objects
are classes that encapsulate the business model, including
rules, data, relationships, and persistence behavior, for items
that are used in your business application." Except that it's a
bit constraining, since the concept applies outside of business
applications as well. (It doesn't necessarily apply everywhere,
of course, and I can imagine that some numerics applications
have no entity objects.) It also fails to mention identity,
which I find to be an important concept of entity objects.

A simple example might be a class modeling a bank account. (The
term "model objects" is also sometimes used, in the sense that
the objects are part of your data model.)
 
W

WaterWalk

Formally, it's undefined behavior to call delete on an object
before the constructor has terminated. Practically, that won't
be a problem here, but...


This will be.


I don't know what you mean about it not being useful in
practice. In many application domains, most deletes are of
this; if you have an entity object which manages its own
lifetime (which is the case of most entity objects), then delete
this is the most natural way of doing it.

Maybe I am wrong here. I'm still relatively new to C++ programming.
 

Ask a Question

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.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,600
Members
45,179
Latest member
pkhumanis73
Top