5

These three are shared pointer classes from Qt, STL and Boost, respectively. They seem to be identical in functionality so I'm puzzled as to:

  1. What are advantages and disadvantages of each of them?
  2. Why do Boost and Qt versions even exist -- it was in STL already, why make your own?
  3. How should I choose which one to use?
Septagram
  • 9,425
  • 13
  • 50
  • 81
  • 2
    It wasn't "in STL already". TR1 based its shared_ptr on Boost. http://en.wikipedia.org/wiki/C%2B%2B_Technical_Report_1#Smart_pointers. I don't know about Qt. – Fred Larson Apr 13 '11 at 04:47
  • 1
    Because I'm not familiar with QT, this doesn't warrant an answer, but... 1. Always prefer the STL version unless you know it has bugs that affect you. 2. The boost version existed **years** before the other versions, and the STL version is based wholly on the boost version. 3. See #1. – ildjarn Apr 13 '11 at 04:53

2 Answers2

9

Look here for the answers to your questions.

Community
  • 1
  • 1
zkunov
  • 3,362
  • 1
  • 20
  • 17
8
  1. QSharedPointer requires Qt, shared_ptr is standard and portable
  2. std::shared_ptr is a standard replacement for boost::shared_ptr (that is, the boost one came first and it became standard)
  3. Don't use QSharedPointer unless you have a Qt class that requires it. If you have a tr1, or C++0x implementation use std::shared_ptr, otherwise used boost::shared_ptr.
edA-qa mort-ora-y
  • 30,295
  • 39
  • 137
  • 267
  • I don't think this answers any of his questions, seems i am the only one though – zkunov Apr 13 '11 at 06:14
  • Short of a long historical discussion of each of them this succinctly states the current state of affairs. Feel free to disagree with any of the points however. – edA-qa mort-ora-y Apr 13 '11 at 06:40
  • Oh it's correct and I agree, on second thought I think it answers partially, I might have overreacted a bit, but that's because I wanted to see some specific & important things to me. – zkunov Apr 13 '11 at 08:18
  • Thanks for your answer. Especially for #2, it solves Boost VS STL part. However: 1. Actually, I'm using Qt all over the place, that's why I'm asking the question in the first place (sorry for not mentioning above). Also, Qt is portable too. 3. Why? I do have Qt classes, they don't require it, but for the sake of consistency I am reluctant to mix STL stuff into Qt code (since Qt provides its own versions of pretty much everything) – Septagram Apr 14 '11 at 04:09
  • 1
    Don't be afraid to mix. The standard is standard and will always be there working. Qt is available on a lot of platforms, but not nearly as many as standard C++ is. That may not be an issue for you now however. Using standard code also lessens the learning curve for new people to your code. If you use only Qt and avoid the standard, people without Qt experience will be facing a huge hurdle to overcome. – edA-qa mort-ora-y Apr 14 '11 at 05:25