471,851 Members | 1,233 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,851 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 1362
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

20 posts views Thread by Ken Godee | last post: by
4 posts views Thread by David | last post: by
136 posts views Thread by Matt Kruse | last post: by
10 posts views Thread by rAinDeEr | last post: by
16 posts views Thread by pauldepstein | last post: by
17 posts views Thread by 2005 | last post: by
19 posts views Thread by Daniel Pitts | last post: by
aboka
reply views Thread by aboka | last post: by

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.