Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Passing a class object by value and standard behavior.

0 views
Skip to first unread message

Alex Fennell

unread,
Jul 16, 2005, 7:38:51 PM7/16/05
to
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! ]

Greg

unread,
Jul 17, 2005, 4:26:42 PM7/17/05
to
Alex Fennell wrote:
> 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

0 new messages