title: Accessing c++ object data members from R
body: <p>Apologies if this is a silly question as I am not a c++ or Rcpp expert but is it possible to access c++ data members from R? My attempt below fails:</p>
  
  <p>test.cpp</p>
  
  <pre><code>#include &lt;Rcpp.h&gt;
  using namespace Rcpp;
  
  class myclass {
  private:
    int k;
  public:
    myclass(int &amp;n) : k(n){}
    int getk() const {return k;}
  };
  
  typedef myclass* pmyclass;
  
  // [[Rcpp::export]]
  XPtr&lt;pmyclass&gt; new_myclass(NumericVector n_) {
    int n = as&lt;int&gt;(n_);
    myclass x(n);
    return(XPtr&lt;pmyclass&gt;(new pmyclass(&amp;x)));
  }
  
  // [[Rcpp::export]]
  NumericVector getk(SEXP xpsexp){
    XPtr&lt;pmyclass&gt; xp(xpsexp);
    pmyclass x = *xp;
    return wrap(x -&gt; getk());
  }
  </code></pre>
  
  <p>test.R</p>
  
  <pre><code>library(Rcpp)
  
  sourceCpp('./cpp_attempt/live_in_cpp/minimal_fail/test.cpp')
  
  ptr &lt;- new_myclass(10)
  
  getk(ptr)
  #19274768
  </code></pre>