Consider the following operator overloading scenario
class integer
{
public:
int a;
integer()
{
a = 0;
}
friend integer operator+(integer &a ,int b);
};
integer operator+(integer &a,int b)
{
a.a = a.a + b;
return a;
}
int main()
{
integer obj;
integer obj2;
obj+2; //Line 1. this works.
The operator+ function returns an object of integer type.So why can't i execute a function call like
(obj+2)+3; //no match for 'operator+' (operand types are 'integer' and 'int')
While this works
obj2 = obj+2;
obj2+3;
(obj+2)returns a temporary. Temporaries can't bind to a non-const reference.+, but it behaves like+=. This will end in tears.