namespace test_py
{
class Event
{
public:
enum Type { BEGIN = 0, RESULT, END };
Type get_type() const { return m_type; }
protected:
Event() { }
~Event() { }
Type m_type;
};
class EventBegin : public Event
{
public:
EventBegin() { m_type = Event::BEGIN; }
~EventBegin() {}
};
class EventResult : public Event
{
public:
EventResult(int result) { m_type = Event::RESULT; m_result = result; }
~EventResult() {}
int get_result() { return m_result; }
protected:
int m_result;
};
class EventEnd : public Event
{
public:
EventEnd() { m_type = Event::END; }
~EventEnd() {}
};
class EventListener
{
public:
virtual void on_event(const Event& event) = 0;
};
struct EventListenerWrap: EventListener, py::wrapper<EventListener>
{
void
on_event(const Event& event)
{
this->get_override("on_event")(event);
}
};
BOOST_PYTHON_MODULE(test_py)
{
{
py::scope outer = py::class_< Event, boost::noncopyable >("Event", py::no_init)
.add_property("event_type", &Event::get_type);
py::enum_<Event::Type>("EventType")
.value("BEGIN", Event::BEGIN)
.value("RESULT", Event::RESULT)
.value("END", Event::END)
.export_values();
}
{
py::class_< EventBegin, py::bases<Event> >("EventBegin");
}
{
py::class_< EventResult, py::bases<Event> >("EventResult", py::no_init)
.def(py::init<int>((py::arg("result"))))
.add_property("result", &EventResult::get_result);
}
{
py::class_< EventEnd, py::bases<Event> >("EventEnd");
}
{
py::class_< EventListenerWrap, boost::noncopyable >("EventListener", py::no_init)
.def("on_event", py::pure_virtual(&EventListener::on_event));
}
}
}
我在事件基類中有一個受保護的構造函數和析構函數,並且無法更改它。 在Python 2.7中,我需要從EventListener類派生並將指針發送回C++代碼。 在編譯過程中我得到了錯誤這樣的:boost :: python受保護的析構函數問題
/boost/python/detail/destroy.hpp: In instantiation of ‘static void boost::python::detail::value_destroyer<false>::execute(const volatile T*) [with T = test_py::Event]’:
/boost/python/detail/destroy.hpp:95:36: required from ‘void boost::python::detail::destroy_referent_impl(void*, T& (*)()) [with T = const test_py::Event]’
/boost/python/detail/destroy.hpp:101:39: required from ‘void boost::python::detail::destroy_referent(void*, T (*)()) [with T = const test_py::Event&]’
/boost/python/converter/rvalue_from_python_data.hpp:135:71: required from ‘boost::python::converter::rvalue_from_python_data<T>::~rvalue_from_python_data() [with T = const test_py::Event&]’
/boost/python/converter/arg_from_python.hpp:107:8: required from ‘PyObject* boost::python::detail::caller_arity<2u>::impl<F, Policies, Sig>::operator()(PyObject*, PyObject*) [with F = void (test_py::EventListener::*)(const test_py::Event&); Policies = boost::python::default_call_policies; Sig = boost::mpl::vector3<void, test_py::EventListener&, const test_py::Event&>; PyObject = _object]’
/boost/python/object/py_function.hpp:38:33: required from ‘PyObject* boost::python::objects::caller_py_function_impl<Caller>::operator()(PyObject*, PyObject*) [with Caller = boost::python::detail::caller<void (test_py::EventListener::*)(const test_py::Event&), boost::python::default_call_policies, boost::mpl::vector3<void, test_py::EventListener&, const test_py::Event&> >; PyObject = _object]’
EventListener.cpp:193:1: required from here
EventListener.cpp:18:5: error: ‘test_py::Event::~Event()’ is protected
~Event() { }
^
In file included from /boost/python/converter/rvalue_from_python_data.hpp:10:0,
from /boost/python/converter/registry.hpp:9,
from /boost/python/converter/registered.hpp:8,
from /boost/python/object/make_instance.hpp:10,
from /boost/python/object/make_ptr_instance.hpp:8,
from /boost/python/to_python_indirect.hpp:11,
from /boost/python/converter/arg_to_python.hpp:10,
from /boost/python/call.hpp:15,
from /boost/python/object_core.hpp:14,
from /boost/python/object/class.hpp:9,
from /boost/python/class.hpp:13,
from ../../defs.hpp:6,
from ../defs.hpp:3,
from defs.hpp:3,
from EventListener.cpp:1:
/boost/python/detail/destroy.hpp:33:9: error: within this context
p->~T();
^
我沒有太多分析你的代碼,但是,你是否有任何機會將派生類的指針分配給基類指針,並嘗試通過基類指針來刪除它? –