I know I can prevent ordinary heap allocation of custom class and its descendants by making the class's operator new
private, but is there any way to prevent a user of a library from calling std::make_shared
on a custom class (or its descendants)? Apparently, simply making the operator new
private in a class does not stop it.
Note that I do not want to completely prevent shared pointers from being created by any means, as I intend to still be able to produce a std::shared_ptr
for my custom class by calling a static generator method, but I still want to prevent others from trying to call std::make_shared
on my class directly.
EDIT:
To address queries below, my ultimate goal here is a flexible and reusable mechanism for extracting a shared_ptr from a raw pointer. std::enable_shared_from_this
is regretfully not very friendly when it comes to inheritance and especially multiple inheritance.
Also, because I am intending for this to be used as in a template class using CRTP, it would complicate matters somewhat for the a child class T to need to explicitly make the templated parent class it inherits from a friend so that it can access otherwise private constructors.