jeudi 13 décembre 2018

why does delete trigger an error when use in "delete shared_ptr::get()"

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