Skip to content
Prev 1330 / 10988 Next

[Rcpp-devel] Designing Rcpp modules - allow access to fields or to field accessor methods?

Le 18/11/10 22:31, Douglas Bates a ?crit :
You can use .property instead of .field to short circuit x back to d_x.

Consider this class (which I believe follows the convention you mention):

class Simple {
public:

     Simple( double x ) : d_x(x){} ;

     const double& x(){ return d_x ;}

private:
     double d_x ;
		
} ;

In R, you'd want to be able to do :

r <- new( Simple, 10 )
r$x

but not :

r$x <- 20



Then .property is what you need:

RCPP_MODULE(mod){

	class_<Simple>( "Simple" )

	    .constructor(init_1<double>())
	    .property( "x" , &Simple::x )
	
	;
}


When the third argument is not given, the property is considered read only.

Romain