【问题标题】:Access to private member variables/functions of a C++ class in Cython在 Cython 中访问 C++ 类的私有成员变量/函数
【发布时间】:2016-07-08 20:08:34
【问题描述】:

假设我有一个类 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?

【问题讨论】:

    标签: python c++ cython


    【解决方案1】:

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

    您可以尝试这样的技巧来覆盖“private”关键字:https://stackoverflow.com/a/424125

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-10
      • 1970-01-01
      • 2023-03-23
      • 2021-02-04
      • 2017-03-11
      • 1970-01-01
      • 2019-07-12
      相关资源
      最近更新 更多