Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > a question of style

Reply
Thread Tools

a question of style

 
 
Tim Partridge
Guest
Posts: n/a
 
      08-01-2003
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

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      08-01-2003
"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


 
Reply With Quote
 
 
 
 
Phlip
Guest
Posts: n/a
 
      08-01-2003
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
>



 
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
DataGrid header style inconsistent with sortable column style cedoucette@alum.rpi.edu ASP .Net 0 10-14-2005 12:13 AM
All style tags after the first 30 style tags on an HTML page are not applied in Internet Explorer Rob Nicholson ASP .Net 3 05-28-2005 03:11 PM
Need help with Style conversion from Style object to Style key/value collection. Ken Varn ASP .Net Building Controls 0 04-26-2004 07:06 PM
Javascript Style Switcher that remebers current site style in use Hardeep Rakhra HTML 8 01-15-2004 08:00 PM
Style sheets, include one style within another (not inheritance) foldface@yahoo.co.uk HTML 1 11-24-2003 01:37 PM



Advertisments