int main(int argc, char *argv[])
{
auto sp = std::make_shared<int>();
auto p = sp.get();
delete p; //here
std::cout << *sp << std::endl;
return 0;
}
I was hoping that the object managed by the shared_ptr sp will be deleted by the statement commented "here" but the object remains intact and is printed in the output statement.
- Is this not working because I didn't create the object managed by the
shared_ptrexplicitly with thenewkeyword (because everynewstatement must have a correspondingdeletestatement and therefore everydeletestatement, an earlier correspondingnewkeyword?)? - And does this mean that pointed objects managed by
shared_ptrs, created from themake_sharedfunction are destroyed only by theshared_ptrs themselves?