Question about Effective STL item 7



 DEVELOP > c-Plus-Plus > Question about Effective STL item 7

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Piotr"
Date: 16 Jan 2006 04:42:55 PM
Object: Question about Effective STL item 7
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.
.

User: "Pete Becker"

Title: Re: Question about Effective STL item 7 16 Jan 2006 04:52:43 PM
Piotr wrote:


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.
--
Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
.

User: "Victor Bazarov"

Title: Re: Question about Effective STL item 7 16 Jan 2006 05:03:32 PM
Piotr wrote:

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
.


  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