这可能是简单的但我找不到任何解释。如何访问SWIG Python生成的抽象C++类方法?
给定一个抽象类,这是在别处实现,并且其界面通过导出函数提供:
class IFoo {
public:
virtual ~IFoo(){}
virtual void bar()=0;
};
extern IFoo* get_interface();
在C++中我会以此为:
IFoo* foo = get_interface();
foo->bar();
如果我SWIG此,我可以导入模块并将get_interface()分配给一个变量:
import myfoo
foo = myfoo.get_interface()
但是我无法接受SS foo.bar():
>>> foo.bar()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'SwigPyObject' object has no attribute 'bar'
我缺少什么?
谢谢!
这里是整个事情,如果上面的片段不够清晰:
的* .i文件:
%module myfoo
#define SWIG_FILE_WITH_INIT
%include "myfoo.h"
%{
#include "myfoo.h"
%}
的myfoo.h头:
class IFoo {
public:
virtual ~IFoo(){}
virtual void bar()=0;
virtual void release()=0;
};
extern IFoo* get_interface();
执行文件(myfoo.cpp)
#include "myfoo.h"
#include <iostream>
class Foo : public IFoo {
public:
Foo(){}
~Foo(){}
void bar();
void release();
};
void Foo::bar() {
cout << "Foo::bar()..." << endl;
}
void Foo::release() {
delete this:
}
IFoo* get_interface() {
return new Foo();
}
,你有“导演”
你需要用'%功能( “董事”)IFoo',但我觉得你失去了一些东西附加太多。你能包括一个完整的最小例子而不仅仅是选定的片段吗? – Flexo