c++ - 纯虚函数调用

标签 c++ python boost-python pure-virtual

我正在使用 boost.python 制作用 C++ 编写的 python 模块。我有一些带有纯虚函数的基类,我是这样导出的:

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

boost::python::class_<Base>("Base")
   .def("GetPosition", boost::python::pure_virtual(&Base::getPosition));

在 Python 中我有代码:

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

Test obj
obj.GetPosition()

RuntimeError: Pure virtual function called

怎么了?

最佳答案

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

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

关于c++ - 纯虚函数调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5688294/

相关文章:

python - Boost Python - 包装函数时限制参数数量

c++ - 嵌入C++的boost python中的正则表达式

c++ - Boost::Python 绑定(bind)类继承

c++ - 适合无符号(自然)数的任何值的宽度

python - LightGBMError : Do not support special JSON characters in feature name - The same code is working in jupyter but doesn't work in Spyder

c++ - 临时右值引用对象的生命周期

python - 添加一个无需 JavaScript 的自定义 Python Bokeh 小部件

python - 解析 .py 文件,读取 AST,修改它,然后写回修改后的源代码

c++ - 设置 MFC 对话框的位置

c++ - 如何在 C/C++ 中做 TCP 连接池