【发布时间】:2020-08-12 12:52:05
【问题描述】:
如何使用 SWIG 从以下 c++ 代码处理 python 中的抽象方法映射:
class A : Base {
virtual int f() = 0;
};
class B : public A {
int f() { return 10 };
};
class C : public A {
int f() { return 20 };
};
std::map< std::string, std::shared_ptr<A>> my_map;
在python中,我也想做类似的事情:
my_B = B()
my_map["foo"] = my_B
或者可能更简单:
my_map["foo"] = B()
为了使用跨语言多态性,我必须明确 A 或 B 可以是主管类。
我的问题:
- 与此问题相关的最小 .i 文件可能是什么?
- 我还读到这会导致 python/C++ 棘手的所有权 例如,如果 my_B 被删除,则会出现问题。我怎样才能轻松转移 从 python 到 C++ 的“my_B”所有权?
非常感谢您的帮助
A.
【问题讨论】:
标签: python c++ abstract-class swig stdmap