On Jun 15, 3:03 pm, Jess <w...@hotmail.comwrote:
I learned that if we do "v.end()", then the returned iterator is a
temporary object and hence cannot be changed like
--v.end();
It depends on the type of the iterator. The above will work
with most iterators (because operator--() is a member function,
and you can call member functions on a temporary), but not with
all (because an iterator can be just a pointer, and the built-in
operator -- doesn't work on temporaries).
Why is the returned iterator a temporary pointer? I think when the
result of a function is returned, then a non-temporary object is
created, with value copied from the local temporary object. If the
code above still gets us a temporary object, then I think the non-temp-
object-creation hasn't taken place yet.
I'm not sure I follow what you're trying to say. The return
value of a function is always a temporary. If you use the
function to initialize an object, the non-temporary object is
initialized by copying this temporary. (In specific cases, the
compiler is allowed to suppress the additional object, as an
optimization. Don't worry about this for now.)
Does it only happen if we do
p = v.end();
ie. assign v.end() to some variable?
Roughly speaking, named objects are not temporaries, unnamed
objects are. If you write:
iterator p = v.end() ;
p is not a temporary. The results of v.end() are, and the
non-temporary p is initialized with a copy of this temporary.
It is also said that we can't modify it because it has a built in
type. I guess "build-in" means pointer type here. This sounds to me
as if we could modify the temporary object if it is of class type.
I'm quite confused....
Understandably, in this case.
The rules for the built-in operators involve something called
lvalue-ness: the resuls of an expression are either an lvalue,
or they are not. Certain built-in operators, like -- require an
lvalue. A function call is an lvalue if and only if the return
type is a reference. (As a first approximation, temporaries are
not lvalues, everything else is. But this is very approximate;
the standard uses a different language here for a reason.)
User overridden operators are functions, and obey the rules for
function calls, not the rules for the operator they are
overriding. Thus, if operator-- is a user defined override,
then whether it can be called depends on the rules for calling
functions. If it is a member function (usually the case), then
it can be called on a temporary, even if the function is
non-const (which it definitely should be). If it is a free
function, taking a non-const reference, it can't be.
So, if the iterator returned from v.end() is a pointer, or is a
class type with a free function operator--, the expression
"--v.end()" is illegal; if the iterator is a class type with a
member operator--, it is legal.
--
James Kanze (Gabi Software) email:
ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34