Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!samsung!usc!apple!agate!e260-2d!c60c-2ca From: c60c-2ca@e260-2d.berkeley.edu (Andrew Choi) Newsgroups: comp.lang.c++ Subject: Re: conversions Message-ID: <1990Apr14.061103.23472@agate.berkeley.edu> Date: 14 Apr 90 06:11:03 GMT References: <7633@cadillac.CAD.MCC.COM> Sender: usenet@agate.berkeley.edu (USENET Administrator;;;;ZU44) Reply-To: c60c-2ca@e260-2d (Andrew Choi) Organization: University of California, Berkeley Lines: 50 In article <7633@cadillac.CAD.MCC.COM> vaughan@puma.cad.mcc.com (Paul Vaughan) writes: > >The following program compiles cleanly under g++-1.37.1, but gets >three errors under cfront (Sun CC, actually). > >--------------------------- >class Int { > int i; >public: > Int(int j) : i(j) {} > Int(Int& I) { i = I.i; } > operator int() { return i; } > operator int&() { return i; } >}; > >main() { > Int i = 4; > i += i; // line 29 >} > >------------------------ >CC -c convert.cc >CC convert.cc: >"convert.cc", line 30: error: ambiguous conversion of Int >"convert.cc", line 30: error: ambiguous conversion of Int >"convert.cc", line 30: error: bad operand types Int Int for += >3 errors The code is incorrect because the user-defined conversion, namely, "operator int()" is ambiguous. Also, this conversion is not applied in line 29, resulting in the third error message. For the first error, please consider the following: Int i; i = i + 3; For the second instance of i (the r-value), either "operator int()" or "operator int&()" can be applied to convert "i" to an integer. For the second error, the conversion "operator int()" is not applied because there is no indication that it should be invoked since no integer is involved in that expression. Rather, the compiler is looking for the function "operator +=(Int &, Int)". Andrew Choi Internet Address: c60c-2ca@web.berkeley.edu Standard Disclaimer