A
abhash
I am bit puzzled at the following piece of code I tried:
----------------------------------------------------------------------------------
#include <iostream>
using namespace std;
class Test {
public:
Test() { cout<<"Cons\n";}
Test(Test& a) { cout<<"Copy cons\n";}
};
Test fun()
{
return Test();
}
int main()
{
cout<<"First way of initialization\n";
Test t1;
Test t2 = t1;
cout<<"\nSecond way of initialization\n";
Test t3 = fun();
return 0;
}
OUTPUT (when compiled on CC compiler) :
First way of initialization
Cons
Copy cons
Second way of initialization
Cons
------------------------------------------------------------------------------------
I am intrigued why second initialization doesn't call copy
constructor ?
Aren't we passing the temporary object returned by fun() call to the
t3 for copying ?
----------------------------------------------------------------------------------
#include <iostream>
using namespace std;
class Test {
public:
Test() { cout<<"Cons\n";}
Test(Test& a) { cout<<"Copy cons\n";}
};
Test fun()
{
return Test();
}
int main()
{
cout<<"First way of initialization\n";
Test t1;
Test t2 = t1;
cout<<"\nSecond way of initialization\n";
Test t3 = fun();
return 0;
}
OUTPUT (when compiled on CC compiler) :
First way of initialization
Cons
Copy cons
Second way of initialization
Cons
------------------------------------------------------------------------------------
I am intrigued why second initialization doesn't call copy
constructor ?
Aren't we passing the temporary object returned by fun() call to the
t3 for copying ?