Suppose I have the following code:
Struct Foo{};
int main()
{
shared_ptr<Foo> pf = make_shared<Foo>();
delete pf.get();
//do anything else
return 0;
}
Why can't I use delete directly delete the Foo object? In my opinion, the problem is that after the smart pointer destructs, the delete will be called twice and leads to the undefined behavior.But why it triggers an error when I use the first time manually. I am confused, please help me.
Aucun commentaire:
Enregistrer un commentaire