[Rcpp-devel] Create an instance of a reference class for a C++ class exported as a module from within C++?

Christian Gunning xian at unm.edu
Fri Jun 3 13:03:15 CEST 2011

> At the moment the only way i see is to make an R call to "new" and construct the object this way...

>> Many of the methods for this class return other members of this class.
>> Can I create a new instance of the reference class in R from within
>> the module?  I'm sure it is possible, I'm just wondering if there is
>> an easy way to do this without duplicating large chunks of the code in
>> Rcpp/src/Module.cpp.  Can i somehow clone the SEXP representing the
>> current instance then swap the external pointer to the C++ object?

To elaborate a bit:

Am I off-base in thinking you can make a custom constructor for your
class that calls R's new(), being careful to avoid circularity, that
returns a blank instance to your method to be filled? Is something
like the following anywhere near this?

class MyClass {
    Bar(bool x_) : {}  // for spawn
    Bar() : {}

    Bar spawn() {
      Environment meth("package:methods");
      Function Rnew = meth["new"];
      Rcpp::XPtr<MyClass> NewObj( Rnew(false) );

A man, a plan, a cat, a ham, a yak, a yam, a hat, a canal – Panama!

More information about the Rcpp-devel mailing list