问题:我使用 SWIG 在 python 中包装了一些 c++ 代码。在 python 方面,我想采用一个包装的 c++ 指针并将其向下转换为指向子类的指针。我在 SWIG .i 文件中添加了一个新的 c++ 函数来执行此向下转换,但是当我从 python 调用它时,我得到一个 TypeError。
以下是详细信息:
我有两个 C++ 类,Base 和 Derived。Derived 是 Base 的子类。我有第三个类,Container,它包含一个 Derived,并提供了一个访问器。访问器将 Derived 作为 const Base& 返回,如下所示:
class Container {
public:
const Base& GetBase() const {
return derived_;
}
private:
Derived derived_;
};
我已经使用 SWIG 将这些类包装在 python 中。在我的 python 代码中,我想将 Base 引用向下转换为 Derived。为此,我在 swig .i 文件中写入了一个使用 c++ 进行向下转换的辅助函数:
%inline %{
Derived* CastToDerived(Base* base) {
return static_cast<Derived*>(base);
}
%}
在我的 python 代码中,我称之为向下转换函数:
base = container.GetBase()
derived = CastToDerived(base)
当我这样做时,我收到以下错误:
TypeError: in method 'CastToDerived', argument 1 of type 'Base *'
为什么会发生这种情况?
作为参考,这里是 SWIG 生成的 .cxx 文件的相关位;即原始函数及其python接口化的doppelganger:
Derived* CastToDerived(Base* base) {
return static_cast<Derived*>(base);
}
// (lots of other generated code omitted)
SWIGINTERN PyObject *_wrap_CastToDerived(PyObject *SWIGUNUSEDPARM(self), PyObject *args) {
PyObject *resultobj = 0;
Base *arg1 = (Base *) 0 ;
void *argp1 = 0 ;
int res1 = 0 ;
PyObject * obj0 = 0 ;
Derived *result = 0 ;
if (!PyArg_ParseTuple(args,(char *)"O:CastToDerived",&obj0)) SWIG_fail;
res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_Base, 0 | 0 );
if (!SWIG_IsOK(res1)) {
SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "CastToDerived" "', argument " "1"" of type '" "Base *""'");
}
arg1 = reinterpret_cast< Base * >(argp1);
result = (Derived *)CastToDerived(arg1);
resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_Derived, 0 | 0 );
return resultobj;
fail:
return NULL;
}
任何帮助将不胜感激。
——马特