Friend Functions

What Is a Friend Class in C++?

Learn what a friend class is in C++, how a friend declaration gives one class access to another's private members, and the limits of that access.

On this page
  1. The short answer
  2. How to declare a friend class
  3. What a friend class can access
  4. What friendship does not do
  5. Friend class or friend function?
  6. When a friend class makes sense
  7. Downsides and alternatives
  8. Common mistakes
  9. Summary

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, Mechanic can read Engine’s private members, but Engine cannot read Mechanic’s private members unless Mechanic declares Engine as a friend.
  • It is not transitive. If Mechanic has its own friends, those friends gain no access to Engine.
  • It is not inherited. A class derived from Mechanic does not get access to Engine’s private members.
  • It does not make private members public. Code outside the friend class still cannot read engine.horsepower directly.

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 friend to 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.

The complete guide to friend functions

Friend Functions in C++
Read the guide

Keep exploring

Guides from other topics

All topics →

How to Stringify a Macro in C++

Learn how the # operator turns a C++ macro argument into a string literal, with examples, the two-level trick for expanded macros and common pitfalls.

Macros 3 min read

Polymorphism in C++

Learn polymorphism in C++ with clear code examples: virtual functions, override, abstract classes, templates, and the mistakes beginners make most often.

Polymorphism 7 min read