Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It's not clear how you are going to use <code>stuff</code> in your example, so I'm going to give you a few different options. </p> <ol> <li><p><code>thing</code> stores its own copy of <code>stuff</code>.<br> In this case your class stores an object of type <code>std::list&lt;char*&gt;</code>.</p> <pre><code>class thing { public: thing(std::list&lt;char*&gt;&amp; aList):m_list(alist){} std::list&lt;char*&gt; m_list; }; </code></pre> <p>When you construct <code>thing</code> copy of <code>stuff</code> is made and stored in the class. When the object is destructed it will automatically deallocate <code>m_list</code>. </p></li> <li><p><code>thing</code> stores a weak reference to <code>stuff</code>.<br> Your class will store a pointer (<code>std::list&lt;char*&gt;* m_list</code>) or reference (<code>std::list&lt;char*&gt;&amp; m_list</code>). <code>thing</code> will be able to use your list in any way, but it shouldn't be in charge of resource management. If the list is in a smaller scope than <code>thing</code> then you'll be left with a hanging reference.</p> <pre><code>thing getThing() { std::list&lt;char*&gt; list1; thing object1(list1); return object1; //bad - list will be deallocated, object1 will have a hanging reference } </code></pre></li> <li><p><code>thing</code> stores a shared pointer to <code>stuff</code>. This is the method that is most like <code>retain</code> in Objective C. C++ doesn't have automatic reference counting. If you want to store an object reference with shared ownership you can use an <code>std::shared_ptr</code>. <code>thing</code> stores <code>std::shared_ptr&lt;std::list&lt;char*&gt;&gt; m_list</code>.</p> <pre><code>std::shared_ptr&lt;std::list&lt;char*&gt;&gt; stuff = std::make_shared&lt;std::list&lt;char*&gt;&gt;(); thing object(stuff); //reference count incremented, even if stuff is destroyed object will still hold a valid reference </code></pre></li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload