Jamala Khaligova
Jamala Khaligova

Reputation: 27

C++ operator overloading with "friend" keyword

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?

Upvotes: 0

Views: 134

Answers (1)

Andreas_75
Andreas_75

Reputation: 64

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.

Upvotes: 1

Related Questions