title: SWIG C++ Python: wrapping int by reference or pointer
body: <p>I'm trying to wrap some C++ functions into a Python wrapper.
  For this, it seems SWIG is a nice and easy way.</p>
  
  <p>Wrapping works, but I get a problem when passing integers by reference or by pointer.
  Since Python cannot work with references, SWIG internally converts these to pointers.</p>
  
  <p>Some simple example code:</p>
  
  <p>Blaat.hpp :</p>
  
  <pre><code>#ifndef __BLAAT_HPP__
  #define __BLAAT_HPP
  class Blaat
  {
  public:
   int mA;
   float mB;
  
  public:
   Blaat() {}
   void getA(int &amp; fA);
   void setA(const int fA);
   ~Blaat() {}
  };
  
  #endif // __BLAAT_HPP__
  </code></pre>
  
  <p>Blaat.cpp </p>
  
  <pre><code>#include "Blaat.hpp"
  #include &lt;iostream&gt;
  
  void Blaat::getA(int &amp; fA) {
   std::cout &lt;&lt; "[Blaat::getA] fA = " &lt;&lt; fA &lt;&lt; std::endl;
   fA = mA;
  } 
  
  void Blaat::setA(const int fA) {
   std::cout &lt;&lt; "[Blaat::setA] fA = " &lt;&lt; fA &lt;&lt; std::endl;
   mA = fA;
  }
  </code></pre>
  
  <p>Blaat.i:</p>
  
  <pre><code>%module Blaat
  %{
  /* Includes the header in the wrapper code */
  #include "Blaat.hpp"
  %}
  
  /* Parse the header file to generate wrappers */
  %include "Blaat.hpp"
  </code></pre>
  
  <p>Than convert the code into a Python wrapper:</p>
  
  <pre><code>#!/bin/sh
  swig -python -c++ -v $1.i 
  gcc -c $1_wrap.cxx -fPIC -I/usr/include/python2.6
  gcc -shared $1_wrap.o -o _$1&lt;library_path&gt; so -L. -l$1
  </code></pre>
  
  <p>This all works fine. Now, I start Python and do:</p>
  
  <pre><code>from Blaat import *
  a = Blaat()
  b = int(1)
  a.setA(b) &lt;-- fine, calls setA() function fine
  a.getA(b) &lt;-- does not work
  </code></pre>
  
  <p>At the "getA()" call, the following error occurs:</p>
  
  <pre><code>Traceback (most recent call last):
   File "&lt;stdin&gt;", line 1, in &lt;module&gt;
   File "Blaat.py", line 86, in getA
     def getA(self, *args): return _Blaat.Blaat_getA(self, *args)
  TypeError: in method 'Blaat_getA', argument 2 of type 'int &amp;'
  </code></pre>
  
  <p>Note that I get this problem both when passing the argument by reference and by pointer.
  Looking at the generated "Blaat_wrap.cxx" file, it stops at the actual type conversion:</p>
  
  <pre><code>res2 = SWIG_ConvertPtr(obj1, &amp;argp2, SWIGTYPE_p_int,  0 );
  if (!SWIG_IsOK(res2)) {
   SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "Blaat_getA" "', argument " "2"" of type '" "int &amp;""'"); 
  }
  </code></pre>
  
  <p>This means that the function SWIG_ConvertPtr() fails, which is strange because it seems that the type it checks for is SWIGTYPE_p_int.
  From the "setA()" function, we see that the type conversion works (if passing by value).</p>
  
  <p><a href="http://www.swig.org/Doc1.3/SWIGDocumentation.html#SWIGPlus_nn18" rel="nofollow noreferrer">The SWIG documentation tells me</a>):</p>
  
  <blockquote>
    <p>C++ references are supported, but SWIG transforms them back into
    pointers. For example, a declaration like this :</p>
    
    <p>class Foo { public:   double bar(double &amp;a); }</p>
    
    <p>has a low-level accessor</p>
    
    <p>double Foo_bar(Foo *obj, double *a) {     obj->bar(*a); }</p>
  </blockquote>
  
  <p>Can someone throw in the thing I'm missing? I'm quite stuck at this point...
  <a href="https://stackoverflow.com/questions/4383719/passing-bool-by-reference-using-swig-and-python">Found this post, but this did not help either</a></p>