首页 > 解决方案 > 使用来自 Python 的 boost 定义的 C++ 虚函数

问题描述

我正在尝试使用 Python 复制一些 C++ 代码,其中 python 调用使用 boost 定义的 C++ 函数。

我的问题出现在原始 C++ 代码指的是虚拟函数的地方。

虚函数是用 no_init 定义的,例如

 class_<G4VPhysicalVolume, G4VPhysicalVolume*, boost::noncopyable>
     ("G4VPhysicalVolume", "physical volume class", no_init)
     // ---

如果我尝试从 Python 中使用它们,我会收到无法从 python 调用它们的错误消息。

   <type 'exceptions.RuntimeError'>: This class cannot be instantiated from Python

即因为 no_init 定义意味着没有构造函数。

所以我的问题是应该如何使用 Python 中的 C++ 虚拟函数。

我看过https://www.boost.org/doc/libs/1_55_0/libs/python/doc/tutorial/doc/html/python/exposing.html#python.class_virtual_functions

但这似乎是关于用 python 覆盖虚函数。

Boost 是 1.65.1 版,我碰巧找到了一些关于 1.55 的 boost 和虚函数的文档。我不确定它处理虚拟功能的方式是否发生了变化。

我正在尝试开发一个 FreeCAD 工作台,虽然 FreeCAD 正在迁移到 Python 3,但每日更新使用的 Python 版本是 Python 版本:2.7.15rc1。

不确定类的氧气文档是最新版本,但 G4VPhysicalVolume 记录在http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4VPhysicalVolume.html。提升定义是我的原始帖子。

如果不要求有人从 FreeCAD 和 Geant4 的源代码下载和构建,就不可能提供完整且可验证的示例

标签: pythonc++python-2.7boost

解决方案


好的,我发现我可以使用 G4PVPlacment 创建物理卷。


推荐阅读