By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,402 Members | 1,110 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,402 IT Pros & Developers. It's quick & easy.

copy constructor and return by value

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.