Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pointers to C++ classes created with Rcpp Objects

Tags:

c++

r

rcpp

I'm attempting to port a discrete event simulation that I wrote with R's ReferenceClasses to C++ with the help of Rcpp. I've organized my code previously into various classes based on the need to access or modify the internal data and other characteristics. Previously, I was successful using ReferenceClasses which organized these subclasses into unit-level data and then another ReferenceClass to contain a List for all the objects in the simulation.

I had no trouble creating the base classes with Rcpp, but once I started creating a container class which used a constructor of elements of a sub-class my instance of R started crashing. Then I tried making the container class member a pointer which decreased the number of problems the compiler was having but still didn't work. I'm at a loss for what to do next...

Here's a short example of code that's not now working for me:

// forward declarations

class insider;
class container;

#include <Rcpp.h>

using namespace Rcpp;

class insider {
public:
  NumericVector a;
  NumericMatrix b;

  insider( NumericVector a_, NumericMatrix b_) : a( a_ ), b( b_ ) {}
  insider( const insider & i ) : a( i.a ), b( i.b ) {}

  NumericVector getA() { return a; }
  NumericMatrix getB() { return b; }
};

class container {
public:
  insider *ins;

  container(insider ins_) : ins( new insider( ins_ ) ) {}
  ~container() { delete ins; }
  insider getInsider() { return *ins; }
};

RCPP_EXPOSED_CLASS(insider)
RCPP_EXPOSED_CLASS(container)

RCPP_MODULE(mod) {
  class_<insider>("insider")
    .constructor< NumericVector, NumericMatrix >()
    .constructor< insider >()
    .method("getA", &insider::getA)
    .method("getB", &insider::getB)
  ;

  class_<container>("container") 
    .constructor< insider >()

    .method("getInsider", &container::getInsider)
  ;
}

/*** R
library(Rcpp)

insider <- mod$insider
container <- mod$container

a <- new(insider, 1:10, matrix(1:20, ncol = 2))
b <- new(container, a)

a$getA()
a$getB()

b$getInsider()$getA()
b$getInsider()$getB()

*/

Edit:

I've found a syntax error in the code (edited now, so code is up to date), and now the code compiles and works. That, in coordination with a typo was dragging me down for quite some time.

Original problem:

  insider(const insider & i) {
    a = i.a;
    b = i.b;
  }

Updated to:

  insider( const insider & i ) : a( i.a ), b( i.b ) {}

Original:

  container() : ins() {}
  void container_insider(insider ins_) { ins = new insider; *ins = ins_; }

Updated to:

  container(insider ins_) : ins( new insider( ins_ ) ) {}

Hope this ends up helping someone else down the road!

like image 647
RobSmith Avatar asked Jul 01 '26 15:07

RobSmith


1 Answers

Per the edit above, the problem was found with the 'insider' copy constructor & 'container' constructor, along with some minor issues with the original RCPP_MODULE macro call.

like image 182
RobSmith Avatar answered Jul 03 '26 05:07

RobSmith



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!