title: C std::string as output parameters in Java with SWIG
body: <p>I need to wrap a C++ library with SWIG to use it with Java.</p>
  
  <p>I have already some methods working but I have encountered a situation that I don't know how to solve it.</p>
  
  <p>I have a couple of methods like this:</p>
  
  <pre><code>void method1(std::string &amp; name, std::string &amp; result);
  
  bool method2(std::string &amp; name, std::string &amp; alias, std::string &amp; resurnValue, std::string &amp; returnType);
  </code></pre>
  
  <p>Note: Actually this are member methods of a class named MyClass.</p>
  
  <p>I could change the first method to return a <code>std::string</code> instead of being <code>void</code>, and that should work; but I have no idea how to handle the second method where the two last parameters are output parameters. I have seen a couple of questions refereeing to <code>char *</code> output params (<a href="https://stackoverflow.com/questions/10061053/passing-multiple-parameters-and-allocating-strings-in-c-using-swig-python">Passing multiple parameters and allocating strings in C using Swig/Python</a>), but in my case should be a <code>std::string</code> and the Documentation of SWIG doesn't mention this situation <a href="http://www.swig.org/Doc2.0/Library.html#Library_std_string" rel="nofollow noreferrer">enter link description here</a>. Also I probably encounter more methods returning 3 or more output params, probably with different types.</p>
  
  <p>Finally I have a little of control about the interface, I'm also developing a class that acts as an entry point to the library, but it just passes call to the real implementation. </p>
  
  <p>For example by this I have managed to change a method like <code>method3(std::string &amp; s)</code> to <code>method3(const std::string &amp; s)</code>, so I could use it from Java with a normal <code>String</code>.</p>
  
  <p>So modifying a little bit the methods signatures is possible, but if a native method returns n outputs parameters I should return all of them (I can't create new methods to return each one).</p>
  
  <p><strong>Update:</strong>
  I have been looking at the solution given by Flexo and works great, however i'm considering doing a class to wrap std::string and using that to interact with returned strings, is a very similar approach to the second solution of Flexo, but using this StringWrapper instead of using a java String array, basically looks like this:</p>
  
  <pre><code>/*
  * The MyClass.i file
  */
  %module example
  
  %include "std_string.i"
  
  %{
      class StringPtr{
  
      private:
          stdString str;
  
      public:
          StringPtr(){
  
      }
  
          StringPtr(const stdString &amp; str){
          this-&gt;str = stdString(str);
          }
  
      stdString &amp; getStrRef(){
          return (this-&gt;str);
          }
  
          stdString getStrVal(){
          return stdString(this-&gt;str);
          }
  
          ~StringPtr(){
  
          }
      };
  
  
  %}
  
  /////////////////// Export StringPtr to Java
  
  class StringPtr{
  
      public:
          StringPtr();
  
      StringPtr(const stdString &amp; str);
  
      stdString getStrVal();
  
      ~StringPtr();
  
  };
  
  // I think this is nor necessary
  %rename ("$ignore", fullname=1) "StringPtr::getStrRef";
  
  %extend MyClass {
  
      void method1(cons std::string &amp; name, StringPtr &amp; result){
          $self-&gt;method1(name, result.getStrRef());
      }
  
      bool method2(cons std::string &amp; name, cons std::string &amp; alias, StringPtr &amp; returnValue, StringPtr &amp; returnType){
          $self-&gt;method2(name, alias, returnValue.getStrRef(), returnType.getStrRef());
      }
  
  };
  
  %rename ("$ignore", fullname=1) "MyClass::method1";
  %rename ("$ignore", fullname=1) "MyClass::method2";
  
  %include "MyClass.h"
  </code></pre>
  
  <p>So i'm wondering, from a performance point of view, witch is better, the structs solution (by Flexo), the string array by Flexo or this pointer (just like a struct with only one member.</p>