Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Convert your container to use <code>shared_ptr</code> as the member:</p> <pre><code>#include &lt;boost/ptr_container/ptr_vector.hpp&gt; #include &lt;boost/shared_ptr.hpp&gt; #include &lt;vector&gt; class Object { public: int m_Id; Object(int id) : m_Id(id) { } }; class ObjectManager { private: typedef boost::shared_ptr&lt;Object&gt; ObjectPtr; typedef std::vector&lt;ObjectPtr&gt; ObjectVectorPtr; typedef ObjectVectorPtr::iterator ObjectIt; ObjectVectorPtr vector_; void AddObject(ObjectPtr&amp; obj) { vector_.push_back(obj); } ObjectPtr FindObject(int id) { for (ObjectIt it = vector_.begin(); it != vector_.end(); ++it) { if (it-&gt;get()-&gt;m_Id == id) { // Found the object - return a shared_ptr reference to it return ObjectPtr(*it); } } // We could not find anything. return ObjectPtr(); } }; </code></pre> <p>btw - prefer <code>++it</code> vs <code>it++</code> to avoid extra construction, and don't use matching like this if the container gets large - switch to <code>std::map&lt;int, ObjectPtr&gt;</code> with <code>m_id</code> as the key, or <a href="http://msdn.microsoft.com/en-us/library/e8wh7665(VS.80).aspx" rel="nofollow noreferrer">std::set</a> with suitably-defined <code>less</code> function.</p> <p>If I was being super-pedantic I would suggest replacing your find loop with a call to <a href="http://msdn.microsoft.com/en-us/library/wkwky1wa(v=VS.90).aspx" rel="nofollow noreferrer">std::find_if</a>, with a predicate that matches on <code>Object::m_id</code>...</p>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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