揭露非const但在加速Python的不可复制成员 [英] Expose a non-const but noncopyable member in Boost Python
问题描述
下面是我的问题:
我有两个类这样的:
class Signal {
public:
void connect(...) { sig.connect(...); }
private:
boost::signal2::signal sig;
};
class MyClass {
public:
Signal on_event;
};
我想揭露 MyClass的:: ON_EVENT
,这样我可以叫 my_class_instance.on_event.connect(...)
从Python的。
I would like to expose MyClass::on_event
so that I can call my_class_instance.on_event.connect(...)
from Python.
这是我是如何包装这些类:
This is how I've wrapped those classes:
class_<Signal, boost::noncopyable> ("Signal", noinit)
.def("connect", &some_helper_function);
class_<MyClass> ("MyClass")
.def_readonly("on_event", &MyClass::on_event);
这编译,但是当我尝试调用连接
在Python,我得到: AttributeError的:不能设置属性
。这是在这里解释的:的http://www.boost.org/doc/libs/1_53_0/libs/python/doc/tutorial/doc/html/python/exposing.html,所以我改成 .def_readwrite
为 ON_EVENT
。
This compiles, but when I try to call connect
from Python I get: AttributeError: can't set attribute
. This is explained here: http://www.boost.org/doc/libs/1_53_0/libs/python/doc/tutorial/doc/html/python/exposing.html, so I changed to .def_readwrite
for on_event
.
但现在我得到一个编译时错误信息。它几乎是不可能的阅读C ++模板的错误消息,但据我了解它,因为的boost :: signals2 ::信号
是不可复制。由于 .def_readwrite
使得成员分配它不能是不可复制。但是,对于我的使用,我不想要分配的成员,我只是wan't调用的方法。
But now I get a compile time error instead. Its an almost impossible to read C++ template error message, but as far as I understand it its because boost::signals2::signal
is noncopyable. Since .def_readwrite
makes the member assignable it must not be noncopyable. But for my usage I don't want to assign the member, I just wan't to call one method.
我想过使得连接
信号
常量,即使它改变了对象,但后来我的方法不能叫 sig.connect()
从该方法,所以这是一个没有去以及..
I thought about making the connect
method of Signal
const, even though it alters the object, but then I couldn't call sig.connect()
from that method, so that was a no go as well..
任何想法?
推荐答案
写了这个问题之后,我添加了一个公共的拷贝构造函数来信号,现在,它的工作原理。
After writing this question I added a public copy constructor to Signal and now it works.
这篇关于揭露非const但在加速Python的不可复制成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!