Friend Scope

The name of a friend function or class first introduced in a friend declaration is not in the scope of the class granting friendship (also called the enclosing class) and is not a member of the class granting friendship.

The name of a function first introduced in a friend declaration is in the scope of the first nonclass scope that contains the enclosing class. The body of a function provided in a friend declaration is handled in the same way as a member function defined within a class. Processing of the definition does not start until the end of the outermost enclosing class. In addition, unqualified names in the body of the function definition are searched for starting from the class containing the function definition.

A class that is first declared in a friend declaration is equivalent to an extern declaration. For example:

class B {};
class A
{
      friend class B; // global class B is a friend of A
};

If the name of a friend class has been introduced before the friend declaration, the compiler searches for a class name that matches the name of the friend class beginning at the scope of the friend declaration. If the declaration of a nested class is followed by the declaration of a friend class with the same name, the nested class is a friend of the enclosing class.

The scope of a friend class name is the first nonclass enclosing scope. For example:

class A {
   class B { // arbitrary nested class definitions
      friend class C;
   };
};

is equivalent to:

class C;
class A {
   class B { // arbitrary nested class definitions
      friend class C;
   };
};

If the friend function is a member of another class, you need to use the class member access operators. For example:

class A
{
public:
      int f() { /* ... */ }
};
class B
{
      friend int A::f();
};

Friends of a base class are not inherited by any classes derived from that base class.



Scope of Class Names


Friend Access
Friends
Nested Classes
Derivation