Reputation: 440
When [abc e = a+b] is called, the copy constructor is not called.
class abc{
int i;
public:
abc()
{
i = 10;
cout<<"constructor"<<endl;
}
abc(const abc &a)
{
cout<<"copy constructor"<<endl;
i = a.i;
}
abc operator=(const abc &a)
{
cout<<"operator="<<endl;
abc temp;
temp.i = a.i;
return temp;
}
abc operator+(abc& a)
{
cout <<"Operator+ called"<<endl;
abc temp;
temp.i = i+a.i;
return temp ;
}
};
int main()
{
abc a,b;
cout <<"----------------------------------------------"<<endl;
a = b;
cout <<"----------------------------------------------"<<endl;
abc c = a;
cout <<"-----------------------------------------------"<<endl;
abc d(a);
cout <<"-------------------------------------------"<<endl;
**abc e = a+b;**
}
However if the overload operators methods are replaced with the following methods that return references to the object of class abc, copy constructor gets called.
abc& operator=(const abc &a)
{
cout<<"operator="<<endl;
i = a.i;
return *this;
}
abc& operator+(const abc& a)
{
cout <<"Operator+ called"<<endl;
i = i+a.i;
return *this ;
}
Can some one please explain why does this happen?
Upvotes: 2
Views: 240
Reputation: 183
You can disable optimization options on your compiler. Please check for the optimization flag used while compiling your code.
Upvotes: 0
Reputation: 264351
Its an optimization explicitly allowed by the standard.
In n3376
12.8 paragraph 31:
When certain criteria are met, an implementation is allowed to omit the copy/move construction of a class object, even if the constructor selected for the copy/move operation and/or the destructor for the object have side effects.
This optimization can be achieved via RVO (this case) and under certain conditions NRVO.
Upvotes: 0
Reputation: 29966
This is happening due to return value optimization.
Since no costly memory-copying operations are being made if your constructor returns a reference, it works as you would expect, because no optimization is being made in this case. If it returns a value, the optimization kicks in and it works in a somewhat unexpected (for you) manner, but it is allowed by the standard.
Upvotes: 1