纯虚函数调用

Max*_*rai 6 c++ python pure-virtual boost-python

我正在使用boost.python来生成用c ++编写的python模块.我有一些基类与纯虚函数,我已导出如下:

class Base
{
    virtual int getPosition() = 0;
};

boost::python::class_<Base>("Base")
   .def("GetPosition", boost::python::pure_virtual(&Base::getPosition));
Run Code Online (Sandbox Code Playgroud)

在Python中我有代码:

class Test(Base):
   def GetPosition(self):
      return 404

Test obj
obj.GetPosition()
Run Code Online (Sandbox Code Playgroud)

RuntimeError:调用纯虚函数

怎么了?

APr*_*mer 4

当构造函数或析构函数直接或间接调用纯虚拟成员时,会发生此错误。

(请记住,在构造函数和析构函数执行期间,动态类型是构造/析构类型,因此将解析该类型的虚拟成员)。