When I try compiling following program, I get an error on l-value which makes sense. Error is: error C2106: '=' : left operand must be l-value
Code:
int main()
{
int a,b,c,d;
b+c=d;
return 0;
}
But following code works absolutely fine when I replace integer with my own structure
struct MyStruct
{
int val;
MyStruct(){}
};
MyStruct operator+(MyStruct& s1, MyStruct& s2)
{
MyStruct temp;
return temp;
}
int main()
{
MyStruct a,b,c,d;
b+c=d;
return 0;
}
How come the second code compiles? I know I can return const from the operator+. But isn't b+c in second example a rvalue? So how come it compiles