0

In our C++ programming class, professor is using "friend" keyword to overload operators. However, when I search on the internet, most people don't use "friend" keyword. So, do we need to use "friend" keyword for operators? Is there such a rule or not?

  • 3
    If the operator needs to access private or protected members of the class, then it has to be a `friend`. If it doesn't, then it doesn't. – Igor Tandetnik Mar 15 '20 at 22:17
  • You should give examples of the exact syntax, not just describe it as using the keyword friend. – Joe C Feb 10 '21 at 17:02

1 Answers1

1

If you define the operators within your class, they automatically have acces to the private parts of the objects involved. Not all operators should be defined as part of the class, though. In those cases where you define the operators outside of your class they need to be declared as a friend if they need to be able to access the private parts of the objects.

Andreas_75
  • 44
  • 5