C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

operator++() for iterator

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Alex Vinokur
Guest





PostPosted: Tue Aug 08, 2006 9:10 am    Post subject: operator++() for iterator Reply with quote



Dinkumware, Ltd. - Compleat Libraries Reference
<iterator>
http://www.dinkumware.com/manuals/?manual=compleat&page=iterator.html


======================================
back_insert_iterator::operator++
--------------------------------
back_insert_iterator& operator++();
back_insert_iterator operator++(int);
The member functions both return *this.
======================================

Question-1. Why do operator++'s return not actually value++, but *this?


Question-2. If operator++(int) returns *this, why doesn't it return
reference, but value? In other words, why not
back_insert_iterator& operator++(int)?



======================================
insert_iterator::operator++
--------------------------------
insert_iterator& operator++();
insert_iterator& operator++(int);
The member functions both return *this.
======================================
Here operator++(int) returns reference, not value.


What is the difference between
* back_insert_iterator::operator++(int)
and
* insert_iterator::operator++(int)
in that context?



Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Earl Purple
Guest





PostPosted: Fri Aug 11, 2006 5:43 pm    Post subject: Re: operator++() for iterator Reply with quote



Alex Vinokur wrote:

Quote:
Question-1. Why do operator++'s return not actually value++, but *this?

because the return value can be used in a place where an l-value is
required
Quote:

Question-2. If operator++(int) returns *this, why doesn't it return
reference, but value?

operator++(int) is post-increment, so the value of *this is modified
but the original value is used for the expression. Thus it needs to
make a copy. It is not technically possible to postpone the
modification (for a user-defined type) until after execution of the
statement so this acts as a sort-of workaround.

In other words, why not
Quote:
back_insert_iterator& operator++(int)?

======================================
insert_iterator::operator++
--------------------------------
insert_iterator& operator++();
insert_iterator& operator++(int);
The member functions both return *this.
======================================
Here operator++(int) returns reference, not value.

back_insert_iterator is a cheat though. operator++ here does nothing
(both post-increment and pre-increment), it is just there to allow it
to work in algorithms. So it is ok for it to return *this rather than a
copy, and there is no need to make a copy even for post-increment so
they don't.

Quote:
What is the difference between
* back_insert_iterator::operator++(int)
and
* insert_iterator::operator++(int)
in that context?

None. Nor is there any difference with not calling it at all, but it's
there because algorithms need it. Compilers probably treat it as a
no-op and strip it out altogether.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.