In the case of some operators, there is absolutely no difference between declaring it as a member function or as a friend function:
1 2 3 4 5 6
|
class Vec
{
int data[3];
public:
int const &operator[](int i) const { return data[i]; }
};
|
is precisely the same as:
1 2 3 4 5 6
|
class Vec
{
int data[3];
public:
friend int const &operator[](Vec const &v, int i) { return v.data[i]; }
};
|
However, if it is not an operator, but an ordinary function, then it makes a difference in notation:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
class Vec
{
int data[3];
public:
int const &subscr(int i) const { return data[i]; }
friend int const &subscr(Vec const &v, int i) { return v.data[i]; }
};
int main()
{
Vec v;
v.subscr(1); // Member
subscr(v,1); // Friend
}
|
In this case, if there is an established consesus, you should follow taht, otherwise let it be a matter of personal taste.
In general, there will always be a duality between these two forms, and it all comes down to a question of what is the most natural notation for the specific thing that you are creating.
As Disch points out, the only time you might not have a choice, is when you are extending somebody else's class.