我有一个C++类( Scenario
Scenario
struct PyScenario { PyObject_HEAD Scenario scen; };
我想揭露 对于Python脚本—不使用显式getter/setter函数:
static PyMemberDef scenarioMembers[] { { (char *)"a", T_DOUBLE, offsetof(PyScenario, scen._a), "The a" }, { (char *)"b", T_INT, offsetof(PyScenario, scen._b), "The b" }, NULL };
'double Scenario::_a' is private within this context .
'double Scenario::_a' is private within this context
_a 和 _b PyMemberDef 一 friend 脚本
_a
_b
PyMemberDef
friend
脚本
class Scenario { #ifdef PY_MAJOR_VERSION friend struct PyMemberDef; #endif private: double _a; int _b; .... };
不幸的是,这没有帮助。。。正确的方法是什么?
您所做的工作的基本问题是(可能)全局变量的初始值设定项位于全局上下文中。初始值设定项既不是类的一部分,也不是函数的一个组件,因此它不能是任何东西的朋友。
static 变量。包含变量的函数可以是类的朋友。
static
PyMemberDef *PyMemberInitializers() { static PyMemberDef scenarioMembers[] { { (char *)"a", T_DOUBLE, offsetof(PyScenario, scen._a), "The a" }, { (char *)"b", T_INT, offsetof(PyScenario, scen._b), "The b" }, NULL }; return scenarioMembers; }