[Rcpp-devel] constructor with many parameters in Rcpp module

Dirk Eddelbuettel edd at debian.org
Tue Nov 1 05:01:06 CET 2011


On 31 October 2011 at 21:14, andre zege wrote:
| Hi everybody, my apologies in advance if i missed something obvious in
| documentation or misinterpreted things. I started  experimenting with Rcpp
| modules and it seems to do in simpler cases exactly what i need. However, I am
| stuck on something that is probably very common
| 
| I am using the class for which constructor has 11 parameters, some of which are
| pointers to doubles.
| 
| Strategy s(double *, double *, double *, double *, double *, int, int, int,
| double, double, double)
| 
| Pointers  point to memory containing matrices that armadillo code inside the
| class Strategy works works with. According to what i read in rcpp module docs,
| a class could be defined in rcpp module only if constructor has six or less

Can you nest it? Just define a new

class StrategyParameters {
private:
  int a,b,c,d,e;
  double f,g,h,i,j,k};
public:
  // some getters/setters and ctors as needed	  
}

and pass that one?  Counts as just one...

| parameters. So it seems that the only way for me to use the class from R is to
| write a Rcpp function that would get called from R with a signature
| 
| RcppExport void helper(SEXP, SEXP, ...., SEXP){
| 
| //convert input parameters
| 
| 
| Strategy s(....);
| 
| //do whatever needs to be done in Strategy
| 
| }
| 
| and  call constructor  within C++ code and then return to R. Is this the best
| way to use Rcpp  things in my circumstance?
| 
| 
| Also, if i take this approach, how  would I pass pointers to double to a C++
| constructor? It seems i need to convert SEXP that i receive from R to double *
| in Rcpp framework -- it seems that as<>() template wouldn't convert to a
| pointer.  I could use of course .C interface, but i wanted to use Rcpp for a
| number  of C++ classes in a uniform way, regardless of the number of
| parameters in the constructor.

Modern C++ style mostly aims to avoid pointers like that. You probably meant
a double array passed as a pointer, a there a std::vector<double> is almost
always preferable.

Dirk

-- 
"Outside of a dog, a book is a man's best friend. Inside of a dog, it is too
dark to read." -- Groucho Marx


More information about the Rcpp-devel mailing list