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
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 :)!