如何使 python class 满足 SWIG 中的接口?

How do I make a python class satisfying an interface in SWIG?

我想在 Python 中创建一个使用 SWIG 满足 C++ 实例的对象。

鉴于我有一个例子 Example.h:

struct iCat
{
    virtual int paws() const = 0;
};

int pawGiver(const iCat& cat);

struct cat : public iCat
{
    int paws() const
    {
        return 4;
    }
};

Example.cpp

#include "Example.h"
int pawGiver(const iCat& cat)
{
    return cat.paws();
}

example.i

/* File : example.i */
%module example
%{
#include "Example.h"
%}
%include "Example.h"

以上当然编译好了。我写了下面的内容来尝试在 Python 中创建一个 iCat,即:

import example;
class pyCat(example.iCat):
     def __init__(self):
             super().__init__()
     def paws(self):
             return 3;

z = pyCat()
example.pawGiver(z)

我想做的事情有可能吗? Python class 可以实现 C++ 实例吗?我做错了什么?

很简单。修改界面为:

/* File : example.i */
%module(directors="1") example
%{
#include "Example.h"
%}
%feature("director");
%include "Example.h"

运行良好。