Topic: overriding postfix operator


Author: "Prateek" <kprateek88@gmail.com>
Date: Thu, 28 Jul 2005 21:26:18 CST
Raw View
> [...] the operators are designed this way so
> that the same function can be used for both using the above tricks.

I don't think so. One should try to keep the semantics of user-defined
operators like those of the in-built ones ("do as the ints do"). So the
prefix and postfix operators should ideally return a non-const
reference and a const value respectively. For that you will have to
declare two functions, and not the same function with a default
argument:

class X
{
 //...
 public:
  X &operator++();
  const X operator++(int);
 //...
};

> The value passed in to this function is always 0 and there is a nifty
> feature in C++ that lets us use the same function to handle both prefix
> and postfix versions with a single function by taking advantage of
> default arguments:

Both the prefix and the postfix can be handled preserving conventional
semantics and without duplication of code by implementing the postfix
in terms of the prefix:

const X X::operator++(int)
{
 const X tmp(*this);
 ++(*this);
 return tmp;
}

-Prateek

--                                                     --
To iterate is human, to recurse divine. -L. Peter Deutsch
--                                                     --

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: "ScottM" <scott@mayo.name>
Date: 24 Jul 2005 21:40:02 GMT
Raw View
I find people new to C++ tend to avoid overriding the postfix operators
(++, etc.) because the syntax is bizarre. I'm wondering if an alternate
syntax (I realise there is no good syntax for this) might help:

   operator ++      //prefix
   operator ++...   //prefix
   operator ...++   //postfix

where ... is the '...' token, which as far as I can tell can't
otherwise be here.

I find an amusing, gentle irony in that fact that the expression c++ is
curiously hard to override in C++.  :-)

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: "Me" <anti_spam_email2003@yahoo.com>
Date: Sun, 24 Jul 2005 20:52:11 CST
Raw View
ScottM wrote:
> I find people new to C++ tend to avoid overriding the postfix operators
> (++, etc.) because the syntax is bizarre.

What's so hard about it? The basic idea to remember is that the prefix
operator is generally the most efficient one since it modifies the
value in-place as opposed to making a new copy of a value:

// prefix
retT operator++();

Now, since the postfix operator already has to make a copy anyway,
passing in a parameter to a function to specify that it is a postfix
operator should be negligible (and yes, the type of the parameter must
be int):

// postfix
retT operator++(int);

The value passed in to this function is always 0 and there is a nifty
feature in C++ that lets us use the same function to handle both prefix
and postfix versions with a single function by taking advantage of
default arguments:

// both
retT operator++(int = 0);

here it is unnamed and assigned the value 0 since we don't care. But if
we actually do care to disambiguate between prefix or postfix (the
argument can be anything, but it must be non-zero):

// both
retT operator++(int pre = 1);

The reason the operators seem difficult for newbies to figure out is
that they probably don't realize the operators are designed this way so
that the same function can be used for both using the above tricks.

(the above prototypes are all member functions, you can also define
non-member functions in the usual way)

> I'm wondering if an alternate
> syntax (I realise there is no good syntax for this) might help:
>
>    operator ++      //prefix
>    operator ++...   //prefix
>    operator ...++   //postfix
>
> where ... is the '...' token, which as far as I can tell can't
> otherwise be here.

That would be even worse because it adds more functions to look for
during overload resolution.

> I find an amusing, gentle irony in that fact that the expression c++ is
> curiously hard to override in C++.  :-)

I've never had any difficulty.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]