C++ · Module 8 · Lesson 71 min read

friend Functions & Classes

friend functions

A non-member function declared friend inside a class can access its private and protected members:

c
class Money {
    long long paisa_;
    friend std::ostream& operator<<(std::ostream& os, const Money& m);
};
std::ostream& operator<<(std::ostream& os, const Money& m) {
    return os << "Rs. " << m.paisa_ / 100;
}

Why operator<< is a friend

std::cout << m calls operator<<(std::cout, m). The left operand is a stream, not our class, so it can't be a member function of Money. Making it a friend gives it access to private data.

friend classes

perl
class Engine {
    friend class Mechanic;   // all Mechanic member functions can access Engine's privates
};

Hidden friends ✅

Defining the friend function inside the class body:

arduino
class Money {
    friend bool operator==(const Money& a, const Money& b) { return a.paisa_ == b.paisa_; }
};

It's only found by argument-dependent lookup (when a Money is involved) — fewer overload surprises and faster compiles.

Properties of friendship

  • Granted by the class, not taken: you can't declare yourself a friend of someone else's class
  • Not inherited (a friend of Base is not a friend of Derived)
  • Not transitive (a friend of a friend isn't your friend)
  • Not mutual (if A says B is a friend, A is not automatically B's friend)

Use friend sparingly

Every friend can break your invariants. Good uses:

  • Operators (<<, >>, ==, +) that need private data
  • Tightly coupled helper classes (an iterator and its container, a test fixture) If public getters are enough, prefer them.

Example

C++
#include <iostream>
#include <string>

class Money {
public:
    explicit Money(long long paisa = 0) : paisa_(paisa) {}

    // hidden friends: defined inside the class, may read paisa_
    friend std::ostream& operator<<(std::ostream& os, const Money& m) {
        return os << "Rs. " << m.paisa_ / 100 << '.' << (m.paisa_ % 100 < 10 ? "0" : "") << m.paisa_ % 100;
    }
    friend Money operator+(const Money& a, const Money& b) { return Money(a.paisa_ + b.paisa_); }
    friend bool operator==(const Money& a, const Money& b) { return a.paisa_ == b.paisa_; }

    friend class Auditor;            // Auditor may inspect private data

private:
    long long paisa_;
};

class Auditor {
public:
    static bool suspicious(const Money& m) { return m.paisa_ > 10'000'000; }   // above Rs. 1 lakh
};

class Vault {
    std::string secret_ = "4521";
    friend void reset_pin(Vault& v, const std::string& pin);   // a free friend function
public:
    bool unlock(const std::string& pin) const { return pin == secret_; }
};

void reset_pin(Vault& v, const std::string& pin) { v.secret_ = pin; }

int main() {
    Money rent(1500000), food(420050);
    Money total = rent + food;
    std::cout << "Rent: " << rent << '\n';
    std::cout << "Food: " << food << '\n';
    std::cout << "Total: " << total << '\n';
    std::cout << std::boolalpha << "rent == food? " << (rent == food) << '\n';

    Money transfer(25'000'000);
    std::cout << "Rent suspicious? " << Auditor::suspicious(rent)
              << ", " << transfer << " suspicious? " << Auditor::suspicious(transfer) << '\n';

    Vault v;
    std::cout << "unlock 1234: " << v.unlock("1234") << '\n';
    reset_pin(v, "1234");
    std::cout << "after reset, unlock 1234: " << v.unlock("1234") << '\n';
    return 0;
}

/* Output:
Rent: Rs. 15000.00
Food: Rs. 4200.50
Total: Rs. 19200.50
rent == food? false
Rent suspicious? false, Rs. 250000.00 suspicious? true
unlock 1234: false
after reset, unlock 1234: true
*/