copy constructor and return by value

B

bipod.rafique

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
 
S

suresh

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.
 
V

Victor Bazarov

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top