Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>boost::python</code> knows all about <code>boost::shared_ptr</code>, but you need to tell it that <code>boost::shared_ptr&lt;A&gt;</code> holds an instance of <code>A</code>, you do this by adding <code>boost::shared_ptr&lt;A&gt;</code> in the template argument list to <code>class_</code>, more information on this 'Held Type' is <a href="http://www.boost.org/doc/libs/1_43_0/libs/python/doc/v2/class.html#HeldType" rel="noreferrer">here in the boost documentation</a>.</p> <p>To prevent instances being created from python, you add <code>boost::python::no_init</code> to the class_ constructor, so you end up with:</p> <pre><code>boost::python::class_&lt; A, boost::shared_ptr&lt;A&gt; &gt;("A", boost::python::no_init) //... .def, etc ; </code></pre> <p>In <em>general</em> you should not pass around shared pointers by reference, since if the reference to the shared pointer is invalidated, then the reference to which the shared pointer is pointing to is also invalidated (since taking a reference of the shared pointer didn't increment the reference counter to the pointed to object).</p> <p>It is perfectly safe to pass <code>boost::shared_ptr</code> objects around to and from python, reference counts (python and shared_ptr) will be correctly managed provided you don't change the <code>return_value_policy</code>. If you change the policy of a method exposed in python so that it returns a reference to a shared pointer then you can cause problems, just as passing shared pointers around by c++ references can cause problems.</p> <p>(Also, you should use <code>make_shared&lt;A&gt;(...)</code> in preference to <code>shared_ptr&lt;A&gt;(new A(...))</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. 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