Menu

Deep dive into the C++ Passkey idiom
πŸ“°

Deep dive into the C++ Passkey idiom

DEV Community: cppΒ·llzzzΒ·1 day ago
#lPl2zXUr
#devtocpp#devto#dev#include#passkey
Reading 0:00
15s threshold

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.…

Continue reading β€” create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.