On 28 May 2013 at 23:14, Anwar Ludin wrote: | Hello, | | I have a Reference class defined in R that I am passing as a parameter | to a C++ class using Rcpp. I am trying to access the fields of the | Reference class from C++ and I'm not sure how to do this.
Well for starters your reference class object had no field price so that makes extracting it hard... But in essence: a) reference classes are S4 objects, and as b) you can see from str() and unclass(), they contain an environment you can access. So try this modification of your code: library(Rcpp) library(methods) Instrument <-setRefClass( Class="Instrument", fields=list("id"="character", "description"="character") ) Instrument$accessors(c("id", "description")) instrument <- Instrument$new(id="AAPL", description="Apple") cat("Instrument:\n") print(instrument) #print(str(instrument)) cat("\n\nInstrument unclassed:\n") print(unclass(instrument)) cat("\n\nInstrument .xData env.:\n") print(ls(instrument@.xData)) cppFunction(' std::string getId(S4 obj) { // get the environment Environment e = obj.slot(".xData"); // extract field std::string txt = as<std::string>(e["id"]); // return it return txt; }') cat("\n\nId extracted from instrument:\n") print(getId(instrument)) Hope this helps, Dirk -- Dirk Eddelbuettel | e...@debian.org | http://dirk.eddelbuettel.com _______________________________________________ Rcpp-devel mailing list Rcpp-devel@lists.r-forge.r-project.org https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel