Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!ucbvax!hplabs!hp-pcd!hplsla!jima From: jima@hplsla.HP.COM (Jim Adcock) Newsgroups: comp.lang.c++ Subject: Re: coercion of x op= y verses x = x op y ??? Message-ID: <6590294@hplsla.HP.COM> Date: 11 Oct 89 18:26:50 GMT References: <6590279@hplsla.HP.COM> Organization: HP Lake Stevens, WA Lines: 31 // okay, okay -- the following "works" -- I don't know *why* I couldn't figure // out how to do this earlier. Unfortunately, now you can't explictely // coerce to (int), as may be "required" to send Int to a "C" routine like // printf. Instead you have to coerce to (int&) -- So now users have to // remember if a class defines an (int) coercion or and (int&) coercion. // [Its not practical to have both, since that results in mega-ambiguities] // -- And no, I'm not really writing an "Int" class. "Int" is just a // metaphor for writing C++ classes supporting "as-if" polymorphism. For // example, one might want to write an "IntStack" where the top-of-stack can // be used "as-if" it were an int. extern "C" {int printf(const char* const p, ...);}; class Int { private: int i; public: Int(int I=0):i(I){} operator int&(){return i;} }; void main() { Int I=10; printf("%d\n",(int&)I); I = I * I; // okay by both g++ 1.5.x and AT&T 2.0 printf("%d\n",(int&)I); I *= I; // g++ 1.35.x accepts, 2.0 accepts it too printf("%d\n",(int&)I); }