473,320 Members | 2,024 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 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 1602
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.