1. Motivation: The Problem with friend
In C++, friend is a double-edged sword. If you allow your friend to touch you inappropriately, that would be hurt. If class A befriends another class B , then everything in A becomes visible to B . friend grants all-or-nothing access β which is rarely what you actually want. You usually want your friend to only able to touch a portion of you.
Consider the following scenario. You have a Widget class that must be created through a factory, because the constructor needs to perform some privileged setup (registering with a resource manager, validating invariants, logging, and so on). But the constructor itself has to be public for std::make_unique or for the factory to reach it.β¦