473,385 Members | 1,282 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

a question of style

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

Jul 19 '05 #1
2 1951
"Tim Partridge" <tj******@lassar.math.uwaterloo.ca> 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
Jul 19 '05 #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, 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

Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: lawrence | last post by:
Can anyone tell me why this code works in Netscape 7.1 but not in IE??? <SCRIPT type='text/javascript'> function makeVisible(nameOfDiv) {...
1
by: Roy | last post by:
Hey all. All I'm trying to do is get a darn double solid bar in my datagrid footer. Doesn't seem to work. Any tips? The weird thing is all the other stylesheet attributes work. If I increase the...
0
by: cedoucette | last post by:
I just wrote code to support sortable columns in a datagrid. It seems to work fine; but, it doesn't look right. The problem is that I have a generic style for links and a different style for the...
1
by: RonY | last post by:
I have a dropdown which calls SetTimePeriod method on change the selection. In the JS function, I reset the field style.display based on what the selection is. This works fine with IE but not working...
8
by: JT | last post by:
Hi, I have done a fair amount of style editing inline in ASP. I'm now using VS 2005 with a standard web project (not Web Application Project). This is my first foray into CSS in a style sheet...
6
by: rongchaua | last post by:
Hi all, I want to change the style of a button. But I don't know how to do it. For example, I have already a button OK on form. I want to add these styles to this button (WS_CHILD || WS_VISIBLE ||...
1
by: Armin Gajda | last post by:
Hi, I have the following problem: An input field get a border assigned by a style class (e.g. 2px solid red). When the field gets the focus, we set the border to green. element.style.border...
0
by: =?Utf-8?B?QXR1bA==?= | last post by:
When .Net 1.0 webservice (VS2003) generates a wsdl - <wsdl:binding name="TestSoap" type="tns:TestSoap"> <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/(note:...
3
by: Michellevt | last post by:
Hi I am working on a project (for college) and wondered if anyone can help me with my problem. In the project we are not allowed to make use of any "style" attributes but "class" attributes...
3
Claus Mygind
by: Claus Mygind | last post by:
I want to move some style setting into a javaScript function so I can make them variable but I get "invalid assignment left-hand side" error? see my question at the bottom of this message. It...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.