【发布时间】:2011-10-03 20:43:45
【问题描述】:
我正在使用 boost python 来创建与 c++ 库的绑定。这个库中的许多类都有接受 iterator/const_iterator 类型作为参数的虚方法。我不是特别想公开这些类型,但更愿意围绕这些接受适当容器的虚拟方法创建一些包装器。我的问题是,在“默认实现”函数中进行这种包装是否安全?
例如
class Test
{
public:
Test();
virtual ~Test();
virtual void iterate(std::vector<int>::iterator it);
};
然后用包装类包装默认..
struct Test_wrapper: Test, boost::python::wrapper<Test>
{
.....
virtual void iterate(std::vector<int>::iterator it);
void default_iterate(std::vector<int> it)
{
Test::iterate(it.begin());
}
};
并设置绑定...
boost::python::class_< Test_wrapper >("Test")
.def("iterate" ,(void ( Test_wrapper::* )(std::vector<int>))(&Test_wrapper::default_iterate));
我对此不确定,因为教程说需要将两个函数传递给“def”,但只传递一个似乎可行.. (http://www.boost.org/doc/libs/1_43_0/libs/python/doc/tutorial/doc/html/python/exposing.html#python.class_virtual_functions)
对此的任何建议将不胜感激。
在此先感谢,
巴巴克
编辑:
更具体地说,我正在尝试绑定一个包含方法“voxelToWorld”的类。此方法根据 vsP/end 中的点转换 wsP 中的位置。我想把这个函数包装起来,让它的界面更“pythonic”,但是我不确定在保持虚拟的同时做这件事的正确方法。
class FieldMapping
{
public:
...
virtual void voxelToWorld(std::vector<V3d>::const_iterator vsP,
std::vector<V3d>::const_iterator end,
std::vector<V3d>::iterator wsP);
};
【问题讨论】:
标签: c++ python boost-python virtual-functions