简体   繁体   中英

Access to private member variables/functions of a C++ class in Cython

Say I have a class Foo:

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

and a Foo python wrapper FooWrapper.pyx:

from libcpp.string cimport string

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

Is it possible to access std::string bar in the .pyx file, without modifying Foo?

If you can't access a private member in C++, then you can't access it in Cython as well.

You could try a trick like this that overwrites the "private" keyword: https://stackoverflow.com/a/424125

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM