I'm new in C++, so, please, go easy on me :) I've found two different ways to overload binary operator in c++.
The first one (from book "Object-Oriented Programming in C++", Robert Lafore):
class Distance
{
private:
int value;
public:
Distance() : value(0) {}
Distance(int v) :value(v) {}
Distance operator+(Distance) const;
};
Distance Distance::operator+(Distance d2) const
{
return Distance(value+d2.value);
}
And another one, with using of friend funcs (from the Internet)
class Distance
{
private:
int value;
public:
Distance() : value(0) {}
Distance(int v) :value(v) {}
friend const Distance operator+(const Distance& left, const Distance& right);
};
const Distance operator+(const Distance& left, const Distance& right)
{
return Distance(left.value + right.value);
}
All these cases make it possible to write following code like this:
Distance d1(11);
Distance d2(5);
Distance d3 = d1 + d2;
My question: what is the main difference of these cases? Maybe some advantages or disadvantages. Or some kind of "good programming manners"?
Thank you in advance for your wisdom! :)