I want to use smart pointers in this way:
using namespace std;
shared_ptr<string> foo;
weak_ptr<string> bar (foo);
foo = make_shared<string>("some string");
cout << *bar.lock(); // SIGSEGV
The clue is how to initialize shared_ptr
's object manager without construction the object (string
in this case). Is it even possible? I could use default ctor of the object and later use copy/move assignment. But may some more elegant solution exist?
It is worth for me that the object won't be initialized - it would be a lie in logic if I would initialize the shared_ptr with empty value.