I have doubt in following piece of code. Function fun1
and fun2
are both same. In one I have declared a local variable and in another a variable is passed by argument. Then why in case of fun1 copy constructor is not called.
#include<stdio.h>
#include<iostream>
using namespace std;
class A
{
public:
A()
{
printf("constructor\n");
}
A(const A&)
{
printf("copy cons\n");
}
~A()
{
printf("destructor\n");
}
};
A fun1()
{
A obj;
return obj;
}
A fun2(A obj)
{
return obj;
}
int main()
{
A a=fun1();
printf("after fun1\n");
A b;
A c = fun2(b);
}
Output
constructor
after fun1
constructor
copy cons
copy cons
destructor
destructor
destructor
destructor