I have a C++ class that has a "clone" method which returns a distinct copy of the object. I want to expose the clone method in R and my question is how this is best done using Rcpp?
My first attempt to expose "clone" results in a compile error since Rcpp cannot automatically handle the returned object. Consider an example, class Cnorm { public: Cnorm() : z(0) {} Cnorm(double z_) : z(z_) {} double operator()( double x, double y ) const { return sqrt( x*x + y*y + z*z ); } Cnorm clone() const { return Cnorm(z); } private: double z; }; RCPP_MODULE(mod) { using namespace Rcpp; class_<Cnorm>("Cnorm") .constructor<double>("constructor") .method("clone",&norm::clone,"clone this object") .method("norm",&Cnorm::operator(),"take the norm"); } I found a solution which seems to work which involves adding a free function. I have two questions, however, 1) Is the workaround below reasonable/best? 2) Does Rcpp properly take ownership of the new'ed Cnorm object? SEXP cloner(Cnorm* self) { Cnorm* cpy = new Cnorm; *cpy = self->clone(); return Rcpp::internal::make_new_object(cpy); } RCPP_MODULE(mod) { using namespace Rcpp; class_<Cnorm>("Cnorm") .constructor<double>("constructor") .method("clone",&cloner,"clone this object") .method("norm",&Cnorm::operator(),"take the norm"); } _______________________________________________ Rcpp-devel mailing list Rcpp-devel@lists.r-forge.r-project.org https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel