I am doing self learning experiments with C++11 shared_pointers to understand how they take ownership of a raw pointer. If they take ownership of the object pointed by raw pointer then the raw pointer should also point to nullptr when the object is deleted.
I have tried a sample code
#include<iostream>
#include<memory>
class X
{
public:
X(){std::cout<<"X created\n";}
virtual ~X(){std::cout<<"X destroyed\n";}
void show(){std::cout<<"show()\n";}
};
int main()
{
X *xp = new X;
{
std::shared_ptr<X> xs1(xp);
}
if(xp)
xp->show();
return 0;
}
The output looks like this after g++ -std=c++14
X created
X destroyed
show()
xp
should be nullptr
but still xp->show()
works. Why ?