Search code examples
c++friend

Friend functions


For the example under Friend Functions
How is the following true?

"Notice that neither in the declaration of duplicate() nor in its later use in main() have we considered duplicate a member of class CRectangle. It isn't! It simply has access to its private and protected members without being a member."

Duplicate is declared in the public section of CRectangle. How is it not a member function and set_values is?

Is this a good example? Any better ones if not?


Solution

  • Any function declared or defined as friend inside a class is not a member of that class. It's just a friend function.

    That said, if a friend function is also defined within the class, it will be found by ADL lookups involving that class (which is normally used for overloaded operators).