Question.3
A developer needs to support int + Vec (not just Vec + int):
class Vec {
int x;
public:
Vec(int v) : x(v) {}
// Vec + int works as member:
Vec operator+(int n) const { return Vec(x+n); }
// int + Vec needs friend:
friend Vec operator+(int n, const Vec& v) {
return Vec(n + v.x);
}
};
Vec a(10);
Vec b = 5 + a; // int + VecWhy is the friend function needed?