【问题标题】:boost.python: Calling function of a base class from Pythonboost.python:从 Python 调用基类的函数
【发布时间】:2016-01-16 18:33:25
【问题描述】:

我有一个暴露了 C++ 类的模块:

class MyClass{
public:
    MyClass(){}
    void foo(){
        //...
    }
};

BOOST_PYTHON_MODULE(my_module){
    class_<MyClass>("MyClass", init<>())
        .def("foo", &MyClass::foo)
        ;
}

在 Python 中,我创建了一个派生自此类的类。

from my_module import *

class Derived(MyClass):
    def __init__(self):
        self.foo()

d = Derived()

我得到错误:

Boost.Python.ArgumentError: Python argument types in
    MyClass.foo(Derived)
did not match C++ signature:
    foo(class MyClass {lvalue})

我希望能够从派生类调用基类中定义的函数。我该如何解决这个问题?

【问题讨论】:

    标签: python inheritance boost boost-python


    【解决方案1】:

    看起来你需要在调用foo之前先调用MyClass.__init__(self);否则foo 没有得到MyClass 实例。使用super()的奖励积分。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-28
      • 1970-01-01
      • 2011-06-19
      • 2011-06-17
      • 2011-05-03
      • 2013-12-19
      • 1970-01-01
      相关资源
      最近更新 更多