1

如果我有类似以下课程的内容

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
        ]
4

3 回答 3

4

您不需要将赋值运算符公开给 python。在 python 中,赋值运算符不会将现有对象重新分配为另一个对象的副本,而是将名称重新分配给不同的对象。在 python 中,您要么创建一个新对象,要么创建一个新对象,它是另一个对象的副本,这更类似于复制构造函数。

如果由于某种原因您需要从 python 调用 C++ 赋值运算符,您可能(自己还没有尝试过)添加一个成员函数来执行此操作:

.def("reassign", &Foo::operator=);

然后,您将在 python 中手动调用它:

f1 = Foo()
f2 = Foo()
f2.reassign(f1)
于 2009-12-18T21:52:04.440 回答
3

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

于 2009-12-18T21:48:30.487 回答
1

上一个直接使用的答案Foo::operator=不起作用。改为这样做:

void assignFoo(Foo& self, const Foo& other)
{
    self = other;
}

class_<Foo>("Foo")
    .def("assign", assignFoo);

用途python

foo, other = Foo(), Foo()
foo.assign(other)
于 2015-01-24T18:07:37.207 回答