Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old August 10th, 2006, 11:55 PM
Joe Van Dyk
Guest
 
Posts: n/a
Default 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
  #2  
Old August 11th, 2006, 12:15 AM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: best practice for returning elements from a container

Joe Van Dyk wrote:
Quote:
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


  #3  
Old August 11th, 2006, 01:55 AM
Daniel T.
Guest
 
Posts: n/a
Default Re: best practice for returning elements from a container

In article <pan.2006.08.10.22.27.13.994163@boeing.com>,
Joe Van Dyk <joe.vandyk@boeing.com wrote:
Quote:
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...
Quote:
Btw, the user shouldn't be able to modify the returned Element at all.
In that case, whatever is returned should be const.
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles