Question about Effective STL item 7

Discussion in 'C++' started by Piotr, Jan 16, 2006.

  1. Piotr

    Piotr Guest

    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.
     
    Piotr, Jan 16, 2006
    #1
    1. Advertising

  2. Piotr

    Pete Becker Guest

    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)
     
    Pete Becker, Jan 16, 2006
    #2
    1. Advertising

  3. 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
     
    Victor Bazarov, Jan 16, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Don Kim
    Replies:
    9
    Views:
    554
    Gregg
    May 23, 2004
  2. Matthias
    Replies:
    25
    Views:
    997
    Thorsten Ottosen
    Feb 1, 2005
  3. John
    Replies:
    4
    Views:
    648
    Ioannis Vranos
    Apr 27, 2005
  4. Replies:
    1
    Views:
    425
    benben
    Aug 7, 2005
  5. Replies:
    4
    Views:
    425
Loading...

Share This Page