19

Like it has been written here Qt up to now has 8 specilized smart pointer classes. It looks like it is all you will ever need. However, in order to use any of these smart pointers your class must be derived from QObject which is not always convenient. Is there other implementations of smart pointers in Qt which work with arbitrary classes?

Seagull
  • 3,319
  • 2
  • 31
  • 37
alexkr
  • 4,580
  • 1
  • 24
  • 21
  • 8
    Actually, for most of those pointers you don't need to derive from QObject, I don't think. There are some other things your classes would need to handle, or derive from some other Q-classes, for some of them. AFAIK, only QPointer needs your class to be derived from QObject. That having been said, they do require the ability to use Qt in your project. – Caleb Huitt - cjhuitt Sep 26 '09 at 16:54

1 Answers1

19

Many Qt classes are derived from QObject, and while some of the built in smart pointer classes are related to QObject (or QSharedData), the QSharedPointer and QScopedPointer templates appear to allow pointers to anything.

More "modern" C++ (from C++11 onwards) has useful smart pointer types: https://stackoverflow.com/a/30143936/33987.

Beyond that, you'll find some smart pointer templates in Boost:

  • scoped_ptr - Simple sole ownership of single objects. Noncopyable.
  • scoped_array - Simple sole ownership of arrays. Noncopyable.
  • shared_ptr - Object ownership shared among multiple pointers.
  • shared_array - Array ownership shared among multiple pointers.
  • weak_ptr - Non-owning observers of an object owned by shared_ptr.
  • intrusive_ptr - Shared ownership of objects with an embedded reference count.
Joris Timmermans
  • 10,814
  • 2
  • 49
  • 75
Paul Dixon
  • 295,876
  • 54
  • 310
  • 348
  • This is right. STL also has auto_ptr. The question is about QT. – alexkr Sep 26 '09 at 16:53
  • 1
    Virtually everything in Qt using QObject as a base, so if you want something that works outside of that, Boost one place to go looking :) – Paul Dixon Sep 26 '09 at 16:55
  • 8
    Wow, i still use C++ like "C with classes" (thats C++ around 1990). That works, all this ugly unreadable smart pointers insaneness make me sick. – Lothar Sep 26 '09 at 17:24
  • 69
    Yeah, automatic memory management, eew, disgusting. How can we trust a C++ application if it doesn't have memory leaks? ;) – jalf Sep 26 '09 at 22:27