a question of style

Discussion in 'C++' started by Tim Partridge, Aug 1, 2003.

  1. If I have an "add" method that adds a pointer to a foo to a private
    std::list, should I define add like

    1. void add( const foo &f ) { lst.push_back( &f ); }

    to keep pointers out of my interface, or should I make it implicit that a
    pointer is being added to the list, like so

    2. void add( foo *f ) { lst.push_back( f ); }

    Would the first method lead the user to believe that their foo is actually
    being stored, potentially leading to a bad memory value in the list once
    the foo is destroyed? Or is this discussion moot as long as I use pre and
    post conditions?

    Tim Partridge
     
    Tim Partridge, Aug 1, 2003
    #1
    1. Advertising

  2. "Tim Partridge" <> wrote...
    > If I have an "add" method that adds a pointer to a foo to a private
    > std::list, should I define add like
    >
    > 1. void add( const foo &f ) { lst.push_back( &f ); }
    >
    > to keep pointers out of my interface,


    Most certainly not. This allows to use temporaries of type 'foo'
    in a call to 'add', and you don't want to store pointers to any
    temporaries in your list.

    > or should I make it implicit that a
    > pointer is being added to the list, like so
    >
    > 2. void add( foo *f ) { lst.push_back( f ); }


    Yes, that's the ticket. You could of course, use a reference to
    non-const 'foo':

    void add(foo& f) { lst.push_back(&f); }

    which is a tad better than the first one because it won't let
    the caller to use a temporary.

    > Would the first method lead the user to believe that their foo is actually
    > being stored, potentially leading to a bad memory value in the list once
    > the foo is destroyed?


    Yes.

    > Or is this discussion moot as long as I use pre and
    > post conditions?


    I suppose it depends on how you set up your conditions and their
    verification.

    Victor
     
    Victor Bazarov, Aug 1, 2003
    #2
    1. Advertising

  3. Tim Partridge

    Phlip Guest

    Tim Partridge wrote:

    > If I have an "add" method that adds a pointer to a foo to a private
    > std::list, should I define add like
    >
    > 1. void add( const foo &f ) { lst.push_back( &f ); }
    >
    > to keep pointers out of my interface, or should I make it implicit that a
    > pointer is being added to the list, like so
    >
    > 2. void add( foo *f ) { lst.push_back( f ); }
    >
    > Would the first method lead the user to believe that their foo is actually
    > being stored, potentially leading to a bad memory value in the list once
    > the foo is destroyed? Or is this discussion moot as long as I use pre and
    > post conditions?


    This is a technical question. The answer is to prefer weaker things to
    stronger ones. Prefer references unless you need a pointer's extra features.
    The only difference at an interface is the pointer could be NULL.

    Can you push a NULL?

    Another answer is symetry. The 'get' or 'pop' methods should, of course,
    return the same type. Can they return a NULL, such as for a "not found"
    situation?

    Finally, you should avoid the implication that the list owns the object, or
    that one can't enlist stack objects.

    --
    Phlip
    http://www.c2.com/cgi/wiki?TestFirstUserInterfaces


    >
    > Tim Partridge
    >
     
    Phlip, Aug 1, 2003
    #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. Rob Nicholson
    Replies:
    3
    Views:
    749
    Rob Nicholson
    May 28, 2005
  2. Replies:
    0
    Views:
    2,466
  3. Replies:
    1
    Views:
    794
    Bertilo Wennergren
    Nov 24, 2003
  4. Hardeep Rakhra
    Replies:
    8
    Views:
    644
    Hardeep Rakhra
    Jan 15, 2004
  5. Ken Varn
    Replies:
    0
    Views:
    472
    Ken Varn
    Apr 26, 2004
Loading...

Share This Page