472,989 Members | 2,959 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,989 software developers and data experts.

A question about copy constructors in C++

http://www.rafb.net/paste/results/V3TZeb28.html
In this code, why copy constructor is not called while returning object from
no_arg() . I was trying to find answer in C++ Standard. and there it's
written that

"Whenever a temporary class object is copied using a copy constructor, and
this object and the copy have the same cv-unqualified type, an
implementation is permitted to treat the original and the copy as two
different ways of referring to the same object and not perform a copy at
all, even if the class copy constructor or destructor have side effects"

I tried to changed object typeof y in function no_arg() to const, and still
copy constructor is not called. Can any one explain me, why does compiler
behaves to both objects differently while returning objects from no_arg()
and pass_arg() ?
Jul 12 '06 #1
2 1585
flamexx7 wrote:
http://www.rafb.net/paste/results/V3TZeb28.html
In this code, why copy constructor is not called while returning object from
no_arg() . I was trying to find answer in C++ Standard. and there it's
written that

"Whenever a temporary class object is copied using a copy constructor, and
this object and the copy have the same cv-unqualified type, an
implementation is permitted to treat the original and the copy as two
different ways of referring to the same object and not perform a copy at
all, even if the class copy constructor or destructor have side effects"

I tried to changed object typeof y in function no_arg() to const, and still
copy constructor is not called. Can any one explain me, why does compiler
behaves to both objects differently while returning objects from no_arg()
and pass_arg() ?

The "cv-unqualified type" is the type you get if you ignore the const
and volatile qualifications. Even after you add const, both the source
and destination for every copy being made has a cv-unqualified type of
test_cc, so your compiler can (and apparently does) eliminate the copies.

--
Alan Johnson
Jul 12 '06 #2
Hi, Here are my two cents worth..

One might think that if a function (without parameters) returns a
object by value, a) it would first have to create the local object, b)
then copy construction a return value (with longer lifetime). And c)
finally copy construct this object into the named object that is to
stores the result. That makes one construction and two copy
constructions.

test_cc no_arg(){
test_cc y("y"); // time a) a construction of local object
y.dostuff();
return y; // time b) a copy construction of unnamed temporary
}

main(){
test_cc ret_obj2 = no_arg(); // time c) a copy construction
}

I don't think any compilers actually do the last step. They combine
the b) and c) steps. The compiler knows what object will store the
result from the function, so this information can be passed to the
function as a hidden argument (by some compiler magic). That means the
function can create object ret_obj2 at the time off b) and thereby
remove one temporary.

This still leaves us with one construction and one copy construction.
Compilers using NRVO (Named return value optimization) can remove the
remaining copy construction. Since the compiler knows that object
ret_obj2 will be used to store the result, it can create this object at
time a) instead and thereby remove the remaining temporary. This leaves
us with only one construction.

So why does the second function involves more copy constructions.

test_cc pass_arg(test_cc arg) { // time a) a copy construction
arg.dostuff();
return arg; //time b) a copy construction
}

main(){
test_cc x("x"); // time a-1) a construction of object
test_cc ret_obj1 = pass_arg(x); //time c) a copy construction
}

Counting that gives me three copy constructions and one construction.
That is one more copy construction right there. Because the object at
time off a-1) is already created before the function is called, it
means the object is not a local temporary and the compiler can not
optimize it away. So this version should always have one copy
construction more than the first version. Other than that the same
optimizations should apply. So that leaves us with one construction and
one copy construction.

So that is what i think is going on, I hope it answered your question.

(Note: I could not test the NRVO optimizations since my compiler does
not support it.)

Regards Patrik

Jul 12 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
11
by: cfchou | last post by:
hi, all, i'm reading ch.20 -smart pointers- of . and i'm tring the trule.hpp test. but there's something different than i expect, and i found that's about temp object. so i simplified the...
7
by: Eckhard Lehmann | last post by:
Hi, I try to recall some C++ currently. Therefore I read the "Standard C++ Bible" by C. Walnum, A. Stevens and - of course there are chapters about operator overloading. Now I have a class...
8
by: Jesper | last post by:
Hi, Does the concept "copy constructor" from c++ excist in c#. What is the syntax. best regards Jesper.
15
by: Sam Kong | last post by:
Hello! I got recently intrigued with JavaScript's prototype-based object-orientation. However, I still don't understand the mechanism clearly. What's the difference between the following...
3
by: ennio | last post by:
Hi, i have a doubt i can't solve right now. Your help will be appreciated. I am studying constructors/copy constructors/destructors. I created this a little class (see below). If i comment the...
4
by: Jess | last post by:
Hello, I tried several books to find out the details of object initialization. Unfortunately, I'm still confused by two specific concepts, namely default-initialization and...
2
by: Jeroen | last post by:
Hi all, I wrote a little bit of code to see the behaviour of (copy) constructors of base and derived classes. But I have a question about it. Let me explain by the following...
7
by: Pep | last post by:
I'm getting weird results at the moment so thought I'd rebase my knowledge of c++ storage with your help :) I have a class used as a type in a struct and the struct is handled by a 3rd party...
10
by: JosephLee | last post by:
In Inside C++ object Model, Lippman said there are four cases in which compile will sythesize a default constructor to initialize the member variables if the constructor is absent: 1. there is a...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.