【发布时间】:2010-11-18 05:57:54
【问题描述】:
在我的 C++ 代码中,我有一个 Foo 类,其中包含许多将 Bar 类型变量作为参数的方法:
class Foo {
public:
void do_this(Bar b);
void do_that(Bar b);
/* ... */
};
Bar 有许多构造函数,可以从许多常见类型(例如 int、std::string、float 等)创建新对象:
class Bar {
public:
Bar(int i);
Bar(float f);
Bar(std::string s);
/* ... */
};
我用 Boost::Python 包装了它,现在我可以使用 Python 文字直接调用我的 Foo 方法,因为它们被隐式转换为 Bar 对象。
f = Foo()
f.do_this(5)
f.do_that("hello")
现在,我还希望能够使用其他 Python 类型,例如元组,如下所示:
f.do_that((1,2,3))
但我不想触及原始的 Bar 定义,也不想用 Boost::Python 的东西污染我的 C++ 库。我想在我的绑定代码中编写一个包装函数,但我就是不明白这是否可行,以及这样做的正确方法是什么。
换句话说:我可以注册一个工厂函数以在 Python 中用作自动转换吗?
【问题讨论】:
标签: c++ python boost-python