我有一个类有一个setter方法,它以unique_ptr作为参数.unique_ptr保存为类成员.
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);
}
};
Run Code Online (Sandbox Code Playgroud)
哪种方式是设置智能指针的正确方法?Way2是否泄漏sp的原始指针?