2

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()
demonplus
  • 5,613
  • 12
  • 49
  • 68
Chenna V
  • 10,185
  • 11
  • 77
  • 104

1 Answers1

6

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.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • oh, so std::unqiue_ptr is preferred to any of the scoped pointers? and what about containers, do both behave the same way? – Chenna V Aug 18 '11 at 20:27
  • @blueskin: That's a separate question. So ask it separately. – Nicol Bolas Aug 18 '11 at 20:27
  • QScopedPointer *is* non-copyable: [Q_DISABLE_COPY(QScopedPointer)](http://www.google.com/codesearch#79Flfm07AwU/trunk/qt.mod/src/corelib/tools/qscopedpointer.h&q=QScopedPointer&type=cs&l=87) – Martin Ba Sep 06 '11 at 15:35
  • Wait, you are doing Qt? What's your opinion on Qt5's GL wrappers / enablers (if I may ask :))? – mlvljr Mar 24 '15 at 00:06