473,396 Members | 1,892 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,396 software developers and data experts.

copy constructor and return by value

Hello all,

Even though this topic has been discussed many times, I still need your
help in clearing my confusions. Consider the following code:

class aclass{

public:

aclass(){
cout<<"calling constructor for: "<<this<<endl;
}
aclass(const aclass &b){
cout<<"calling copy constructor for: "<<this<<" copying from:
"<<&b<<endl;
}
void print(){
cout<<"why me?"<<endl;
}
~aclass(){
cout<<"destructor for: "<<this<<endl;
}

};

aclass myf(){
aclass a;
return a;
}

aclass myf2(aclass a){
return a;
}

I have noticed that when I called myf(), from main, no copy constructor
is called. Which I guess is because of the optimization by the
compiler. That is if I run the code as:

myf();
aclass t1 = myf();
aclass t2(myf());

No copy constructor is called. Is there any situation in which copy
constructor will be called for myf() function? what would be the case?

I have also noticed that when I called myf2(..), from main, copy
constructor is called twice (one for the argument passed by value and
one for the return by value) and I am guessing no optimization is done
there. What is the deal here then? What goes in the compiler?

Local object are destructed when the object goes out of the scope. But
in the following situation:

int main(){
myf().print();
}

the destructor is called after the print() function of aclass. What is
the deal?

Thanks for your help...thank for taking time...

Bipod

Jul 29 '05 #1
3 2409
Hi Bipod,
First of all there is no compiler optimization concept in this context.
When you are creating a copy of existing object the copy constructor is
called... And when you are constructing the object in isolation the
normal constructor will be called... I'll try to explain a bit in deep
with the examples that you have mentioned in your post.
1. myf()
inside myf() a object is created from the dust... So normal
constructor gets called.
2. aclass t1 = myf()
In this first a object is created in myf() function which is because
of default constructor. And you are creating a copy of object that is
returned by myf() function. So copy constructor is called which creates
object t1.
3. aclass t2(myf())
t2 is the copy of the object which is returned by myf(). here default
constructor is called when control is inside myf() and copy constructor
is called while creating t2 because t2 is a copy of t1.
4. myf().print()
if you are evaluating an expression some temporary objects gets
created. These gets destructed only after the expression evaluation...
So in this example the destructor is called after print() function is
evaluated.

Please correct me if I am wrong anywhere.

Jul 29 '05 #2
bi***********@gmail.com wrote:
Even though this topic has been discussed many times, I still need your
help in clearing my confusions. Consider the following code:

class aclass{

public:

aclass(){
cout<<"calling constructor for: "<<this<<endl;
}
aclass(const aclass &b){
cout<<"calling copy constructor for: "<<this<<" copying from:
"<<&b<<endl;
}
void print(){
cout<<"why me?"<<endl;
}
~aclass(){
cout<<"destructor for: "<<this<<endl;
}

};

aclass myf(){
aclass a;
return a;
}

aclass myf2(aclass a){
return a;
}

I have noticed that when I called myf(), from main, no copy constructor
is called. Which I guess is because of the optimization by the
compiler. That is if I run the code as:

myf();
OK. A function call. The compiler can optimize away the creation of
the return value since it's not used.
aclass t1 = myf();
OK. Copy-initialisation. Actual copying can be optimized away by the
compiler, and 't1' can serve as the destination for the return value.
aclass t2(myf());
This is a declaration of a function. No object is constructed.
No copy constructor is called. Is there any situation in which copy
constructor will be called for myf() function? what would be the case?

I have also noticed that when I called myf2(..), from main, copy
constructor is called twice (one for the argument passed by value and
one for the return by value) and I am guessing no optimization is done
there. What is the deal here then? What goes in the compiler?
Why don't you ask in a newsgroup dedicated to your compiler? Or make your
compiler generate the assembly code for you and take a closer look at what
the compiler creates...
Local object are destructed when the object goes out of the scope. But
in the following situation:

int main(){
myf().print();
}

the destructor is called after the print() function of aclass. What is
the deal?


The temporary object lives as long as needed, and is destroyed as the last
step in evaluating the _full_expression_ in which it was constructed.

V
Jul 29 '05 #3
Victor Bazarov wrote:
aclass t2(myf());

This is a declaration of a function. No object is constructed.


My bad. It's not a declaration of a function.
[...]

Jul 29 '05 #4

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...
5
by: I wish | last post by:
I wrote some program #include <iostream> class A { public: A() {} A( const A& t ) { std::cout << "Good" << std::endl; } }; A f()
1
by: masood.iqbal | last post by:
I have a few questions regarding overloaded typecast operators and copy constructors that I would like an answer for. Thanks in advance. Masood (1) In some examples that I have seen...
3
by: Tony Johansson | last post by:
Hello experts! I have this piece of code. No user defined copy constructor exist. AccountForStudent create(long number) { AccountForStudent local(number, 0.0); return local; } int main() {
10
by: utab | last post by:
Dear all, So passing and returning a class object is the time when to include the definition of the copy constructor into the class definition. But if we don't call by value or return by value, ...
1
by: blangela | last post by:
3.0 Advanced Topic Addendum There are a few cases where the C++ compiler cannot provide an overloaded assignment operator for your class. If your class contains a const member or/and a...
13
by: Jeroen | last post by:
Hi all, I'm trying to implement a certain class but I have problems regarding the copy ctor. I'll try to explain this as good as possible and show what I tried thusfar. Because it's not about a...
10
by: SzH | last post by:
The code below demonstrates that the copy constructor of moo is not called on the first line of main. In spite of this, g++ (version 4.1.2) refuses to compile it if I make the copy constructor...
22
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.