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