469,575 Members | 1,662 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,575 developers. It's quick & easy.

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 1281
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
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.