Search code examples
qtboostqscopedpointer

QScopedPointer Vs boost::scoped_ptr [which one to choose]


If I had an option to choose between QScopedPointer and boost::scoped_ptr, which one would do most good in the following cases:

1. QScopedPointer<QObject> Vs boost::scoped_ptr<QObject>
2. QScopedPointer<QtContainer> Vs boost::scoped_ptr<QtContainer>
3. QScopedPointer::data() Vs boost::scoped_ptr::get()

Solution

  • They both do basically the same thing. The Qt version seems to have the ability to abscond with the pointer (QScopedPointer::take()), which allows you to transfer ownership to someone else. You can't do that with scoped_ptr, but you can swap both kinds.

    boost::scoped_ptr and QScopedPointer are also is explicitly non-copyable.

    QScopedPointer does have a mechanism that allows you to pass a "deleter" to the pointer. This is effectively a public static member of the given class, so QScopedPointer is still only the size of a pointer. It does mean that the type of QScopedPointer must include the deleter's type.

    Both of them are made obsolete by std::unqiue_ptr in C++0x.