Article View: comp.std.c++
Article #3762Re: Constructors and conversion operator
From: Steve Clamage
Date: Thu, 21 Mar 1996 19:16
Date: Thu, 21 Mar 1996 19:16
46 lines
2713 bytes
2713 bytes
In article 2E03@cs.tu-berlin.de, Roman Lechtchinsky <wolfro@cs.tu-berlin.de> writes:
>Steve Clamage wrote:
>>
>> Suppose a copy constructor T::T(const T&) is viewed as a conversion
>> from T to T. If such a conversion is "needed", the shortest conversion
>> sequence must be chosen. That would be the null sequence, and so the
>> copy ctor would never be used or even considered for that purpose.
>> (Just as the sequence int->Rational->float would never be considered in
>> converting an int to a float.)
>
>I'm not too sure here. First, if the copy constructor is the identity
>conversion, why is it called at all? I mean, if the identity conversion is
>eliminated from the conversion sequence, why isn't the call to the copy
>constructor itself eliminated? Second, if the copy constructor is called, why
>is it called only once? Consider
>
>class A { A(const A&); };
>void foo(A);
>A a;
>
>Now, foo(a) is implicitly converted to foo(A(a)). Why not to foo(A(A(a))?
Let's not confuse type conversion with making a copy. In the call foo(a)
no type conversion is required. Function foo requires an A object, and is
invoked with an A object. But foo does require a copy of the actual argument,
no matter what its origin. In this case foo gets a local copy of 'a'.
Example 2:
class B { public: operator A(); };
B b;
foo(b);
We do need a conversion of 'b' to type A, which is available. A temporary A
object is created by "B::operator A()", and a copy of that is passed to foo.
In both examples a copy of an A object is needed, accomplished as always
by the copy constructor.
---
Steve Clamage, stephen.clamage@eng.sun.com
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Technical Details
Message-ID:
<4is5ao$dds@engnews1.Eng.Sun.COM>Path:
arc1-ca.pugleaf.net!chronicle.mti.sgi.com!austernReferences:
<31505061.2E03@cs.tu-berlin.de>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!chronicle.mti.sgi.com!austern From: clamage@Eng.Sun.COM (Steve Clamage) Newsgroups: comp.std.c++ Subject: Re: Constructors and conversion operator Date: 21 Mar 1996 11:16:04 PST Organization: Sun Microsystems Inc. Approved: austern@isolde.mti.sgi.com Message-ID: <4is5ao$dds@engnews1.Eng.Sun.COM> References: <31505061.2E03@cs.tu-berlin.de> Reply-To: clamage@Eng.Sun.COM NNTP-Posting-Host: isolde.mti.sgi.com X-Original-Date: 21 Mar 1996 17:57:44 GMT X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBVAwUBMVGrXUy4NqrwXLNJAQFBKwIAj2dR4J7Y/r3LlWZVD1Y4IhdX5a8o14ri GU291fG+AZVgQE5S1W6tDEkmjfNj+ZstDX5I794cgn+JsVKhN7pYWw== =FrWa Originator: austern@isolde.mti.sgi.com