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
.