class test{
public:
int data;
test(const test& ){cout<<"INSIDE COPY CON "<<endl;}
test(int val = 0) : data(val){ cout<<"INSIDE CON "<<endl; }
test testfun(const test& obj)
{
cout<<"data : "<<data<<endl;
//test test3(this->data + obj.data);
//cout<<"test3 :"<<test3.data<<endl;
//return test3; //This will work only if return type is changed to const ref
return test(data + obj.data);
}
};
int main()
{
test testO1(1);
test testO2(2);
test testO3 = testO1.testfun(testO2);
cout<<testO3.data<<endl;
getchar();
}
OUTPUT:
INSIDE CON
INSIDE CON
data : 1
INSIDE CON
3
What happens when constructor is called in return statement? Since i am able to return by value and it works i think its not a temporary location. OR is it creating the object as a temporary and using copy constructor to copy the values , inthat case why is the print inside the copy constructor not getting printed.