The short answer
A friend class is a class that another class has allowed to access its private and protected members. The class that owns the data declares the friend with the friend keyword. After that, every member function of the friend class can read and change those private members.
How to declare a friend class
The declaration goes inside the class that owns the data:
#include <iostream>
class Mechanic; // forward declaration
class Engine {
int horsepower = 300; // private
friend class Mechanic; // Mechanic can access the private members above
};
class Mechanic {
public:
void inspect(const Engine& e) const {
std::cout << "Horsepower: " << e.horsepower << '\n';
}
};
int main() {
Engine engine;
Mechanic mechanic;
mechanic.inspect(engine); // Output: Horsepower: 300
}
The friend line belongs in Engine, the class that owns the private data. Writing it in Mechanic would not give Mechanic access to Engine’s private members. The forward declaration is optional, but it makes the code easier to read.
What a friend class can access
- The private and protected members of the class that declares it.
- Every member function of the friend class gets this access, not just one. That is the main difference from a friend function.
What friendship does not do
Friendship is narrower than it may look:
- It is not symmetric. In the example,
Mechaniccan readEngine’s private members, butEnginecannot readMechanic’s private members unlessMechanicdeclaresEngineas a friend. - It is not transitive. If
Mechanichas its own friends, those friends gain no access toEngine. - It is not inherited. A class derived from
Mechanicdoes not get access toEngine’s private members. - It does not make private members public. Code outside the friend class still cannot read
engine.horsepowerdirectly.
Friend class or friend function?
A friend function grants access to one function. A friend class grants access to every member function of another class. Use a friend function when only one operation needs the access. Use a friend class when several operations in one class need the same internal access.
When a friend class makes sense
A common case is two classes that are designed to work closely together. A container and a helper class that walks through the container’s internal storage is one example.
Downsides and alternatives
A friend class couples two classes tightly. If the owner class changes its private members, the friend class may need to change too. Friend classes also weaken some of the data hiding that encapsulation in c is meant to provide. The access modifiers in c keep outside code out, and a friend declaration deliberately opens a door for one class.
Before adding a friend, check whether:
- public member functions in the owner class can do the job,
- the two classes should be combined, or
- the shared data belongs in a separate class.
Common mistakes
- Placing the friend declaration in the wrong class. It must go in the class that owns the private members.
- Expecting the friendship to work in both directions.
- Using
friendto avoid designing a proper public interface.
Summary
A friend class is a class that another class has allowed to access its private and protected members. Declare it with friend class inside the class that owns the data. Keep the list of friends short, because every friend is one more class that depends on your internal details.