title: How to initialize a variable from a class method binding C++ with Swig
body: <p>I want to bind some C++ code to python thanks to swig.
  In my C++ code, I initialize some important variables via class methods.
  However, this kind of initialization seems to create some troubles to swig which returns <code>Error: Syntax error - possibly a missing semicolon.</code></p>
  <p>Below is a very simple example extracted from swig documentation, where I only add this kind of initialization from a method.
  This example is made of 3 files (<code>my_class.hpp</code> ; <code>my_class.cpp</code> ; <code>py_myclass.i</code>).</p>
  <p>my_class.hpp:</p>
  <pre class="lang-cpp prettyprint-override"><code>#ifndef MYCLASS_H
  #define MYCLASS_H
  
  class MyClass {
      public:
      float getSqr();
      float getSqr(float _v);
      
      private:
      float value=2.;
  };
  
  float toto=MyClass().getSqr(3.);     // the only line I added !
  
  #endif
  </code></pre>
  <p>my_class.cpp</p>
  <pre class="lang-cpp prettyprint-override"><code>#include &quot;my_class.hpp&quot;
  
  float MyClass::getSqr() {
      return getSqr(value);
  }
  
  float MyClass::getSqr(float value) {
      return value*value;
  }
  </code></pre>
  <p>py_myclass.i</p>
  <pre><code>%module py_myclass
  
  %{
  #include &quot;my_class.hpp&quot;
  %}
  
  %include &quot;my_class.hpp&quot;
  </code></pre>
  <p>So, the only modification I made from swig documentation was to add the initialization line <code>float toto=MyClass().getSqr(3.);</code>.
  I tried to play around a bit with it, but I always get the syntax error.</p>
  <p>From these files, I execute swig to create wrappers.
  I do it with the command line:</p>
  <pre><code>swig -python -c++ -o py_myclass_wrap.cpp py_myclass.i
  </code></pre>
  <p>which yields me the following error:</p>
  <pre><code>py_myclass.i:13: Error: Syntax error - possibly a missing semicolon.
  </code></pre>
  <p>So, is there a way to accomplish this kind of initialization with swig ?</p>
  <p>I also tried to add the line <code>%ignore toto;</code> before the last <code>%include &quot;my_class.hpp&quot;</code> in the file <code>py_myclass.i</code>, but it seems that the ignoring was ignored itself.</p>