 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Ivan Novick Guest
|
Posted: Sun Dec 10, 2006 10:10 am Post subject: how conversion constructor works? |
|
|
Hi, (topic is motivated by a topic on comp.lang.c++)
AFAIK, the main function below will use the conversion constructor to
create a temporary object on the right hand side of the assignment
operator and then the copy constructor to initialize the object on the
left hand side of the assignment operator. This is confirmed by the
fact that if you make the copy constructor private, the code in main
won't compile saying it can't access the copy constructor. However I
do not receive a printout from the copy constructor when running the
program.
So the question is how does the code actually work? If the copy
constructor is called it should print, if it is not called then why
does the compiler complain when the copy constructor is made private?
struct thing
{
int data;
thing(int x) : data(x)
{ std::cout << "conversion constructor" << std::endl; }
thing(const thing& cpy) : data(cpy.data)
{ std::cout << "copy constructor" << std::endl; }
};
int main(int argc, char** argv)
{
thing thg = 1;
return 0;
}
thanks,
---
Ivan
http://www.0x4849.net
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
h.yuzhao@gmail.com Guest
|
Posted: Mon Dec 11, 2006 5:11 am Post subject: Re: how conversion constructor works? |
|
|
{ Please don't quote what's not necessary to establish context;
including the signature and the clc++m banner. -mod }
"Ivan Novick дµÀ£º
"
| Quote: | Hi, (topic is motivated by a topic on comp.lang.c++)
AFAIK, the main function below will use the conversion constructor to
create a temporary object on the right hand side of the assignment
operator and then the copy constructor to initialize the object on the
left hand side of the assignment operator. This is confirmed by the
fact that if you make the copy constructor private, the code in main
won't compile saying it can't access the copy constructor. However I
do not receive a printout from the copy constructor when running the
program.
So the question is how does the code actually work? If the copy
constructor is called it should print, if it is not called then why
does the compiler complain when the copy constructor is made private?
I tested your sample under VS2005 with VC8 compliler and run it, only |
the conversion ctor is invoked, and the compliler does not complain any
thing. I think in your code the stuct not class should make all members
public by default. So what you said about complaining won't happen.
BTW, what is your compliler? Maybe this problem was
compliler-dependent, it should be confirmed after refering the
Standard.
| Quote: | struct thing
{
int data;
thing(int x) : data(x)
{ std::cout << "conversion constructor" << std::endl; }
thing(const thing& cpy) : data(cpy.data)
{ std::cout << "copy constructor" << std::endl; }
};
int main(int argc, char** argv)
{
thing thg = 1;
return 0;
}
{ signature and banner stripped -mod }
|
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Thomas Maeder Guest
|
Posted: Mon Dec 11, 2006 5:18 am Post subject: Re: how conversion constructor works? |
|
|
"Ivan Novick" <ivan (AT) 0x4849 (DOT) net> writes:
| Quote: | AFAIK, the main function below will use the conversion constructor to
create a temporary object on the right hand side of the assignment
operator and then the copy constructor to initialize the object on the
left hand side of the assignment operator. This is confirmed by the
fact that if you make the copy constructor private, the code in main
won't compile saying it can't access the copy constructor. However I
do not receive a printout from the copy constructor when running the
program.
|
A C++ implementation is entitled to optimize away the invokation of
the copy constructor. The implementation that you use seems to do
that.
| Quote: | So the question is how does the code actually work? If the copy
constructor is called it should print, if it is not called then why
does the compiler complain when the copy constructor is made
private?
|
Even if the implementation does the optimization, it is still required
to verify the accessibilility of the copy constructor.
The idea is that if o1 is copied into o2, o2 should be equal to o1
(more precisely: equal to o1 before the copying), so optimizing away
the copying shouldn't prevent a program from working correctly.
If the programmer explicitly prevents copying, however, the code
shouldn't be accepted, independently of whether the optimization takes
place or not.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Carlos Moreno Guest
|
Posted: Mon Dec 11, 2006 5:21 am Post subject: Re: how conversion constructor works? |
|
|
vvgzjj (AT) hotmail (DOT) com wrote:
| Quote: | the copy constructor and the assignment operator(=) are different.The
code you write doesn't call the copy constructor but the assignment
operation.
|
This is profoundly wrong --- there is no assignment in the shown code;
there is *initialization*; two very distinct things.
The OP's code requires the *copy constructor*, since the line
Aclass a = b;
Is, by definition, equivalent to:
Aclass a (Aclass(b));
There is the conversion constructor that the OP was talking about, and
then there is copy construction --- as already said by others, while
semantically a requirement (therefore, the code does not compile if
the copy-ctor is not available --- e.g., if it is declared private),
the compiler is allowed to optimize away unnecessary temporaries,
even if the copy-consrtuctor has observable side-effects, and so the
*final* code (after optimization) may be made exactly equivalent to:
Aclass a(b);
(using only the conversion constructor)
HTH,
Carlos
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Carlos Moreno Guest
|
Posted: Mon Dec 11, 2006 5:43 am Post subject: Re: how conversion constructor works? |
|
|
Ivan Novick wrote:
| Quote: | So the question is how does the code actually work? If the copy
constructor is called it should print, if it is not called then why
does the compiler complain when the copy constructor is made private?
|
Optimization, perhaps?
It is a semantic requirement that the copy contructor be available,
because the code invokes it --- now, the compiler is free to decide
how to do things so long as the behaviour is respected; your line
in main has the exact same effect whether the copy-ctor is called
or not; except for the side-effect of the copy-ctor; but the
standard specifically allows any copy constructors to be optimized
away, even if they echibit side-effects with observable behaviour
(like yours, which has the side effect of sending something to
stdout).
Perhaps if you compile it with a compiler option to disable all
optimizations (-Od, perhaps?), maybe your copy-ctor will show up
and display the message.
HTH,
Carlos
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|