python - 如何使 python 类满足 SWIG 中的接口(interface)?

标签 python c++ 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 类可以实现 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/

相关文章:

c++ - boost::bind 和虚函数

c++ - 带有 Boost 的 CMake 找不到静态库

c# - 如何从我的 C# 单元测试中通过 SWIG 调试到 Quantlib

python - SWIG:如何使用 %apply 返回结构? 'No typemaps defined' 警告

python - 一个简单的 Python SWIG 错误问题

python - 使用 id、xpath、class 等单击按钮时遇到问题

python - 我不知道如何在 Python 中分散散点图 y 轴上的空间

c++ - Qt - 网格布局中的标签重叠

python - ImportError:无法导入名称解包

python - pandas 和 Stata 13 个文件