简体   繁体   中英

How to access C++ baseclass functions using a derived Python swigged object?

I have a C++ class, say B, that is publicly derived from another class, A.

The B class is swigged and made accessible in Python. This works great, and all public functions defined in class B are readily available in Python without almost any work.

However, when creating a B object in Python the public functions that are defined in class A seem not to be exposed, available automatically.

Question is, how do one get access to functions up a C++ class hierarchy for a swigged Python object?

It seemed the answer was in the actual ordering of include files in the swig interface file.

By placing the baseclass headers BEFORE derived classes, everything seemed to start working as expected.

For anyone "swigging", seeing the warning;

warning 401: 'AnObjectXXX' must be defined before it is used as a base class.

will give you a python object without expected baseclass functionality.

You should get that without any further effort. Here's some documentation from http://www.swig.org/Doc1.3/Python.html#Python_nn21 .

31.3.8 C++ inheritance

SWIG is fully aware of issues related to C++ inheritance. Therefore, if you have classes like this

class Foo {
...
};

class Bar : public Foo {
...
};

those classes are wrapped into a hierarchy of Python classes that reflect the same inheritance structure. All of the usual Python utility functions work normally:

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