Question about Effective STL item 7

P

Piotr

I am reading Effective STL item 7

My understanding is, instead of doing this:
tempate <typename T>
struct DeleteObject:
public unary_function<const T*, void> {

void operator() (const T* ptr) const
{
delete ptr;
}
}
we should do this:
struct DeleteObject:
tempate <typename T>
void operator() (const T* ptr) const
{
delete ptr;
}
}
because of this "undefined behavior! Deletion of a derived object via a
base class pointer where there is no virtual destructor
void doSomething() {
depque <SpecialString*> dssp;
...
for_each(dssp.begin(), dssp.end(), DeleteObject<string>());
}

My questions are:
1. Why the 'ehanced' implementation DeleteObject: #2, does not need to
inherit unary_function<const T*, void>?
2. Should all my child classes which implement unary_function<const T*,
void> should be converted to 'enhanced implementation (#2)?

Thank you.
 
P

Pete Becker

Piotr said:
My questions are:
1. Why the 'ehanced' implementation DeleteObject: #2, does not need to
inherit unary_function<const T*, void>?

If the orignal one needs to, then so does the "enhanced" one. But the
"enhanced" one can't, because the type T isn't known at the point where
the class is defined. So the example seems to be inconsistent.
2. Should all my child classes which implement unary_function<const T*,
void> should be converted to 'enhanced implementation (#2)?

Only if they need to. The undefined behavior resulting from deleting an
object of a derived type through a pointer to the base type only occurs
if you in fact derive from the original type, create objects of that
derived type, and delete 'em through pointers to the base type. If you
don't to that, the original version works just fine.
 
V

Victor Bazarov

Piotr said:
I am reading Effective STL item 7

That's good.
My understanding is, instead of doing this:
tempate <typename T>
struct DeleteObject:
public unary_function<const T*, void> {

void operator() (const T* ptr) const
{
delete ptr;
}
}
;
we should do this:
struct DeleteObject:

Did you mean

struct DeleteObject {

??
tempate <typename T>
void operator() (const T* ptr) const
{
delete ptr;
}
}
;
because of this "undefined behavior! Deletion of a derived object via a
base class pointer where there is no virtual destructor

Your punctuation leaves a lot to be desired... You open a double quote
and never close it. Is that a quotation from somewhere? Or are you in
a rush to ask your question and can't be bothered to type correctly?
void doSomething() {
depque <SpecialString*> dssp;

What's "depque"? What's "SpecialString"?.. Ah, I see, you're trying to
use the example from the book... OK...
...
for_each(dssp.begin(), dssp.end(), DeleteObject<string>());

Isn't this code flagged in the book as "bad"?
}

My questions are:
1. Why the 'ehanced' implementation DeleteObject: #2, does not need to
inherit unary_function<const T*, void>?

Inheriting 'unary_function' is usually done for the sake of using the
typedefs 'unary_function' has. If they are not used, you don't have to
use them.

Now, think about it. Say, you do want to inherit from 'unary_function'.
Your class is now a true class, so to inherit you need to provide the
template arguments to 'unary_function'. What to provide?
2. Should all my child classes which implement unary_function<const T*,
void> should be converted to 'enhanced implementation (#2)?

No. Yes. How should we know? The item 7 does not talk about using the
template 'unary_function', does it? It talks about deleting objects in
a container of pointers before disposing of the container. Do all your
"child classes" delete something?

V
 

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

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top