Friends

A friend of a class X is a function or class that is granted the same access to X as the members of X. Functions declared with the friend specifier in a class member list are called friend functions of that class. Classes declared with the friend specifier in the member list of another class are called friend classes of that class.

A class Y must be defined before any member of Y can be declared a friend of another class.

You can declare an entire class as a friend.

If the class has not been previously declared, use an elaborated type specifier and a qualified type specifier to specify the class name.

If the friend class has been previously declared, you can omit the keyword class, as shown in the following example:

class F;
class X
{
public:

      X() {a=1; b=2;}
private:
      int a, b;
      friend F; // elaborated-type-specifier not required
};


Examples of Friends


Friend Scope
Friend Access
Member Functions