Friend Function

Functions that can access any private fields and methods of the class. They are the non-member functions that can access and manipulate the private and protected members of the class for they are declared as friends.

For example, a Linked List class may be allowed to access private members of Node.

A friend function can be:

  1. A global function
  2. A member function of another class
friend return_type function_name (arguments);// for a global function
// or
friend return_type class_name::function_name (arguments); // for a member function of another class

Links to this page CS247 Lecture 2