【发布时间】:2013-02-01 17:49:41
【问题描述】:
我有混合系统(c++,boost python)。 在我的 c++ 代码中,层次结构非常简单
class Base{...}
class A : public Base{...}
class B : public Base{...}
另外 2 个业务(在 c++ 上)方法
smart_ptr<Base> factory() //this produce instances of A and B
void consumer(smart_ptr<A>& a) //this consumes instance of A
在 python 代码中,我使用 factory 创建 A 的实例并尝试调用使用者方法:
v = factory() #I'm pretty sure that it is A instance
consumer(v)
绝对合理,我有例外:
consumer(Base) 中的 Python 参数类型与 C++ 签名不匹配:consumer(class A{lvalue})
发生这种情况是因为无法告诉 Boost 应该进行一些转换工作。
有什么方法可以指定动态转换行为吗? 提前谢谢你。
【问题讨论】:
-
@GHL 大多数智能指针声明都有很多提供所需语义的操作,例如在 boost 中:
template<class T, class U> shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const & r)或template<class T> ... template<class Y> explicit shared_ptr(Y * p)。所以我提到的“动态”更广泛,然后引用dynamic_cast运算符
标签: c++ boost-python