overloading for multiple arithmetic operations

Hi,

I would like to add multiple objects of a class with integers/
floats. Need some basic help in fixing an error

Eg:

#include<iostream>
using namespace std;
class Test
{
public:
Test(float _code){ code = _code;}
Test(){}
float code;
friend Test operator+(Test & ob, float val);
friend Test operator+(float val, Test & ob);
Test operator+(Test & ob);
};
Test Test::operator+(Test & ob)
{
Test temp;
temp.code = this->code + ob.code;
return temp;
}
Test operator+(Test & ob, float val)
{
Test temp;
temp.code = ob.code + val;
return temp;
}
Test operator+(float val, Test & ob)
{
Test temp;
temp.code = ob.code + val;
return temp;
}
int main()
{
Test ob1(1.0), ob2(2.0), ob3(3.0), ob4(0.0);
float c1=100.5 ;

ob4 = ob1+ob2+ob3 ; // This works
ob4 = ob1+c1; // This works
ob4 = ob1 + ob2 + c1 ;// Does not work... why ?
ob4 = ob1 + c1 + ob2 ;// This works...


return 0;
}

My question is : why does the third operation ob4 = ob1 + ob2 + c1
fail ?

-- Dominic
What does the error say?
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 ?
You can't bind a temporary object to a non-const reference parameter.

ob1 + ob2 returns a temporary.

All of your operator+ take Test instances by non-const reference, which
means the compiler will not call those functions for the "+ ob3" part.

domi0002 wrote:
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.

Test operator+(const Test& ob) const;
Thanks... What if I need to change the Test object inside the operator+ ? In that case, i cannot use const ... Any workarounds ?
domi0002 wrote:
Thanks... What if I need to change the Test object inside the operator+ ?

You don't need to.
operator+ has no business modifying its operands.
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?
Topic archived. No new replies allowed.