简体   繁体   中英

Using boost defined C++ Virtual functions from Python

I am trying to replicate some C++ code with Python where the python calls C++ functions defined with boost.

My problem comes where the original C++ code refers to a Virtual function.

The virtual functions are defined with no_init eg

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

If I try and use them from Python I get the error message that they cannot be called from python.

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

ie because of the no_init definition that means no constructors.

So my question is how is one supposed to use C++ Virtual functions from Python.

I have seen https://www.boost.org/doc/libs/1_55_0/libs/python/doc/tutorial/doc/html/python/exposing.html#python.class_virtual_functions

But this seems to be about overiding virtual functions with python.

Boost is version 1.65.1 I just happened to find some doc on boost and virtual functions that referred to 1.55. I am not sure the way it deals with Virtual functions has changed.

I am trying to develop a FreeCAD workbench and whilst FreeCAD is in the process of migrating to Python 3 the version of Python used by the daily update is Python version: 2.7.15rc1.

Not sure the oxygen documentation of classes is the latest version but G4VPhysicalVolume is documented at http://www.apc.univ-paris7.fr/~franco/g4doxy/html/classG4VPhysicalVolume.html . The boost definition is as my original post.

To provided a Complete and verifiable example is not possible without requiring somebody to download and build from source both FreeCAD and Geant4

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM