Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documentation for .factory #937

rstub opened this Issue Feb 7, 2019 · 1 comment


None yet
2 participants
Copy link

rstub commented Feb 7, 2019

As far as I can tell the .factory method for Rcpp Modules is not documented yet. Recently I wrote the following example for a SO answer:

#include <Rcpp.h>
using namespace Rcpp;

// abstract class
class Base {
    virtual ~Base() {}
    virtual std::string name() const = 0;

// derived class
class Derived1: public Base {
    Derived1() : Base() {}
    virtual std::string name() const { return "Derived1"; }

// derived class
class Derived2: public Base {
    Derived2() : Base() {}
    virtual std::string name() const { return "Derived2"; }

Base *newBase( const std::string &name ) {
  if ( name == "d1" ){
    return new Derived1 ;
  } else if ( name == "d2" ){
    return new Derived2 ;
  } else {
    return 0 ;

  Rcpp::class_< Base >("Base")
    .factory<const std::string&>(newBase)
    .method("name", &Base::name)

/*** R
(dv1 <- new(Base, "d1"))
(dv2 <- new(Base, "d2"))

Would you be interested in a PR that adds something along these lines to the Rcpp modules vignette?


This comment has been minimized.

Copy link

eddelbuettel commented Feb 7, 2019

That sounds like a useful addition.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.