From 921472429052796926
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,41c422257f67d844
X-Google-Attributes: gidf78e5,public
From: James Kanze <james-albert.kanze@vx.cit.alcatel.fr>
Subject: Re: Difference in constructor invokation
Date: 1997/07/04
Message-ID: <rf5hgebkpx3.fsf@vx.cit.alcatel.fr>#1/1
X-Deja-AN: 254541866
References: <33BA74FF.4128@stgl.sel.alcatel.de>
X-Original-Date: 04 Jul 1997 13:38:48 +0200
Organization: -
X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBFAgUBM70DX+EDnX0m9pzZAQFERwGAnSIgVXEeE+niNhivopfd1ODPq/4r3JDU nqsycg277GHPUMf3vRHm1Kx9N1dg6KgA =Mmpv
Newsgroups: comp.std.c++
Originator: fjh@murlibobo.cs.mu.OZ.AU


Martin Lang <Martin.Lang@stgl.sel.alcatel.de> writes:

|>  Is there a difference if the object in the following example is
|>  created by copy construction or by initialization construction?

Maybe.

|>  class A {
|>  public:
|>  	A(int v);
|>  	A(const A& rhs);
|>  private:
|>  	int value;
|>  };
|>  
|>  
|>  class B {
|>  public:
|>  	B(int v);
|>  	operator int()  const;
|>  private:
|>  	int value;
|>  };
|>  
|>  
|>  void f(const B& x)
|>  {
|>  	A a1= x;    // ???

Illegal.

Formally, the rule for this is convert the expression on the right hand
side of the equal sign to an A, then copy construct the object using the
results of this conversion.  The compiler is explicitly allowed to elide
the call to the copy constructor if it wishes, but it must still analyse
the program as if it called the copy constructor.

In this case, you need an A, or something convertible to an A, on the
right side of the equals sign.  But x is a B, which is not convertible
to an A.  (The conversion would involve two user defined conversions.)

|>  	A a2(x);    // ???

This is legal.  The rule is to convert x to something which can be used
as an argument to a constructor for A (not necessarily the copy
constructor).   A has a constructor taking an int, and x (a B) can be
converted to an int.

|>  }

-- 
James Kanze      home:     kanze@gabi-soft.fr        +33 (0)1 39 55 85 62
                 office:   kanze@vx.cit.alcatel.fr   +33 (0)1 69 63 14 54
GABI Software, Sarl., 22 rue Jacques-Lemercier, F-78000 Versailles France
            -- Conseils en informatique industrielle --
---
[ 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                             ]



