- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在使用 Rcpp 为 C++ 库实现 R 包。该库从一个抽象类实现了几个派生类。函数初始化新的派生类并返回它的指针作为抽象类。是否可以在不更改 C++ 库的情况下使用 Rcpp 为 R 获得类似的构造?
这是一个简化的可重现示例:
#include <iostream>
#include <string>
using namespace std;
// abstract class
class Base {
public:
virtual ~Base() {}
virtual std::string name() const = 0;
virtual Base* getNew() const = 0;
};
// derived class
class Derived: public Base {
public:
Derived() : Base() {}
virtual std::string name() const { return "Derived"; }
virtual Derived* getNew() const { return new Derived(); };
};
Base *newDerived( const std::string &name ) {
if ( name == "d1" ){
return new Derived ;
} else {
return 0 ;
}
};
int main( void ) {
Base* b = newDerived( "d1" ) ;
cout << b->name() << endl ;
return(1);
}
compiled 的输出代码是:Derived
我当前的版本使用 Rcpp::RCPP_MODULE
和 Rcpp::XPtr
。然而,这个版本不像 C++ 实现那样可用:
#include <Rcpp.h>
using namespace Rcpp;
//... Base and Derived class and newDerived function implementations ... //
// wrapper function for pointer
typedef Base* (*newDerivedPtr)(const std::string& name);
//[[Rcpp::export]]
Rcpp::XPtr< newDerivedPtr > getNewDerived(const std::string type) {
return(Rcpp::XPtr< newDerivedPtr >(new newDerivedPtr(&newDerived)));
}
RCPP_MODULE(mod) {
Rcpp::class_< Base >("Base")
;
Rcpp::class_< Derived >("Derived")
.derives<Base>("Base")
.default_constructor()
.method("name", &Derived::name)
;
}
执行示例:
(dv = new(Derived))
# C++ object <0x101c0ce20> of class 'Derived' <0x101b51e00>
dv$name()
# [1] "Derived"
(dvptr = getNewDerived("d1"))
# pointer: 0x101c82770> // WANTED: C++ Object <0x...> of class 'Base' <0x...>
最佳答案
让我重新表述一下这个问题:您有一个带有非公共(public)构造函数的 Derived
类。创建此类实例的唯一方法是通过工厂方法 newDerived
。此工厂方法不返回指向 Derived
的指针,而是返回指向 Base
的指针,Derived
是从中派生的。
如果这是正确的,那么您可以使用 expose class in Rcpp - factory instead of constructor 中所示的技术.不过,您必须确保公开 Base
类:
#include <Rcpp.h>
using namespace Rcpp;
// abstract class
class Base {
public:
virtual ~Base() {}
virtual std::string name() const = 0;
};
// derived class
class Derived1: public Base {
public:
Derived1() : Base() {}
virtual std::string name() const { return "Derived1"; }
};
// derived class
class Derived2: public Base {
public:
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_MODULE(mod) {
Rcpp::class_< Base >("Base")
.factory<const std::string&>(newBase)
.method("name", &Base::name)
;
}
/*** R
(dv1 <- new(Base, "d1"))
dv1$name()
(dv2 <- new(Base, "d2"))
dv2$name()
*/
输出:
> (dv1 <- new(Base, "d1"))
C++ object <0x1cd3e60> of class 'Base' <0x1ea3560>
> dv1$name()
[1] "Derived1"
> (dv2 <- new(Base, "d2"))
C++ object <0x1fbb9f0> of class 'Base' <0x1ea3560>
> dv2$name()
[1] "Derived2"
请注意,我添加了第二个派生类,删除了未使用的 getNew
方法,并重命名了 factor 方法。这种方式对我来说更现实。
原始答案,现在可以作为替代方案:我担心您将不得不手动完成 Rcpp 模块提供的所有自动化,即在 R 级别生成一个类并使用工厂方法进行初始化。以下是 Rcpp 模块小插图和 Rcpp 属性中代码的改编,以便于编码:
#include <Rcpp.h>
using namespace Rcpp;
// abstract class
class Base {
public:
virtual ~Base() {}
virtual std::string name() const = 0;
virtual Base* getNew() const = 0;
};
// derived class
class Derived1: public Base {
public:
Derived1() : Base() {}
virtual std::string name() const { return "Derived1"; }
virtual Derived1* getNew() const { return new Derived1(); };
};
// derived class
class Derived2: public Base {
public:
Derived2() : Base() {}
virtual std::string name() const { return "Derived2"; }
virtual Derived2* getNew() const { return new Derived2(); };
};
Base *newBase( const std::string &name ) {
if ( name == "d1" ){
return new Derived1 ;
} else if ( name == "d2" ){
return new Derived2 ;
} else {
return 0 ;
}
}
//[[Rcpp::export]]
Rcpp::XPtr< Base > getNewBase(const std::string type) {
return(Rcpp::XPtr< Base >(newBase(type)));
}
//[[Rcpp::export]]
std::string Base__name(Rcpp::XPtr< Base > ptr) {
return ptr->name();
}
/*** R
setClass("Base", representation( pointer = "externalptr"))
Base_method <- function(name) {
paste("Base", name, sep = "__")
}
setMethod("$", "Base", function(x, name) {
function(...) do.call(Base_method(name), list(x@pointer, ...))
})
setMethod("initialize", "Base", function(.Object, ...) {
.Object@pointer <- getNewBase(...)
.Object
})
(dv <- new("Base", "d1"))
dv$name()
(dv <- new("Base", "d2"))
dv$name()
*/
有趣的输出:
> (dv <- new("Base", "d1"))
An object of class "Base"
Slot "pointer":
<pointer: 0x19b83e0>
> dv$name()
[1] "Derived1"
> (dv <- new("Base", "d2"))
An object of class "Base"
Slot "pointer":
<pointer: 0x1c02600>
> dv$name()
[1] "Derived2"
请注意,我在这里使用了一个 S4 类,而 RCPP_MODULE
创建了一个引用类。人们也可能为此使用 RC。将是一个有趣的学习练习,因为到目前为止我还没有直接使用过 RC。另请注意,我使用的 XPtr
与您的完全不同。我不确定你想在那里包装什么。
关于c++ - 如何使用 Rcpp 公开抽象类的指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54469409/
我对 java 中的抽象有点困惑。 我查了很多页面说抽象就是数据隐藏(隐藏实现)。 我对抽象的理解是它是“部分实现”。只需在抽象类/接口(interface)中定义您需要的内容,然后扩展/实现它们并添
我是 Ruby 的新手,主要来自 C# 和 ActionScript 3(以及其他语言)。我对抽象功能很好奇。具体来说,包装和抽象 Ruby 的 FTP 和 SFTP 库。 我四处搜索,发现了一个名为
目录 Java基础知识(抽象) 抽象 抽象定义 abstract的使用 定义抽象类
这个月我花了一些时间与 Emacs Lisp 进行斗争,试图获得更好地满足我需求的自动缩进。令人惊讶的是,大多数缩进代码是多么低级。我只看到了很少的抽象,例如 搜索不在字符串或注释中的第一个正则表达式
我有以下内容: public abstract class Foo{ //contents of Foo // ... public class Bar extends
我有三个类(class)(A 类、B 类和 C 类)。 类 A 调用 B 的实例并运行 start()。 B类扩展了Thread,因此当调用start()时,run()方法中的任何内容都会被执行。 在
这个问题已经有答案了: Calling a subclass method from superclass (5 个回答) 已关闭 7 年前。 Klasse1是一个抽象类,有一个 foo()方法。 K
我有一个这样的函数: def test(): x = "3" # In actual code, this is computed if x is None: retu
我有两个基类之间的关系: public abstract class RecruiterBase { // Properties declare here // Constructors de
这是我第一次发帖,但我遇到了很多问题。我目前有一个带有标题的 AbstractDevice 类: public abstract class AbstractDevice> implements De
我有一个 MotorDefinition 类和一个名为 Motor 的抽象类: class MotorDefinition { public: MotorDefinition(int p1,
是否有任何方法可以在这种代码(sass)中制定 css 的抽象规则: #cottage-image-gallery input:nth-of-type(1):checked ~ label:nth-o
是否可以声明一个已知的基类型并允许传输所有派生类型? [ServiceContract] public interface IService { [OperationContract]
我目前正在为基于 Java 的文本游戏开发角色生成机制,但我遇到了一个问题,看不出哪里出了问题。我有一个“Character”类,它是抽象的,然后是另一个类“NPCharacter”,它是建立在这个之
抱歉,标题令人困惑。不太确定如何表达它,这可能是问题所在! 我正在寻找一个好的抽象来用于涉及并发线程的情况。 我已经接近了,但还不是很清楚。 稍微简化一下,我在 Android 手机上收集了两种传感器
提前感谢您阅读本文。我不完全理解如何/何时使用摘要,所以我试图在我从事的每个项目中考虑它,看看它是否会在某一天全部点击 Smile | :) 此外,可访问性级别(私有(private)、 protec
我正在探索用于生成 Web 内容的 XML -> XSLT -> HTML 模因。我的 XSLT 经验很少。 我很好奇 XSLT 中有哪些机制可用于处理抽象或“重构”。 例如,使用通用 HTML 和服
在这些谈话中 Nicholas Zakas和 Addy Osmani他们讨论了在构建大型 Javascript 应用程序时将外观模式用作沙箱的想法,以便将应用程序与底层基础库分离。 这种解耦理论上允许
我使用C++和CUDA/C,想为特定问题编写代码,但遇到了一个非常棘手的简化问题。 我在并行编程方面的经验不容忽视,但相当有限,我无法完全预见到此问题的特殊性。 我怀疑是否有一种方便甚至“轻松”的方式
假设我有: trait A class B extends A class C extends A 有没有办法配置类型参数: class Foo[AType <: A with canCreateIn
我是一名优秀的程序员,十分优秀!