3

I am passing a derived class into a function taking a base class using Python 3.3 and SWIG 2.0.10.

The same SWIG .i file is being used for C# and it works well. However, in Python, SWIG reports that there is no C++ method accepting the derived type - only a method accepting the base type. That statement is true, but I need to pass the derived type and have SWIG direct the call as if it was the base type.

The derived type does not exist in C++. It only exists in Python (and C#). However, we have directors enabled and, as stated, it is working fine in C#.

Same result in Python 2.6 and 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*)
4

1 回答 1

4

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

一旦我解决了这个问题,Swig 中的多态性就起作用了。

class Derived(Base):
    def __init__(self):
        super(Derived, self).__init__()
于 2013-08-01T23:43:32.913 回答