-2

Why do we need the friend keyword in this case?

friend bool operator=(const Stack&<E> s, const Stack&<E> q);
tadman
  • 208,517
  • 23
  • 234
  • 262
Alan
  • 99
  • 4
  • 10

2 Answers2

2

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

MasterID
  • 1,510
  • 1
  • 11
  • 15
1

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.

tadman
  • 208,517
  • 23
  • 234
  • 262