Connecting Tech Pros Worldwide Help | Site Map

Return an iterator

Allerdyce.John@gmail.com
Guest
 
Posts: n/a
#1: Jan 25 '06
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();
}

Pete Becker
Guest
 
Posts: n/a
#2: Jan 25 '06

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)
Alan Johnson
Guest
 
Posts: n/a
#3: Jan 25 '06

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
Alan Johnson
Guest
 
Posts: n/a
#4: Jan 25 '06

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
Closed Thread