在最低级别,SWIG 处理 C++ class member variables通过生成一对访问器函数,例如来自:
class List {
public:
int length;
...
};
SWIG 创建:
int List_length_get(List *obj) { return obj->length; }
int List_length_set(List *obj, int value) {
obj->length = value;
return value;
}
我可以提供自己的访问器,并告诉 SWIG 使用它们在 Python 中创建“虚拟”成员变量吗?例如。从此:
class List {
public:
int setLength(int aLength) { _length = aLength; return _length; }
int getLength() { return _length; }
...
private:
int _length;
...
};
告诉 SWIG 让我这样做:
>>> l = List();
>>> print(l.length)
用Python?
最佳答案
我希望我没有侵犯任何版权。
来自http://swig.10945.n7.nabble.com/attribute-directive-and-C-templates-td11288.html
I recently discovered how to use
attribute.i
and the%attribute
directive to create attributes with customized behavior, as in the following (superfluous) example:%attribute(Point, int, x, getX, setX); %attribute(Point, int, y, getY, setY); class Point { public: int getX() { return _x }; void setX(int x) { _x = x }; int getY() { return _y }; void setY(int y) { _y = y }; private: int _x; int _y; }
关于python - 将我自己的 get/set C++ 成员函数视为 SWIG 中的成员变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31617191/