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.