C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Passing a class object by value and standard behavior.

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Alex Fennell
Guest





PostPosted: Sat Jul 16, 2005 11:38 pm    Post subject: Passing a class object by value and standard behavior. Reply with 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!


[ 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





PostPosted: Sun Jul 17, 2005 8:26 pm    Post subject: Re: Passing a class object by value and standard behavior. Reply with quote



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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.