Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pure virtual function call

I'm using boost.python to make python-modules written in c++. I have some base class with pure virtual functions which I have exported like this:

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: Pure virtual function called

What's wrong?

like image 610
Max Frai Avatar asked Apr 16 '11 17:04

Max Frai


People also ask

What is pure virtual function call?

A pure virtual function is declared, but not necessarily defined, by a base class. A class with a pure virtual function is "abstract" (as opposed to "concrete"), in that it's not possible to create instances of that class.

Can you call virtual function?

A virtual function is a member function that you expect to be redefined in derived classes. When you refer to a derived class object using a pointer or a reference to the base class, you can call a virtual function for that object and execute the derived class's version of the function.

Can you call a pure virtual function in base class?

Pure virtual functions must not be called from a C++ constructor. As a general rule, you should never call any kind of virtual function in a constructor or destructor because those calls will never go to a more derived class than the currently executing constructor or destructor.


2 Answers

This error happens when a constructor or a destructor directly or indirectly calls a pure virtual member.

(Remember that during constructor and destructor execution, the dynamic type is the constructed/destructed type and so virtual members are resolved for that type).

like image 106
AProgrammer Avatar answered Sep 21 '22 14:09

AProgrammer


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

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

like image 31
Andrew Rasmussen Avatar answered Sep 21 '22 14:09

Andrew Rasmussen