jeudi 23 juillet 2015

How to destroy a smart pointer prematurely

I have a class that has a setter method which takes a unique_ptr as an argument. That unique_ptr is saved as a class member.

class TestClass {
    std::unique_ptr<Tester> sp;

    void setTester_Way1(std::unique_ptr<Tester> te) {
         auto deleter=std::move(sp);
         sp=std::move(te);
    }

    void setTester_Way2(std::unique_ptr<Tester> te) {
         sp=std::move(te);
    }
};

Which way is the correct way to set the smart pointer? Does Way2 leak the original pointer of sp?

Aucun commentaire:

Enregistrer un commentaire