Python 没有默认构造函数的对象成员的Swig setter
Swig为没有默认构造函数的对象成员生成包装代码 要包装的代码:Python 没有默认构造函数的对象成员的Swig setter,python,c++,swig,Python,C++,Swig,Swig为没有默认构造函数的对象成员生成包装代码 要包装的代码: class Foo { public: Foo (int i); }; Class Bar { public: Bar(int i):foo(i) { ... } Foo foo; }; 生成的Swig设置程序: SWIGINTERN PyObject *_wrap_Bar_foo_set(PyObject *SWIGUNUSEDPARM(self), PyObject *
class Foo {
public:
Foo (int i);
};
Class Bar {
public:
Bar(int i):foo(i)
{
...
}
Foo foo;
};
生成的Swig设置程序:
SWIGINTERN PyObject *_wrap_Bar_foo_set(PyObject *SWIGUNUSEDPARM(self), PyObject *args) {
PyObject *resultobj = 0;
Bar *arg1 = (Bar *) 0 ;
Foo arg2 ; // -> swig generates a call to a non existing default constructor
...
然后,如果尝试编译包装器,我会得到一个错误,因为默认构造函数不存在:
error: no matching function for call to ‘Foo::Foo()’
请注意,getter生成也采用相同的方法
如何告诉swig生成接受Foo*或Foo&的setter
谢谢,
PabloSWIG从根本上支持这一点很好,事实上,我无法用您展示的代码重现您所看到的内容。例如,这一切都有效:
%module test
%inline %{
class Foo {
public:
Foo (int i) {}
};
class Bar {
public:
Bar(int i):foo(i)
{
}
Foo foo;
};
%}
在使用SWIG 3.0.2编译和运行时(现在已经很旧了!),让我运行以下Python代码:
import test
f=test.Foo(0)
b=test.Bar(0)
b.foo=f
print('Well that all worked ok')
即使在更一般的情况下,这种方法也能起作用,这是因为。本质上,这是为了通过将副本构造函数包装到另一个对象中来解决缺少副本构造函数的问题。(尽管在特定的实例中,您已经展示了它实际上并不需要)
无论如何,尽管这应该自动适用,但也有一些情况是不能适用的。幸运的是,使用%功能
您只需在.i文件中,在没有副本的类型的第一次声明/定义之前的某个位置包含以下内容:
%feature("valuewrapper") Foo;
就这样