OOP with Rcpp modules
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
The purpose of Rcpp modules has always been to make it easy to expose C++ functions and classes to R. Up to now, Rcpp modules did not have a way to declare inheritance between C++ classes. This is now fixed in the development version, and the next version of Rcpp will have a simple mechanism to declare inheritance.
Consider this simple example, we have a base class Shape with two virtual methods (area and contains) and two classes Circle and Rectangle) each deriving from Shape and representing a specific shape.
The classes might look like this:
And we can expose these classes to R using the following module declarative code:
It is worth noticing that:
- The area and contains methods are exposed as part of the base Shape class
- Classes Rectangle and Circle simply declare that they derive from Shape using the derives notation.
R code that uses these classes looks like this:
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.