Pure virtual function call

I use boost.python to create python modules written in C ++. I have a base class with pure virtual functions that I exported as follows:

class Base { virtual int getPosition() = 0; }; boost::python::class_<Base>("Base") .def("GetPosition", boost::python::pure_virtual(&Base::getPosition)); 

in Python I have code:

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

RuntimeError: A pure virtual function called

What's wrong?

+6
source share
2 answers

This error occurs when a constructor or destructor directly or indirectly calls a pure virtual element.

(Remember that at runtime of the constructor and destructor, the dynamic type is a built / destroyed type, and therefore virtual members are allowed for this type).

+4
source

A "pure virtual function" is a function that does not have a definition in the base class. This means that all children of this base class will have an overridden implementation of this function, but the base class has no implementation.

In your example, it looks like you are calling a pure virtual function, so you are calling a declared function, but since you are not calling a child implementation, it has no definition.

+1
source

Source: https://habr.com/ru/post/886055/


All Articles