wrl@apple.com (Wayne Loofbourrow) (10/12/89)
The following is a proposed extension to operator overloading of
operator++ and operator-- :
In current C++, it is possible to overload operator ++. However, it is
not possible to make a distinction between prefix and postfix usage.
This leads to an inability to create user-defined types that
behave just like built in types.
How about allowing declarations of the form:
class complex
{
// ...
public:
// ...
complex operator ++@ (); // prefix ++ operator
complex operator @++ (); // postfix ++ operator
};
complex complex::operator ++@()
{
*this = *this + 1;
return *this;
}
complex complex::operator @++()
{
complex c = *this;
*this = *this + 1;
return c;
}
For compatibility with current C++, defining the operator++ function would
be equivalent to defining both operator ++@ and operator @++ identically.
This should allow all current C++ code to work just fine and also
allow further discrimination when it is desired.
The same extension would apply to operator-- .
What do people think?
Wayne Loofbourrow
Advanced Technology Group
Apple Computer, Inc.
Internet: wrl@apple.comjima@hplsla.HP.COM (Jim Adcock) (10/13/89)
// not to imply that the following is the "right" solution to pre vs post ++,
// --and it doesn't work anyway, BUT, can someone explain to me why 2.0 accepts
// postfix in the following, but not prefix??? ...And why g++ 1.35.x won't
// accept either form???
extern "C" {int printf(char* p, ...);};
class cmpx
{
double re,im;
// ...
public:
// ...
cmpx(double r=0,double i=0):re(r),im(i){}
cmpx& operator=(const cmpx& x){re=x.re; im=x.im; return *this;}
operator double&(){return re;}
};
main()
{
cmpx c,d;
d=c;
printf("%g\n",(double&)(d));
//d=++c; sorry, 2.0 won't accept this
printf("%g\n",(double&)(d));
d=c++;
printf("%g\n",(double&)(d));
d=c++;
printf("%g\n",(double&)(d));
d=c;
printf("%g\n",(double&)(d));
}