Skip to content
Prev 2432 / 10988 Next

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

I played with this a little more, and the following works fine.
Nothing striking here. The part I don't understand is, can you employ
the SEXP returned by spawn() as an instance of Bar, or are you then
stuck with the R interface to Bar?

-Christian

## in R
Rcpp.package.skeleton('testmod', module=T)

//in testmod/src/rcpp_module.cpp

#include <Rcpp.h>
using namespace Rcpp;
class Bar {
public:
    Bar()  {}
    void set(int state_) { this->state = state_; }
    int get() { return state; }

    SEXP spawn() {
        Environment meth("package:methods");
        Function Rnew = meth["new"];
        Function Rget("get");
        SEXP NewObj( Rnew(Rget("Bar")) );
        return NewObj;
    }
private:
    int  state;
};

## in R
require(testmod)
bb = new(Bar); bb$set(3)
cc = bb$spawn();  cc$set(4)
bb$get(); cc$get()