Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Question about Effective STL item 7

Reply
Thread Tools

Question about Effective STL item 7

 
 
Piotr
Guest
Posts: n/a
 
      01-16-2006
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.

 
Reply With Quote
 
 
 
 
Pete Becker
Guest
Posts: n/a
 
      01-16-2006
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)
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      01-16-2006
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
more effective c++ item 31 cfchou@gmail.com C++ 4 09-27-2005 09:15 AM
Effective C++ - item 7 (memory mgt). FBergemann@web.de C++ 1 08-07-2005 12:06 PM
meyers: Item 12: Effective C++ John C++ 4 04-27-2005 04:18 PM
Effective STL Item 4 (size() vs. empty()) Matthias C++ 25 02-01-2005 02:16 PM
Item 13 in Meyer's Effective C++ Don Kim C++ 9 05-23-2004 07:02 PM



Advertisments