Are you trying to pass the shared_ptr around everywhere? If you are, don't.
It is ok to obtain the pointer from out of the smart pointer.
#include <memory>
struct test
{
};
void do_something(test *)
{
//do something to test
}
int main()
{
std::unique_ptr<test> t_ptr = std::make_unique<test>();
do_something(t_ptr.get()); //obtains the pointer from the smart pointer
return 0;
}
The smart pointer signifies what is responsible for deleting the block of memory. So unless you want to change this, it is recommended that you pass around the raw pointer.
Also, if you want to create a singleton, there is a better option.
struct test
{
static test *get_instance()
{
static test instance{};
return &instance;
}
};
int main()
{
test *instance = test::get_instance();
return 0;
}
Because the local variable is static it has global lifetime, and this removes any need for memory management for this object too.