Copy constructor not getting called when a function returns object

Hi,
I studied that copy constructor will get called when a function returns object.But in my case it dint.I have attached the sample code below.
Thanks in Advance
class base
{
public:
base()
{
cout<<"constructor"<<endl;
}
base( const base &b)
{
cout<<"copy constructor"<<endl;
}
};

base f1()
{

base b;
return b;
}

base f1(base c)
{

base b;
return b;
}
int main()
{

base a;

base b = a;

base c(b);

b=a;

c =f1(); //expecting copy constructor to get called,but it dint

a = f1(b);
return 0;
}
Output is:
constructor
copy constructor
copy constructor
constructor
copy constructor
constructor

Without getting into it in detail, I'd guess http://en.wikipedia.org/wiki/Return_value_optimization, but only because that's what I always say when there are fewer copy consturctor calls that I might expect.
Last edited on
A constructor is only used when a new object is created. c =f1(); will use the copy assignment operator.
Last edited on
Topic archived. No new replies allowed.