I have been looking at some open source code and I see two different definitions of overloading an operator. What is the difference between them and are there any benefits of doing either?
For example, one example we have a class:
class foo{
public:
void SetValue(double input_value) {foo_value = input_value};
double GetValue() {return foo_value;}
private:
double foo_value;
};
Then I sometimes see two different types / styles of overloading the addition operator (for example)
class foo{
const foo operator+(const foo& input_foo);
};
const foo foo::operator+(const foo& input_foo) {
foo_value += input_foo.foo_value;
return *this;
}
The other type of overload I sometimes see is:
class foo{
friend const foo operator+(const foo& input_foo1, const foo& input_foo2);
};
const foo operator+(const foo& input_foo1, const foo& input_foo2); // Defined right after class
const foo operator+(const foo& input_foo1, const foo& input_foo2) {
foo temp_foo;
temp_foo.SetValue(input_foo1.GetValue() + input_foo2.GetValue());
return temp_foo;
}