在Cython中访问C ++类的私有成员变量/函数

时间:2016-07-08 20:08:35

标签: python c++ cython

说我有一个班级Foo:

class Foo {
private:
    std::string bar;
public:
    Foo () {}
    Foo (const std::string& bar_) { this->bar = bar_; }
    std::string get_bar () { return this->bar; }
};

和一个Foo python包装器FooWrapper.pyx:

from libcpp.string cimport string

cdef extern from "Foo.h":
    cdef cppclass Foo:
        Foo ()
        Foo (string)

是否可以访问.pyx文件中的std::string bar,而无需修改Foo?

1 个答案:

答案 0 :(得分:1)

如果您无法使用C ++访问私有成员,那么您也无法在Cython中访问它。

你可以尝试这样的技巧来覆盖"私人"关键字:https://stackoverflow.com/a/424125