 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Alex Fennell Guest
|
Posted: Sat Jul 16, 2005 11:38 pm Post subject: Passing a class object by value and standard behavior. |
|
|
Hi all,
I am doing some experiments with unit testing and as part of that I am
passing a class object by value into a function. When the function returns
the class object's destructor is called as I would expect. What I didn't
expect is that there is no call to the copy constructor or assignment
operator.
I hunted in Stroustrup a bit to no avail and C++FAQs touches on the subject
but does not spell it out clearly. Can anyone enlighten me as to what the
standard says the behavior should be or point me in the direction of a free
copy of the standard for me to look?
Thanks in advance!
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Greg Guest
|
Posted: Sun Jul 17, 2005 8:26 pm Post subject: Re: Passing a class object by value and standard behavior. |
|
|
Alex Fennell wrote:
| Quote: | Hi all,
I am doing some experiments with unit testing and as part of that I am
passing a class object by value into a function. When the function returns
the class object's destructor is called as I would expect. What I didn't
expect is that there is no call to the copy constructor or assignment
operator.
I hunted in Stroustrup a bit to no avail and C++FAQs touches on the subject
but does not spell it out clearly. Can anyone enlighten me as to what the
standard says the behavior should be or point me in the direction of a free
copy of the standard for me to look?
Thanks in advance!
|
The compiler does not always need to copy a class object when a copy of
that object is required.
In this case, a copy of the object is required in order it to be passed
by value in a function call. But if the object being copied (or less
likely, the copied object) is never used again (and only its destructor
will be called), then the compiler can treat the object as its own copy
and simply use the original object as the parameter in the function
call. Doing so of course elminates the temporary and the otherwise
expected copy constructor call.
Greg
[ 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
|
|