mercredi 5 septembre 2018

How to get offsetof() a private class-member?

I have a C++ class (Scenario) wrapped in a Python type:

struct PyScenario {
    PyObject_HEAD
    Scenario scen;
};

I'd like to expose some of the private members of Scenario to Python scripts -- without using the explicit getter/setter functions:

static PyMemberDef scenarioMembers[] {
    {
        (char *)"a",
        T_DOUBLE, offsetof(PyScenario, scen._a),
        "The a"
    },
    {
        (char *)"b",
        T_INT, offsetof(PyScenario, scen._b),
        "The b"
    },
    NULL
};

Unfortunately, when I compile the above, I get an error like: 'double Scenario::_a' is private within this context.

I don't want to make the _a and _b public, so I tried declaring the PyMemberDef a friend of Scenario:

class Scenario {
#ifdef PY_MAJOR_VERSION
    friend   struct PyMemberDef;
#endif
private:
    double   _a;
    int      _b;
    ....
};

Unfortunately, that didn't help... What's the right way?

Aucun commentaire:

Enregistrer un commentaire