c++ - Boost::Python 中的运算符=

标签 c++ python boost-python

如果我有类似下面的类

class Foo
{
private:
    int _bar;
public:
    Foo& operator=( const Foo& other )
    {
        _bar = other._bar;
        return *this;
    }
}

有没有一种使用 boost::python 将该功能导出到 python 的简单方法?文档没有列出,很好很容易

.def( self = self )

我不是 python 专家,所以老实说我什至不知道这是否有必要。但是我想在我的 python 脚本中使用这个功能,所以我发布这个问题只是为了确定。

编辑:

这是我执行 .def( self = self ) 时的编译器错误

.\src\Python.cpp(12) : error C2780: 'boost::python::class_<W> &boost::python::class_<W>::def(const char *,Fn,const A1 &,const A2 &,const A3 &)' : expects 5 arguments - 1 provided
        with
        [
            W=Foo
        ]
        depends\common\include\boost/python/class.hpp(265) : see declaration of 'boost::python::class_<W>::def'
        with
        [
            W=Foo
        ]
.\src\Python.cpp(12) : error C2780: 'boost::python::class_<W> &boost::python::class_<W>::def(const char *,Fn,const A1 &,const A2 &)' : expects 4 arguments - 1 provided
        with
        [
            W=Foo
        ]
        depends\common\include\boost/python/class.hpp(249) : see declaration of 'boost::python::class_<W>::def'
        with
        [
            W=Foo
        ]
.\src\Python.cpp(12) : error C2780: 'boost::python::class_<W> &boost::python::class_<W>::def(const char *,A1,const A2 &)' : expects 3 arguments - 1 provided
        with
        [
            W=Foo
        ]
        depends\common\include\boost/python/class.hpp(242) : see declaration of 'boost::python::class_<W>::def'
        with
        [
            W=Foo
        ]
.\src\Python.cpp(12) : error C2780: 'boost::python::class_<W> &boost::python::class_<W>::def(const char *,F)' : expects 2 arguments - 1 provided
        with
        [
            W=Foo
        ]
        depends\common\include\boost/python/class.hpp(233) : see declaration of 'boost::python::class_<W>::def'
        with
        [
            W=Foo
        ]
.\src\Python.cpp(12) : error C2784: 'boost::python::class_<W> &boost::python::class_<W>::def(const boost::python::def_visitor<Derived> &)' : could not deduce template argument for 'const boost::python::def_visitor<Derived> &' from 'boost::python::self_ns::self_t'
        with
        [
            W=Foo
        ]
        depends\common\include\boost/python/class.hpp(223) : see declaration of 'boost::python::class_<W>::def'
        with
        [
            W=Foo
        ]

最佳答案

我不是 Python 专家,但在 Python 中,运算符“=”的矫揉造作与 C++ 中的含义不同:a=b 创建对同一内部对象的新引用,所以将 c++ 的 operator= 导出到 python 接口(interface)是没有意义的。
您可以做的是创建一个“克隆”成员函数(根据 operator= 实现),它将返回对象的拷贝。并将此函数导入 Python。
或者,在 Python 中,您可以使用复制构造函数:foo2 = Foo(foo1)(当然这个构造函数必须在 c++/python 接口(interface)中定义)

关于c++ - Boost::Python 中的运算符=,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1930610/

相关文章:

c++ - 使用模板参数在模板类中定义一对迭代器

c++ - 通过 Boost.Python 将 C 风格的数组数据成员暴露给 Python

c++ - "class std::map used without template paramaters"错误

python - 你如何使用 Hypothesis @given 参数化一个 pytest 类?

python - 我是否正确解析了这个 HTTP POST 请求?

c++ - 在 QtCreator 中调试使用 Boost.Python 构建的模块

boost - Homebrew 软件包之间缺少链接

c++ - 如何用C++编写二进制文件

python - SciPy构建/安装Mac Osx

c++ - 关于在 boost.python 中将 Python 二进制文件转换为 C++ 二进制文件的错误