Because operator+
is a free function, which is completely unrelated to the A
class. It just so happens that one of its arguments is of class A
.
This means that it is not the same as A::operator+
which would be defined as:
class A {
public:
const A operator+(const int);
};
In your code sample you are defining a friend
function to the class. So the free function can now access the private and protected part of the class. If you wouldn't define it (operator+
) friend
would only be able to access public members of A
. That's why you are making it friend
.