title: QSharedPointer, how to pass them around, and do I need them?
body: <p>Been trying to understand shared pointer for a few days now and it feels like I cant seem to get it. Not sure if it's just to obvious or if it's too complicated. First of all, could anyone please give me an example where you would ACTUALLY use shared pointers. The examples on Wikipedia makes no sense to me. And how would you pass a shared pointer to another function or create an object with a shared pointer. So, how do you pass it around and where would you use it? ANY information or examples would be great. </p>
  
  <p>Also, I have this issue where I don't know what to use. I have this function where I allocate a <code>QFile</code> and passes it to a function in another class. That function takes the file as a <code>QIODevice*</code> and then creates an object containing the file. I was wondering what the best solution would be here and how (if I should) use a shared pointer here? How can I make a shared pointer with <code>&lt;QFile&gt;</code> and pass it in where the function takes <code>&lt;QIODevice&gt;</code>. Feels like I don't get shared pointers at all... </p>
  
  <p>My other approach would be to put the allocation of the <code>QFile</code> in a <code>QScopedPointer</code>. I then pass it to the class and when creating the object where the file will be stored, I use <code>QPointer</code> or <code>QScopedPointer</code>. In the end of the first calling function I should call take() right?</p>
  
  <pre><code>function () {
     QScopedPointer&lt;QFile&gt; item(new QFile("filename"));
     SomeClassObject-&gt;doStuff(item.data());
     item.take();
  }
  ---------------------------------
  SomeClass::doStuff(QIODevice *item) {
      _currentObject = new MyObject(item); // should _currentObject be a smartpointer?
      ...
  }
  ---------------------------------
  class MyObject {
      QPointer&lt;QIODevice&gt; _item;
  
      ...
      MyObject(QIODevice *item) {  _item = item; }
  }
  </code></pre>
  
  <p>So I want a way to store pointers and a way to handle them during creation if "new" throws an exception. </p>