Search code examples
c++encapsulationfriendinformation-hiding

C++ friend functions/class uses?


What is the real use of a friend function/class in C++? Could you give an example where only friend is the right approach?

Thanks


Solution

  • "In C++, only your friends can access your private parts."

    The point of a friend is that you can package your software into smaller groupings, like friend classes etc, while still allowing access to the internals of a class. This arguably lets you maintain finer control on encapsulation than without friending.