我想在 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++ 实例吗?我做错了什么?
最佳答案
很容易。修改接口(interface)为:
/* File : example.i */
%module(directors="1") example
%{
#include "Example.h"
%}
%feature("director");
%include "Example.h"
运行良好。
关于python - 如何使 python 类满足 SWIG 中的接口(interface)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51641780/