【问题标题】:Passing a derived class to a function taking a base class in SWIG Python将派生类传递给采用 SWIG Python 中的基类的函数
【发布时间】:2013-08-01 03:51:37
【问题描述】:

我使用 Python 3.3 和 SWIG 2.0.10 将派生类传递给采用基类的函数。

相同的 SWIG .i 文件用于 C#,并且运行良好。但是,在 Python 中,SWIG 报告说没有接受派生类型的 C++ 方法 - 只有接受基类型的方法。该陈述是正确的,但我需要传递派生类型并让 SWIG 直接调用,就好像它是基类型一样。

派生类型在 C++ 中不存在。它仅存在于 Python(和 C#)中。但是,我们启用了导向器,并且如前所述,它在 C# 中运行良好。

Python 2.6 和 2.7 中的结果相同。

C++

class Base {};
// Note: there is NO "class Derived" in C++.
void f(Base* a) { ... }

Python

class Derived(Base): pass

x = Derived()
f(x) # SWIG runtime error - In C++ there is no f(Derived*) - there is only f(Base*)

【问题讨论】:

    标签: python swig


    【解决方案1】:

    问题是在 Derived 类中我没有正确调用 Base.__init__()。

    一旦我解决了这个问题,Swig 中的多态性就可以正常工作了。

    class Derived(Base):
        def __init__(self):
            super(Derived, self).__init__()
    

    【讨论】:

      猜你喜欢
      • 2015-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-18
      • 1970-01-01
      • 2023-04-07
      • 1970-01-01
      相关资源
      最近更新 更多