Topic: Inialization and user-defined conversion


Author: James Kanze US/ESC 60/3/141 #40763 <kanze@lts.sel.alcatel.de>
Date: 1996/03/06
Raw View
In article <199603051249.PAA00513@boy.nmd.msu.ru> Alexander Krotoff
<krotoff@boy.nmd.msu.ru> writes:

|> I have a confusion with initialization of class objects.
|> Example:

|> class C {
|>  public:
|>  C(int i);
|> };

|> class D {
|>  public:
|>  D(C c);
|> };

|> D d = 1;

|> void f()
|> {
|>  D d = 1;
|> }

|> I assume, that both initializations are wrong, since they require sequence
|> of two user-defined conversions. But some compilers disaggre ;-)

Correct.  This is a difference in the initialization formats: if you
had written `D d( 1 )', they would be legal.  Apparently, some
compilers are not making the distinction.  (The compiler is allowed to
optimize out the temporary and the call to the copy constructor, but
this is just an optimization, and is not allowed to affect the
legality of the program.)

|> Boralnd c++ 4.0: cannot convert "int" to "D"
|> g++ 2.7.2:  no matching funcion for call to "D::D(int)"
|> MS VC 4.0:  no errors
|> Watcom 10.0:  no errors

|> As seems to me only Borland gives adequate diagnostics.

|> Who is right ?

Borland and g++.  While I find the g++ diagnostics acceptable, it's
true that the Borland one is clearer, particularly for a neophyte.

--
James Kanze         Tel.: (+33) 88 14 49 00        email: kanze@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs-Bourgeois, F-67000 Strasbourg, France
Conseils,    tudes et r   alisations en logiciel orient    objet --
                -- A la recherche d'une activit    dans une region francophone
---
[ 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
]