title: returning a custom object from a wrapped method in Rcpp
body: <p>I have the following problem with the Rcpp module:
  let's assume I've two classes in a Rcpp module</p>
  
  <pre><code>class A {
    public:
      int x;
  };
  
  class B
    public:
      A get_an_a(){
        A an_a();
        an_a.x=3;
        return an_a;
      }
  };
  
  RCPP_MODULE(mod){
    using namespace Rcpp ;
    class_&lt;A&gt;("A")
     .constructor()
     .property("x",&amp;A::get_x)
    ;
    class_&lt;B&gt;("B)
     .constructor()
     .method("get_an_A",&amp;get_an_a)
    ;
  }
  </code></pre>
  
  <p>.</p>
  
  <p>Right now compilation fails as it does not know what to do with the return type of A. </p>
  
  <p>I figured I could do something with Rcpp::Xptr, however, then I can't connect it to the S4 structure that Rcpp generated for the class A. I actually get an external pointer object from the method in R.</p>
  
  <p>Is there any possiblity to get a correctly wrapped object back from a method of the second class?</p>
  
  <p>Thanks,
  Thomas</p>
  
  <p>[edit]</p>
  
  <p>According to Dirk's answer I constructed a wrapper that can create the wrapped S4 object:</p>
  
  <pre><code>template &lt;&gt; SEXP wrap(const A &amp;obj) { // insprired from "make_new_object" from Rcpp/Module.h
    Rcpp::XPtr&lt;A&gt; xp( new A(obj), true ) ; // copy and mark as finalizable
    Function maker=Environment::Rcpp_namespace()[ "cpp_object_maker"];
    return maker ( typeid(A).name() , xp );
  }
  </code></pre>
  
  <p>Still, I don't know how to get the object back in as an parameter to a method/function. The following is not working:</p>
  
  <pre><code>template &lt;&gt; A* as( SEXP obj){
    Rcpp::List l(obj);
    Rcpp::XPtr&lt;A&gt; xp( (SEXP) l[".pointer"] );
    return (A*) xp;
  }
  </code></pre>
  
  <p>So how could I get the external pointer to the C++ object from the S4 object provided as SEXP in the parameter list?</p>