C++0x unique_ptr replaces scoped_ptr taking ownership?

前端 未结 5 579
别跟我提以往
别跟我提以往 2020-12-08 21:24

I used to write code like this:

class P {};

class Q: public P {};

class A {
    // takes ownership
    A(P* p): p_(p) {}

    scoped_ptr

p_; }; A

5条回答
  •  粉色の甜心
    2020-12-08 22:01

    IMO it is better to use unique_ptr as it provides an additional feature: move semantics. i.e. you can write a move constructor, etc for your class, unlike scoped_ptr. Also, unique_ptr doesn't have an overhead associated with it as it is the case with scoped_ptr, so it is a superior facility. A decision of a rewrite is up to you of course, in case you don't need move semantics then there is no point of the rewrite. Don't forget that unique_ptr is from the standard library, so it must be provided with any compliant implementation of C++0x(when it becomes reality of course :)!

提交回复
热议问题