C++ Friend Functions
In this page:
Introduction to Friend Functions
A friend function is a function defined outside a class's own scope, but explicitly granted permission by that class to access its private and protected members. It breaks strict encapsulation in a controlled, opt-in way, chosen by the class itself rather than forced from outside.
Example: Introduction to Friend Functions
#include <iostream>
class Box {
int size = 5;
friend void showSize(Box b);
};
void showSize(Box b) {
std::cout << b.size << std::endl; // allowed: showSize is a friend
}
int main() {
Box b;
showSize(b);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Friend Class
A class can declare an entire other class as its friend. When it does, every member function of that friend class gains access to the private and protected members of the class granting friendship, which is a much broader grant than befriending a single function.
Example: Friend Class
#include <iostream>
class Engine {
int power = 100;
friend class Car;
};
class Car {
public:
void show(Engine e) { std::cout << e.power << std::endl; }
};
int main() {
Engine e;
Car c;
c.show(e);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Friend Member Functions
Instead of making a whole other class a friend, you can grant friend status to just one specific member function of that class, giving it private access while keeping the rest of that class's methods locked out — a more surgical alternative to a full friend class.
Example: Friend Member Functions
#include <iostream>
class Car;
class Mechanic {
public:
void inspect(Car &c);
};
class Car {
int mileage = 50000;
friend void Mechanic::inspect(Car &c);
};
void Mechanic::inspect(Car &c) {
std::cout << c.mileage << std::endl;
}
int main() {
Car car;
Mechanic m;
m.inspect(car);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Friend Functions with Multiple Classes
A single friend function can be declared a friend of multiple classes at once, letting it act as a bridge that reads or compares private data belonging to two different, otherwise-unrelated classes — useful for things like custom equality or arithmetic operators between two types.
Example: Friend Functions with Multiple Classes
#include <iostream>
class Wallet;
class Bank {
int reserve = 1000;
friend void compare(Bank b, Wallet w);
};
class Wallet {
int cash = 50;
friend void compare(Bank b, Wallet w);
};
void compare(Bank b, Wallet w) {
std::cout << (b.reserve > w.cash) << std::endl;
}
int main() {
Bank bank;
Wallet wallet;
compare(bank, wallet);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Best Practices for Friend Functions
Friend declarations are convenient but should be used sparingly, since every friend you add is one more piece of code with direct access to your private internals. Overusing them chips away at the very encapsulation that private and protected access specifiers exist to enforce.
Example: Best Practices for Friend Functions
#include <iostream>
class Box {
int size = 5;
public:
int getSize() { return size; } // prefer a public getter over a friend when possible
friend void debugPrint(Box b); // reserved for genuinely tight-coupled cases only
};
void debugPrint(Box b) {
std::cout << "Debug size: " << b.size << std::endl;
}
int main() {
Box b;
debugPrint(b);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: