James Kanze
10/6/2008 12:31:00 PM
On Oct 4, 4:52 pm, Pete Becker <p...@versatilecoding.com> wrote:
> On 2008-10-04 09:03:30 -0400, "bashill....@gmail.com"
> <bashill....@gmail.com> said:
> > Ptr_to_T& operator++(int){//postfix
> > cout << "Entering Operator++" << endl;
> > T* temp = _p;
> > _p += 1;
> > return *this;
> > }
> This operator increments *this then returns it. What it should
> do is make a local copy of *this, increment *this, and return
> the local copy by value. Typically, that looks like this:
> my_iterator my_iterator::operator++(int)
> {
> my_iterator temp(*this);
> ++*this;
> return temp;
> }
This is, of course, the difference he was complaining about, but
there is another significant difference: the built-in operator++
requires an lvalue; this overload doesn't. If you really want
to be as close as possible to the built-in types (and think that
it makes a difference), you should define the operator as a
non-member, taking a non-const reference.
Of course, even then, it introduces sequence points which aren't
present with a built-in operator:-).
(Seriously, the rule of making a user defined operator behave as
closely as possible to what the built-in operator does is really
only applicable in cases where the built-in operator would be
usable. Trapping accidental use where the built-in operator
would cause an error is a lot less important.)
--
James Kanze (GABI Software) email:james.kanze@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