Overload -> operator to forward member-access through Proxy

前端 未结 3 778
南方客
南方客 2021-01-13 04:16

I\'m trying to wrap a Python PyObject* in an Object class. In Python, everything is a PyObject*. A list is a PyObject*, a

3条回答
  •  难免孤独
    2021-01-13 05:14

    If you can change Object, you may add

    class Object {
    public:
        // other code
        const Object* operator -> () const { return this; }
        Object* operator -> () { return this; }
    };
    

    And for your Proxy

    Object operator->() { return container[key]; }
    

    So, for example

    myObj[42]->myFoo = ...
    

    is mostly equivalent to

    Proxy proxy = myObj[42];
    Object obj = proxy.operator ->();
    Object* pobj = obj.operator ->(); // so pobj = &obj;
    pobj->myFoo = ...
    

提交回复
热议问题