首页 > 解决方案 > 使用 Rcpp 模块通过派生对象公开继承的方法

问题描述

我使用此答案中描述的方法创建了一个几乎为空的 Rcpp 项目。的内容rcpp_hello_world.cpp已替换为:

#include <Rcpp.h>
using namespace Rcpp;

class A {
public:
  virtual int foo() {
    return 1;
  }
};

class B : public A {
public:
  int bar() {
    return 2;
  }
};

// [[Rcpp::export]]
int fun() {
  B b;
  return b.foo();
}

RCPP_MODULE(mod_ab) {
  class_<A>("A").
    constructor().
    method("foo", &A::foo);

  class_<B>("B")
    .constructor()
    //.method("foo", &B::foo)
    .method("bar", &B::bar);
}

Bpublic 派生自A,因此应该可以像在 function 中那样从Athrough调用方法。如何通过 公开此方法?如果我取消注释该行Bfun()RCPP_MODULES

    //.method("foo", &B::foo)

我收到以下错误:

rcpp_hello_world.cpp: In function ‘void _rcpp_module_mod_ab_init()’:
rcpp_hello_world.cpp:31:27: error: no matching function for call to ‘Rcpp::class_<B>::method(const char [4], int (A::*)())’
     .method("foo", &B::foo)

是否有可能做到这一点,或者我应该创建一个B转发到其基类的方法并公开它?

标签: rinheritancercpp

解决方案


推荐阅读