From 7003909584734566671
X-Google-Thread: f78e5,d807f402b53e9296
X-Google-Attributes: gidf78e5,public
X-Google-Language: ENGLISH,ASCII-7-bit
Path: g2news1.google.com!news4.google.com!news.glorb.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local01.nntp.dca.giganews.com!nntp.speakeasy.net!news.speakeasy.net.POSTED!not-for-mail
NNTP-Posting-Date: Sun, 24 Jul 2005 21:00:04 -0500
Return-Path: <devnull@stump.algebra.com>
X-Authentication-Warning: mulga.cs.mu.OZ.AU: fjh set sender to devnull@stump.algebra.com using -f
X-Robomod: STUMP, ichudov@algebra.com (Igor Chudov)
Delivered-To: std-c++@ucar.edu
From: "Me" <anti_spam_email2003@yahoo.com>
Newsgroups: comp.std.c++
Subject: Re: overriding postfix operator
Organization: http://groups.google.com
Message-ID: <1122251322.470361.23170@g44g2000cwa.googlegroups.com>
References: <1122152522.719764.325240@z14g2000cwz.googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="iso-8859-1"
X-Complaints-To: groups-abuse@google.com
User-Agent: G2/0.2
Complaints-To: groups-abuse@google.com
Injection-Info: g44g2000cwa.googlegroups.com; posting-host=64.30.198.174;
   posting-account=KFz-JA0AAACBYC450zUdi4wesIaXcwmB
X-Virus-Scanned: amavisd-new at ucar.edu
X-Virus-Scanned: amavisd-new at cs.mu.OZ.AU
Approved: Fergus Henderson <fjh@cs.mu.oz.au>, moderator of comp.std.c++
X-Virus-Scanned: amavisd-new at cs.mu.OZ.AU
Date: Sun, 24 Jul 2005 20:52:11 CST
Lines: 65
NNTP-Posting-Host: 65.182.171.162
X-Trace: sv3-jP3sRdzytKGzYropGNsLJvCz3dAGlArK3EmOuMFj/x5DVGebTjLkkdVGRLU6sADL1JwCPOpXKSS1+HE!RkplIzh38EllL/AP7VYsPWvnmumc3weA/RBMcLZroQng6gCGHjkrD3/hP6f3cvf5WI/h1MSuGPjI!joGADK22xHThHuViyUKq5UK+r+EZKw+G8oz2YSoq8g==
X-Complaints-To: abuse@speakeasy.net
X-DMCA-Complaints-To: abuse@speakeasy.net
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.32
Xref: g2news1.google.com comp.std.c++:1473

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                       ]



