You're violating const correctness by passing the Test objects as non-const references, even though the operators do not change them. Since temporaries are const when they are passed to functions, there is no matching operator+ for (ob1 + ob2) + c1.
Thanks.. but how does the first/fourth operation work ? In the first operation, ob1+ob2 is first executed and is returned as a const temp object. so there should not be any matching function for (ob1 + ob2)+ ob3 ?
Thanks.. but how does the first/fourth operation work ? In the first operation, ob1+ob2 is first executed and is returned as a const temp object. so there should not be any matching function for (ob1 + ob2)+ ob3 ?
You can still call non-const member functions on temporaries, so that includes operator+(Test&).
In the first case ob1+ob2 results in a temporary object, but said operator can be called with ob3 as the parameter. ob3 can be bound to a non-const reference, so it's fine. It's basically the same for the fourth case.
Just make sure to be const correct for all your parameters and functions, e.g.
I have a situation where I need to change just one variable of the Test object.. and you can still modify the Test object even though you pass it as const. Use 'mutable' for the variable that needs to be modified and it works.
I have a situation where I need to change just one variable of the Test object.. and you can still modify the Test object even though you pass it as const. Use 'mutable' for the variable that needs to be modified and it works.
That's horrible practice.
And why exactly do you have to modify the objects that are being added?