Connecting Tech Pros Worldwide Help | Site Map

Return an iterator

 
LinkBack Thread Tools Search this Thread
  #1  
Old January 25th, 2006, 04:35 PM
Allerdyce.John@gmail.com
Guest
 
Posts: n/a
Default Return an iterator

Hi,

When I return an iterator, should I return an iterator, or should I
return a reference to the iterator?

for example:
vector<A> aVector;

Should it be:
vector<A>::Iterator getBeginIterator() {
aVector.begin();
}

or

vector<A>::Iterator& getBeginIterator() {
aVector.begin();
}


  #2  
Old January 25th, 2006, 04:45 PM
Pete Becker
Guest
 
Posts: n/a
Default Re: Return an iterator

Allerdyce.John@gmail.com wrote:[color=blue]
> Hi,
>
> When I return an iterator, should I return an iterator, or should I
> return a reference to the iterator?
>
> for example:
> vector<A> aVector;
>
> Should it be:
> vector<A>::Iterator getBeginIterator() {
> aVector.begin();
> }
>
> or
>
> vector<A>::Iterator& getBeginIterator() {
> aVector.begin();
> }
>[/color]

What happened when you tried it? (Hint: one of these has a serious
error, and understanding that error will answer your question)

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
  #3  
Old January 25th, 2006, 06:45 PM
Alan Johnson
Guest
 
Posts: n/a
Default Re: Return an iterator

Alan Johnson wrote:[color=blue][color=green]
>> for example:
>> vector<A> aVector;
>>
>> Should it be:
>> vector<A>::Iterator getBeginIterator() {
>> aVector.begin();
>> }
>>[/color]
>
> Except for capitalization, this is correct.[/color]

And, perhaps, the fact that you didn't actually include a return statement.

-Alan
  #4  
Old January 25th, 2006, 06:45 PM
Alan Johnson
Guest
 
Posts: n/a
Default Re: Return an iterator

Allerdyce.John@gmail.com wrote:[color=blue]
> Hi,
>
> When I return an iterator, should I return an iterator, or should I
> return a reference to the iterator?
>
> for example:
> vector<A> aVector;
>
> Should it be:
> vector<A>::Iterator getBeginIterator() {
> aVector.begin();
> }
>[/color]

Except for capitalization, this is correct.
[color=blue]
> or
>
> vector<A>::Iterator& getBeginIterator() {
> aVector.begin();
> }
>[/color]

If you have a reference, it must refer to some object. To what object
does the returned reference refer? If your answer is: The iterator
returned by aVector.begin(), then what happens to that iterator when the
getBeginIterator() function ends?

-Alan
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

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 220,989 network members.