Friend Functions

Friend Functions in C++

Learn what a friend function is in C++, how the friend keyword lets a non-member function reach private members, and when to use one, with code examples.

On this page
  1. What is a friend function?
  2. How to declare and use a friend function
  3. Friend functions are not member functions
  4. A common use: the stream insertion operator
  5. Friend function or friend class?
  6. When to use a friend function
  7. Common mistakes
  8. Summary

What is a friend function?

A friend function is a function that a class has allowed to access its private and protected members. The class grants that permission by declaring the function with the friend keyword inside the class body. The function itself is not a member of the class. It is defined outside the class and called like any other function.

By default, code outside a class cannot read or change its private members. The access modifiers in c are what enforce that rule. A friend function is a deliberate exception to it. Because the class writes the friend declaration, the class decides who gets the access.

How to declare and use a friend function

Put the friend declaration inside the class, and write the function’s definition outside the class without the friend keyword:

#include <iostream>

class Box {
    double length;  // private by default

public:
    Box(double l) : length(l) {}

    friend void printLength(const Box& b);  // grants access to private members
};

void printLength(const Box& b) {
    std::cout << "Length: " << b.length << '\n';  // allowed by the friend declaration
}

int main() {
    Box box(3.5);
    printLength(box);  // Output: Length: 3.5
}

Remove the friend line, and the compiler rejects the access to b.length because that member is private.

Notice that the function is called as printLength(box), not box.printLength(). A friend function has no this pointer, so the object is passed in as an argument.

Friend functions are not member functions

A friend function differs from a member function in a few ways:

  • It has no this pointer, so it receives any object it needs as a parameter.
  • It cannot be called with dot syntax.
  • The friend declaration works the same way whether it sits in the public, private or protected section.
  • Friendship is not inherited. A class derived from a class that has a friend does not get that friend’s access.
  • Friendship is not transitive. A friend of a friend does not gain access just because of that chain.

A common use: the stream insertion operator

Friend functions often appear in operator overloading. The stream insertion operator << is a common case. In std::cout << p, the left operand is a std::ostream, not your class, so the operator can’t be a member of your class. Making it a friend lets it read private data:

#include <iostream>

class Point {
    int x;
    int y;

public:
    Point(int x, int y) : x(x), y(y) {}

    friend std::ostream& operator<<(std::ostream& out, const Point& p) {
        return out << "(" << p.x << ", " << p.y << ")";
    }
};

int main() {
    Point p(2, 5);
    std::cout << p << '\n';  // Output: (2, 5)
}

The function is defined inside the class body here. It is still not a member. The definition simply sits in the class. You will see this pattern often when you study operator overloading in c.

Friend function or friend class?

A friend function grants access to one function. A friend class grants access to every member function of another class. Many readers also ask, What is a friend class? The short answer is that a class can name another class as its friend, and then all of that other class’s member functions can use its private members.

When to use a friend function

A friend function can make sense when:

  • A function needs to work closely with a class’s private data but is not logically one of the class’s operations. Examples include a stream operator, or a binary operator where your class is on the right side.
  • Two closely related functions need the same internal details, and a public interface would expose too much.

Be cautious with friends:

  • A public getter or a member function may do the job without any friendship.
  • Each friend becomes part of the class’s internals. If the class changes how it stores its data, the friend may need to change too.

Friend functions weaken some of the protection that encapsulation in c provides. Use them when the closeness is intentional and the list stays short.

Common mistakes

  • Writing friend in the definition outside the class. The keyword belongs only in the declaration inside the class.
  • Calling the function with dot syntax, such as box.printLength(). This won’t compile because the function is not a member.
  • Assuming a derived class gets the friend’s access. Friendship is not inherited.
  • Adding a friend to get around a design problem without checking whether a public method would work better.

Summary

A friend function is a non-member function that a class explicitly allows to access its private and protected members. Declare it with friend inside the class, define it outside the class (or inside it, as is common with operators), and call it like a normal function. Keep the list of friends short, because each one widens what can touch the class’s internals.

Keep exploring

Guides from other topics

All topics →

Is C++ Harder Than Python?

For most beginners, C++ is harder than Python because it needs a compiler, explicit types and careful memory handling. See why, with a side-by-side example.

C++ vs Python 4 min read