【问题标题】:Wrap int pointer member variable in boost.python/pyplusplus在 boost.python/pyplusplus 中包装 int 指针成员变量
【发布时间】:2012-04-06 18:23:15
【问题描述】:

如果我使用的是 boost.python 或 pyplusplus,我如何包装一个 int 指针,或者任何作为类成员变量的指针?

例如,我将如何包装以下类中的x

class Foo{
    int * x;
}

【问题讨论】:

    标签: c++ python boost boost-python py++


    【解决方案1】:

    首先,您需要向 Python 公开类和属性。

    #include <boost/python.hpp>
    
    BOOST_PYTHON_MODULE(mylib)
    {
        using namespace boost::python;
        class_<Foo>("Foo")
            .def_readwrite("x", &Foo::x);
    }
    

    在 Python 中调用类同样简单。

    >>> import mylib
    >>> fooObj = mylib.Foo()
    >>> fooObj.x = 3
    >>> print 'fooObj.x is ', fooObj.x
    fooObj.x is 3
    

    【讨论】:

    • 当我尝试这个时,我得到了:ArgumentError: Python argument types in None.None(Foo, int) did not match C++ signature: None(Foo {lvalue}, int*)跨度>
    • 另外,如果你尝试读取变量,你会得到:Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; TypeError: No to_python (by-value) converter found for C++ type: int*
    猜你喜欢
    • 2014-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-18
    • 1970-01-01
    • 2018-03-07
    • 2011-02-07
    相关资源
    最近更新 更多