Why do we need the friend keyword in this case?
friend bool operator=(const Stack&<E> s, const Stack&<E> q);
It means there is an overload of operator= that is allowed to access the private members of Stack objects.
You could, in principle, do it inside your class, without the friend modifier, so the signature would be
bool operator=(const Stack&<E> q) const; // The first operand would be this object
But be warned, operator= can't be defined outside class: Demo
These "functions" are not called directly on the object like x.y(z)
where y()
is clearly a function call, but instead indirectly as when exercising x = z
.
If that function needs to access protected
or private
internals of the object, which is a common thing, they need to be friend
since they're considered "external" to the object.