Friend Functions and Classes
A friend function is a function that can access the private members of another class.
Similarely, a friend class is a class that can access the private members of another class.
Defining a friend function
E.g. friend void printValue(MyClass& my_class_ref)
inside the class definition:
class MyClass {
private:
int my_number;
public:
MyClass(int number) : my_number{number} {}
friend void printValue(MyClass& my_class_ref);
};
void printValue(MyClass& my_class_ref) {
// Accessing an otherwise private member
printf("%d\n", my_class_ref.my_number);
}
int main() {
MyClass my_class = { 123 };
printValue(my_class); //=> 123
}
Defining a friend class
E.g. friend class MyUtils
inside the class definition:
class MyClass {
private:
int my_number;
public:
MyClass(int number) : my_number{number} {}
friend class MyUtils;
};
class MyUtils {
public:
static void printMyNumber(MyClass& my_class_ref) {
// Accessing an otherwise private member
printf("%d\n", my_class_ref.my_number);
}
};
int main() {
MyClass my_class = { 456 };
MyUtils::printMyNumber(my_class); //=> 456
}
Defining a friend member function
Use this to make only some members of a class friend.
We use a technique called "forward declaration" below. Basically, we are telling the compiler ahead of time that certain class or function exists (with the given signature).
Otherwise, we will get compile error because the compiler compiles the source code sequentially from top to bottom, and will complain if it sees a name not yet defined.
// Forward declaration:
// (the compiler needs to know that `MyClass` exists)
class MyClass;
class MyUtils {
public:
// Forward declaration again:
// (the compiler needs to know that the function
// `void printMyNumber(MyClass& my_class_ref)` exists)
static void printMyNumber(MyClass& my_class_ref);
};
class MyClass {
private:
int my_number;
public:
MyClass(int number) : my_number{number} {}
friend void MyUtils::printMyNumber(MyClass& my_class_ref);
};
void MyUtils::printMyNumber(MyClass& my_class_ref) {
printf("%d\n", my_class_ref.my_number);
}
int main() {
MyClass my_class = { 789 };
MyUtils::printMyNumber(my_class); //=> 789
}
References
- 12.15 — Friend functions and classes — https://www.learncpp.com/cpp-tutorial/friend-functions-and-classes/
- 2.6 — Forward declarations and definitions — https://www.learncpp.com/cpp-tutorial/forward-declarations/