A common idiom is to have a privately constructable class with a (static) factory that returns a shared (or unique) pointer.

class A { A() {}; public: static std::shared_ptr<A> make() { return std::make_shared<A>(); } };

std::make_shared

Looks simple and yet doesn't work sincecannot access A's constructor.

I found a simple trick how to solve this without opening the class up to public construction: add a parameter with a private »cookie« to the constructor which proves that the class is being constructed »from within«.

class A { struct ctor_cookie {}; public: explicit A(ctor_cookie) {}; static std::shared_ptr<A> make() { return std::make_shared<A>(ctor_cookie()); } };