Connecting Tech Pros Worldwide Help | Site Map

Return an iterator

Allerdyce.John@gmail.com
Guest
 
Posts: n/a
#1: January 25th, 2006, 05:35 PM
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: January 25th, 2006, 05:45 PM

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: January 25th, 2006, 07:45 PM

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: January 25th, 2006, 07:45 PM

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem overloading the ++ operator of an iterator mkborregaard answers 4 October 22nd, 2007 07:35 PM
What makes an iterator an iterator? Steven D'Aprano answers 27 April 20th, 2007 04:35 AM
How can i return an iterator from a fucntion? TOMERDR answers 6 May 22nd, 2006 10:25 PM
Return an iterator? Markus Dehmann answers 6 July 22nd, 2005 06:47 PM