-
Notifications
You must be signed in to change notification settings - Fork 71
Friend Classes and Functions
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 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:
- Friends should be used only for limited purpose. too many functions or external classes are declared as friends of a class with
protected
orprivate
data, it lessens the value of encapsulation of separate classes in object-oriented programming. - Friendship is not mutual. If a class A is friend of B, then B doesn’t become friend of A automatically.
- Friendship is not inherited (see example below).
- The concept of friends is implemented in all OOP languages, Java for one.
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:
- https://www.geeksforgeeks.org/friend-class-function-cpp/
- http://en.wikipedia.org/wiki/Friend_class
- http://en.wikipedia.org/wiki/Friend_function
- http://www.cprogramming.com/tutorial/friends.html
- http://www.parashift.com/c++-faq/friends-and-encap.html
- https://www.geeksforgeeks.org/g-fact-34/
- https://en.cppreference.com/w/cpp/language/friend