C++ Patterns: The Badge
An outsider class or function can also be declared as a friend. Let’s use an example from the (virtual file system) class in Serenity:
These functions are called by all objects when they are constructed and destroyed respectively. A common technique for preventing others from accessing these functions would be making them private and adding as a friend of :
This prevents outsiders except from calling those functions, but it also means that now has full access to the private parts of which is not great.
Source: awesomekling.github.io