Topic: Difference in constructor invokation
Author: Alexandre Oliva <oliva@dcc.unicamp.br>
Date: 1997/07/04 Raw View
Martin Lang writes:
> Is there a difference if the object in the following example is
> created by copy construction or by initialization construction?
Since a copy-initialization may involve the copy-constructor, if the
copy-constructor produces any visible effect, or if it does not
produce an exact copy of the copied object, there may be a
difference. However, compilers are allowed to optimize away the
temporary used for copy-initialization, thus performing a direct
initialization, so there may be no visible difference at all.
--
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
Universidade Estadual de Campinas, SP, Brasil
---
[ 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 ]
Author: James Kanze <james-albert.kanze@vx.cit.alcatel.fr>
Date: 1997/07/04 Raw View
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 ]
Author: Martin Lang <Martin.Lang@stgl.sel.alcatel.de>
Date: 1997/07/03 Raw View
Is there a difference if the object in the following example is
created by copy construction or by initialization construction?
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; // ???
A a2(x); // ???
}
---
[ 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
]