4

I have used Boost::shared_ptr in my previous projects and now I want to find a smart-pointer in Qt which does the same/similar thing. Since there are many smart-pointer classes in Qt I was wondering which one to use. Is it QSharedPointer?

demonplus
  • 5,036
  • 11
  • 41
  • 56
Ashika Umanga Umagiliya
  • 7,998
  • 19
  • 86
  • 167

3 Answers3

9

If you used boost::shared_ptr, continue using it. Prefer boost::shared_ptr to any Qt 'extensions' because boost::shared_ptr is accepted into the C++0x standard (std::shared_ptr).

Yakov Galka
  • 61,035
  • 13
  • 128
  • 192
  • Being accepted into the C++0x standard doesn't mean Qt "extensions" should be avoided. Qt is much older and generally more stable, consistent, and available. What if your compiler doesn't support C++0x because it is too old? What if the C++ standard library you're using didn't implement std::shared_ptr or implemented it wrong? Just because something is accepted as a standard doesn't mean all else should be thrown away. You have to look at each individual use case and determine what's the best fit for the job. – Jake Petroules Oct 15 '12 at 16:17
  • 2
    @JakePetroules: as the OP said he has already been using boost, so switching to something non-standard when the standard way is already employed is just ridiculous. (This also answers your "What if"—use boost.) And yes, Qt stinks like Java and .Net, still living in the 90s object oriented mindset. It also thinks that it is the only library on earth that everybody should use, thus feeling free to `#define` e.g. `foreach` and other common names that clash with everything else... – Yakov Galka Oct 15 '12 at 18:57
5

You want QSharedPointer. You'll find a comparison of all the pointer classes offered by Qt in the detailed description of QSharedPointer.

feedc0de
  • 3,260
  • 4
  • 26
  • 50
gnud
  • 73,015
  • 5
  • 56
  • 76
4

There is a bunch of different smart pointers in Qt, described in a blog post. QSharedPointer is most probably what you're looking for.

Bruce
  • 6,984
  • 1
  • 23
  • 41