[Rcpp-devel] Creating pointers to objects and wrapping them

schattenpflanze at arcor.de schattenpflanze at arcor.de
Mon Apr 18 10:04:39 CEST 2011


> Could you mock up a small example?
Here is a small, abstract example which demonstrates one of the 
scenarios where I would like to create and return object pointers. There 
are two classes, A and B. Instances of A contain pointers to instances 
of B. I would like to create the B objects pointed to by A on demand, 
and query A for these. (In the actual application, both A and B are 
rather complex mathematical objects, which require a lot of work to 
compute. They are not only created and stored, but also modified, 
swapped, removed, etc.)

This is the basic idea:

// B just stores an integer i here
class B {
     B(int i) : i(i) {}

     int get_i() {
       return i;

     int i;

// A contains a vector v of pointers to B
class A {
     A(int n) : v(n) {}

     SEXP foo(int j) {
       if (!v[j]) {
         v[j] = new B(j);

       return wrap(v[j]);

     std::vector<B*> v;

// Rcpp module exposing both A and B
   class_<A>( "A" )
             "Create an instance of A with a given number of pointers")
             "(Create and) get one of the objects of type B.")

   class_<B>( "B" )
     .constructor<int>("Create an instance of B from an int.")

In R, I would then like to do, e.g., this:

a <- new (A, 5)
b <- a$foo(0)

I could rephrase the question as follows: How do I create objects of the 
exposed classes within C++ in a way which allows me to return them to R?

>> Meta question:
>> Is there a search function for the Rcpp mailing list archive?
> I just constrain Google to, say, site:gmane.org to restrict the search to a
> particular site.  And I also use grep on my folder with the list messages.
OK, thank you.

Best regards,

Am 17.04.2011 10:45, schrieb schattenpflanze at arcor.de:
> Hello,
> I have made progress exposing my C++ classes to R with Rcpp-Modules and
> I really like the way it works. Thanks for this great package!
> There are, however, lots of aspects about Rcpp which I do not yet fully
> understand. Here is one of them:
> I have exposed a C++ class MyClass to R via an Rcpp module. Now I would
> like to write a C++ function, which creates (pointers to) objects of
> this class. The return type should ideally be a vector/list of pointers
> to the created C++ objects. The pointers should have the same type as
> those returned by the exposed constructors of MyClass in the module. In
> particular, I want R to do the memory management and garbage collection
> when I destroy the returned list of pointers.
> When I try to directly wrap an object of type MyClass or a pointer to it
> or a List containing it, I get compiler errors. So I suppose this is not
> the way to do it. I can create a vector of Rcpp::XPtrs and return it
> successfully to R, but then I cannot use the objects in R in the same
> way as those created by the constructor, namely object_pointer$method().
> Is there a way to achieve this or can you recommend a reasonable
> alternative design?
> How are XPtrs related to the objects a Rcpp-Module class constructor
> returns, if at all?
> Meta question:
> Is there a search function for the Rcpp mailing list archive?
> Thanks for your help,
> Peter
> _______________________________________________
> Rcpp-devel mailing list
> Rcpp-devel at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

More information about the Rcpp-devel mailing list