Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>I can somewhat guarantee that no object would exists in multiple_selection or simple_selection if it is not in object_container first.</p> </blockquote> <p>If you 150% sure, than there is no need for smart ptr.</p> <p>Reason you may need it in this situation is debug, I think. In case you describe - multiple_selection and simple_selection is not <strong>shared_ptr</strong>, but <strong>weak_ptr</strong>.</p> <p>Code with <strong>error</strong>:</p> <pre><code> std::vector&lt;int*&gt; owner_vector; std::vector&lt;int*&gt; weak_vector; int* a = new int(3); owner_vector.push_back(a); weak_vector.push_back(a); std::for_each( owner_vector.begin(), owner_vector.end(), [](int* ptr) { delete ptr; } ); std::for_each( weak_vector.begin(), weak_vector.end(), [](int* ptr) { *ptr = 3; // oops... usage of deleted pointer } ); </code></pre> <p>You can catch it with smart pointers:</p> <pre><code> std::vector&lt;std::shared_ptr&lt;int&gt;&gt; owner_vector; std::vector&lt;std::weak_ptr&lt;int&gt;&gt; weak_vector; { auto a = std::make_shared&lt;int&gt;(); owner_vector.push_back(a); weak_vector.push_back(a); } std::for_each( owner_vector.begin(), owner_vector.end(), [](std::shared_ptr&lt;int&gt;&amp; ptr) { ptr.reset(); // memory delete } ); std::for_each( weak_vector.begin(), weak_vector.end(), [](std::weak_ptr&lt;int&gt;&amp; ptr) { assert(!ptr.expired()); // guarantee to be alive auto shared_ptr = ptr.lock(); *shared_ptr = 3; } ); </code></pre> <p>In last example you will have assert failed, but not undefined/segmentation fault. In not debug case you can disable shared_ptr <strong>overhead</strong>.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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.
 

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