Note that there are some explanatory texts on larger screens.

plurals
  1. POSwig and Python - different object instantation
    text
    copied!<p>I Have a question regarding swig wrapped objects generated on the Python side and wrapped objects generated on the C++ side. Suppose I have the following simple C++ class definitions</p> <pre><code>#include &lt;vector&gt; class Sphere { public: Sphere(){}; }; class Container { public: Container() : data_(0) {}; void add() { data_.push_back(Sphere()); } Sphere &amp; get(int i) { return data_[i]; } std::vector&lt;Sphere&gt; data_; }; </code></pre> <p>and the following swig setup</p> <pre><code>%module engine %{ #define SWIG_FILE_WITH_INIT #include "sphere.h" %} // ------------------------------------------------------------------------- // Header files that should be parsed by SWIG // ------------------------------------------------------------------------- %feature("pythonprepend") Sphere::Sphere() %{ print 'Hello' %} %include "sphere.h" </code></pre> <p>If I then do the following in Python</p> <pre><code>import engine sphere_0 = engine.Sphere() container = engine.Container() container.add() sphere_1 = container.get(0) </code></pre> <p>Then the first instantiation of the wrapped Sphere class does call the <strong>init</strong> method of the Python wrapping interface ('Hello' is printed).</p> <p>However, the second, where the instance is generated on the C++ side does not ('Hello' is not printed).</p> <p>Since my goal is to be able to add additional Python functionality to the object upon its construction, I'd be pleased to hear if anybody has any pointers for a correct approach to achieve this - for both of the above instantiation scenarios.</p> <p>Best regards,</p> <p>Mads</p>
 

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