在 Rcpp 中检查 S4 对象的多重继承

问题描述

假设我在 R 中有以下两个类:

setClass("Person",representation(name = "character",age = "numeric"))
setClass("Employee",representation(boss = "Person"),contains = "Person")

我可以轻松地检查基 R 中的继承:

employee <- new("Employee",name = "Jack",age = 25)

inherits(employee,"Employee")
#> [1] TRUE
inherits(employee,"Person")
#> [1] TRUE

我无法在 Rcpp 中实现相同的效果

Rcpp::cppFunction('
void check_class(Rcpp::S4 x) {
  Rcout << "Class: " << as<std::string>(x.attr("class")) << std::endl;
  Rcout << "x.inherits(\\"Employee\\")     : " << x.inherits("Employee") << std::endl;
  Rcout << "x.inherits(\\"Person\\")       : " << x.inherits("Person") << std::endl;
  Rcout << "Rf_inherits(x,\\"Employee\\") : " << Rf_inherits(x,"Employee") << std::endl;
  Rcout << "Rf_inherits(x,\\"Person\\")   : " << Rf_inherits(x,"Person") << std::endl;
  Rcout << "R_extends : " << R_extends(x,Rf_mkString("Person"),R_GlobalEnv) << std::endl;
}')

check_class(employee)
#> Class: Employee
#> x.inherits("Employee")     : 1
#> x.inherits("Person")       : 0
#> Rf_inherits(x,"Employee") : 1
#> Rf_inherits(x,"Person")   : 0
#> R_extends :
#> Error in extends(new("Employee",boss = new("Person",name = character(0),: 'class1' must be the name of a class or a class deFinition

我希望 x.inherits("Person") 也是 true

如何检查对象是否继承自 Rcpp 中的父类

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...