473,385 Members | 1,843 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.

best practice for returning elements from a container

Hi,

I have a class that looks like:

class ResultSet
Element* getNext();
// more

Now, I'd like to allow syntax similar to

while( i can grab stuff from the result set)
do something with the next element

What should ResultSet::getNext() return? A pointer to the next result and
NULL if there's no more results? A reference to the next element and
throw an exception if the user tries to getNext() when there's nothing
left? A smart pointer?

Btw, the user shouldn't be able to modify the returned Element at all.

Thanks,
Joe
Aug 10 '06 #1
2 1449
Joe Van Dyk wrote:
I have a class that looks like:

class ResultSet
Element* getNext();
// more

Now, I'd like to allow syntax similar to

while( i can grab stuff from the result set)
do something with the next element

What should ResultSet::getNext() return? A pointer to the next
result and NULL if there's no more results? A reference to the next
element and throw an exception if the user tries to getNext() when
there's nothing left? A smart pointer?

Btw, the user shouldn't be able to modify the returned Element at all.
I don't know of the best practice, but at some point something similar to

ResultSet resultset;
/// ...
ResultSet::Enumerator enumerator(resultset);
while (!enumerator.atEnd()) {
Element const* pEl = enumerator.getNext(); // returns and moves
...
}

worked for me...

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 10 '06 #2
In article <pa****************************@boeing.com>,
Joe Van Dyk <jo********@boeing.com wrote:
Hi,

I have a class that looks like:

class ResultSet
Element* getNext();
// more

Now, I'd like to allow syntax similar to

while( i can grab stuff from the result set)
do something with the next element

What should ResultSet::getNext() return? A pointer to the next result and
NULL if there's no more results? A reference to the next element and
throw an exception if the user tries to getNext() when there's nothing
left? A smart pointer?
There are several ways to do this.

class ResultSet {
public:
template < typename Op >
void doToAll( Op fn ) {
// iterate through each element calling fn( e ) on them
}
};

The caller would:

struct operation {
void operator()( const Element& e ) {
// do whatever on 'e'
}
};

void code( ResultSet& r ) {
r.doToAll( operation() );
}

Another...

class ResultSet {
public:
const Element& current() const;
bool done() const;
void advance();
void reset();
};

void code( const ResultSet& r )
{
for ( r.reset(); !r.done(); r.advance() ) {
// do whatever on 'r.current()'
}
}

And of course, there is the way the standard does it...
Btw, the user shouldn't be able to modify the returned Element at all.
In that case, whatever is returned should be const.
Aug 11 '06 #3

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

Similar topics

20
by: Ken Godee | last post by:
module1 calls a function in module2 module2 starts a thread that calls a function in module3 and then returns to module1 thread finishes and I need the return value from the thread to use in...
0
by: mat | last post by:
I would like to create a schema for a document where I would have complex elements of the sort <entry type='initial'> <reason>...</reason> <t>...</t> </entry> <entry type='other'>...
4
by: David | last post by:
Hello. I am looking for advice on what is "best practice" regarding looping through a form to check its checkboxes and associated data fields. Here is what I am trying to do (Here is the page...
3
by: Ed | last post by:
Hi, I have a WorkUnit class. I will pass a reference to a group of these WorkUnits to other classes in my application. I have chosen to use a vector to hold pointers to these WorkUnits for now....
136
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their...
10
by: rAinDeEr | last post by:
Hi, I am trying to create around 70 tablespaces for around 100 tables.. Am using DB2 UDB 8.2 in Linux environment... This is one i generated through Control centre.... CREATE REGULAR...
16
by: pauldepstein | last post by:
Can anyone give a realistic example of a task for which the list container works better than vector or deque? Paul Epstein
17
by: 2005 | last post by:
Hi In C++, are the following considered best practices or not? - passing aguments to functions (ie functions do not take any arguments ) - returning values using return statement Anything...
19
by: Daniel Pitts | last post by:
I have std::vector<Base *bases; I'd like to do something like: std::for_each(bases.begin(), bases.end(), operator delete); Is it possible without writing an adapter? Is there a better way? Is...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.