Article View: comp.std.c++
Article #3729Re: Constructors and conversion operator
From: Steve Clamage
Date: Tue, 19 Mar 1996 00:18
Date: Tue, 19 Mar 1996 00:18
48 lines
2104 bytes
2104 bytes
In article 3438@cs.tu-berlin.de, Roman Lechtchinsky
<wolfro@cs.tu-berlin.de> writes:
>
>I have a question about conversion operators and constructors. Consider the
>following:
>
>class X
>{
> X( Y );
>};
>
>class Y
>{
> operator X();
>};
A constructor taking a single argument is a type conversion operator from
the argument type to the class type. The arrangement you show is always
going to lead to ambiguity errors. There are two ways to convert a Y
to an X, and neither is preferred over the other. Operator conversion
functions should normally be avoided, partly for this reason, and partly
because they can wind up being used in surprising circumstances. The
compiler might find a conversion that allows erroneous code to compile.
>Now the second one:
>
>class A
>{
> A(B);
>};
>
>class B
>{
> operator A&();
>};
This is the same situation. There is no syntactic difference between
using a reference or an object. Once again, there are two ways to
make an A out of a B, and neither is preferred.
---
Steve Clamage, stephen.clamage@eng.sun.com
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Technical Details
Message-ID:
<4ikkp8$b5u@engnews1.Eng.Sun.COM>Path:
arc1-ca.pugleaf.net!in2.uu.net!bounce-backReferences:
<314CD29A.3438@cs.tu-berlin.de>Show Full Headers
Complete Article Headers
From: clamage@Eng.Sun.COM (Steve Clamage) Message-ID: <4ikkp8$b5u@engnews1.Eng.Sun.COM> X-Original-Date: 18 Mar 1996 21:32:24 GMT Path: arc1-ca.pugleaf.net!in2.uu.net!bounce-back Date: 19 Mar 96 00:18:48 GMT Approved: fjh@cs.mu.oz.au Return-Path: <daemon@meeker.UCAR.EDU> Newsgroups: comp.std.c++ Subject: Re: Constructors and conversion operator Organization: Sun Microsystems Inc. References: <314CD29A.3438@cs.tu-berlin.de> Reply-To: clamage@Eng.Sun.COM X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBFAgUBMU39eOEDnX0m9pzZAQE0oAF/ei1CZIDT/goTRkAGrf3xVvlMYC+hMp0q XDwgkcEJyLT9FzOYk+SQykyazyxIma3K =oYMh