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

2024-04-19 20:55:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用Python制作一个对象,使用SWIG来对C++实例进行一个SASTIFY。你知道吗

我举了一个类似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++实例吗?我做错什么了?你知道吗


Tags: 实例returnincludeinitexampledefstructcat