Skip to content

Friend Classes and Functions

Terry Griffin edited this page Nov 14, 2018 · 2 revisions

Friend Class

The friend declaration appears in a class body and grants a function or another class access to private and protected members of the class where the friend declaration appears. It is sometimes useful to allow a particular class to access private members of other class.

class Y {
    int data; // private member
    // the non-member function operator<< will have access to Y's private members
    friend std::ostream& operator<<(std::ostream& out, const Y& o);
    friend char* X::foo(int); // members of other classes can be friends too
    friend X::X(char), X::~X(); // constructors and destructors can be friends
};
// friend declaration does not declare a member function
// this operator<< still needs to be defined, as a non-member
std::ostream& operator<<(std::ostream& out, const Y& y)
{
    return out << y.data; // can access private member Y::data
}

Example a LinkedList class may be allowed to access private members of Node:

class Node { 
private: 
  int key; 
  Node *next; 

  /* Other members of Node Class */
  
  friend class LinkedList; // Now class  LinkedList can  
                           // access private members of Node 
}; 

Friend Functions

Friend Function Like friend class, a friend function can be given special grant to access private and protected members. A friend function can be:

  • A method of another class
  • A global function
class Node { 
private: 
   int key; 
   Node *next; 
  
   /* Other members of Node Class */
   friend int LinkedList::search(); // Only search() of linkedList  
                                   // can access internal members 
}; 

Following are some important points about friend functions and classes:

  1. Friends should be used only for limited purpose. too many functions or external classes are declared as friends of a class with protected or private data, it lessens the value of encapsulation of separate classes in object-oriented programming.
  2. Friendship is not mutual. If a class A is friend of B, then B doesn’t become friend of A automatically.
  3. Friendship is not inherited (see example below).
  4. The concept of friends is implemented in all OOP languages, Java for one.

Friend and Inheritance

In C++, friendship is not inherited. If a base class has a friend function, then the function doesn’t become a friend of the derived class(es).

For example, following program prints error because show() which is a friend of base class A tries to access private data of derived class B.

#include <iostream> 
using namespace std; 
  
class BaseClass { 
protected: 
    int x; 
public: 
    BaseClass() { 
      x = 0;
    } 
    friend void show(); 
}; 
  
class SubClass: public BaseClass { 
public: 
 
    SubClass(){
      y = 0;
    }
private: 
    int y; 
}; 


void show() 
{ 
  SubClass sub; 
  cout << "The default value of BaseClass::x = " << sub.x; 
  
  // Can't access private member declared in class 'SubClass' 
  cout << "The default value of SubClass::y = " << sub.y; 
} 
  
int main() 
{ 
  show(); 
  return 0; 
}

Sources:

  1. https://www.geeksforgeeks.org/friend-class-function-cpp/
  2. http://en.wikipedia.org/wiki/Friend_class
  3. http://en.wikipedia.org/wiki/Friend_function
  4. http://www.cprogramming.com/tutorial/friends.html
  5. http://www.parashift.com/c++-faq/friends-and-encap.html
  6. https://www.geeksforgeeks.org/g-fact-34/
  7. https://en.cppreference.com/w/cpp/language/friend
Clone this wiki locally