如何制作一个满足SWIG中接口的python类

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

本文关键字:SWIG 接口 python 满足 一个 何制作      更新时间:2023-10-16

我想在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类可以实现C++实例吗?我做错了什么?

很简单。将界面修改为:

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

运行良好。