Article View: comp.std.c++
Article #1994Re: overload resolution
From: Nathan Myers
Date: Wed, 20 Sep 1995 03:08
Date: Wed, 20 Sep 1995 03:08
38 lines
2034 bytes
2034 bytes
chopps@water.emich.edu (Christian E. Hopps) wrote:
>When figuring out which operator to use is any precedence given to
>operators defined in the class?
struct A { A(int); operator int(); bool operator== ( A&); };
struct B { B(int); operator int(); bool operator== (const B&); };
struct C { C(int); operator int(); };
bool operator==(const C&, const C&);
void f()
{
A a(0);
a == 3; // OK, int(a) == 3 [!!]
3 == a; // OK, 3 == int(a)
B b(0);
b == 3; // error: b == B(3), or int(b) == 3 ?
3 == b; // OK, 3 == int(b)
C c(0);
c == 3; // error: c == C(3), or 3 == int(c) ?
3 == c; // error: 3 == int(c), or C(3) == c ?
}
I believe the remarks above are correct. (Notice [!!] that leaving
out the "const" in A means that conversion isn't attempted, because the
result of an implicit conversion is const.)
This really argues for avoiding conversion loops, so you don't
have to remember the silly rules; and if you must use a type that
has such a loop, explicitly qualify the arguments so you get the
operation you want.
Nathan Myers
myersn@roguewave.com
[ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu.
Contact address: std-c++-request@ncar.ucar.edu. The moderation policy
is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID:
<43npdu$lar@shellx.best.com>Path:
arc1-ca.pugleaf.net!engnews2.Eng.Sun.COM!taumet!clamageReferences:
<MATT.95Sep18124228@physics2.Berkeley.EDU>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!engnews2.Eng.Sun.COM!taumet!clamage From: Nathan Myers <ncm@netcom.com> Newsgroups: comp.std.c++ Subject: Re: overload resolution Date: 20 Sep 1995 03:08:54 GMT Organization: Best Internet Communications Approved: clamage@eng.sun.com (comp.std.c++) Message-ID: <43npdu$lar@shellx.best.com> References: <MATT.95Sep18124228@physics2.Berkeley.EDU> NNTP-Posting-Host: taumet.eng.sun.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Nntp-Posting-Host: ncm.vip.best.com X-Mailer: Mozilla 1.1N (X11; I; SunOS 5.4 sun4m) X-Url: news:MATT.95Sep18124228@physics2.Berkeley.EDU Content-Length: 1088 Originator: clamage@taumet