Boost logo

Boost :

From: John Hunter (jdhunter_at_[hidden])
Date: 2001-08-19 18:28:48

I have a pure virtual abstract base class with no default constructor,
ie, it handles some data for its derived classes but also has pure
virtual methods. boost::python appears to reject such classes. For
example, the following code fails to compile in boost::python.

class Base {
  Base(int i) : _i(i) {}
  virtual ~Base() {}
  virtual int get() const=0;
  int get_i() const {return _i; }
  int _i;

struct Derived : Base {
  Derived( int i) : Base(i) {}
  virtual int get() const { return get_i(); }


int get(const Base& b) {
  return b.get();

which can be used in C++ like:

int main() {
  Derived d(1);
  Base& b = d;
  cout << get(b) << endl;

However, when I try to use it with boost::python (1.23.0) as

       python::module_builder my_module("my_module");
       python::class_builder<Base> base_class(my_module, "Base");
       python::class_builder<Derived> derived_class(my_module, "Derived");
       my_module.def(get, "get");

I get the compiler error below: basically, it complains that it cannot
create instance_value_holder<...>::m_help since Base has pure virtual

I guess this is why they say not to store data in abstract base


John Hunter

cd /home/guest/jdhunter/c/trade/python/
g++ -I. -I.. -I/home/guest/jdhunter/c/mylibs -I/usr/local/include/mysql/ -L/usr/local/include -I/home/guest/jdhunter/c/lib/boost_1_23_0 -I/usr/local/include/python1.6 -fPIC -ftemplate-depth-21 test_pure.cpp -lboost_python
/home/guest/jdhunter/c/lib/boost_1_23_0/boost/python/detail/extension_class.hpp: In instantiation of `boost::python::detail::instance_value_holder<Base,boost::python::detail::held_instance<Base> >':
/home/guest/jdhunter/c/lib/boost_1_23_0/boost/python/detail/init_function.hpp:266: instantiated from `boost::python::detail::init1<boost::python::detail::instance_value_holder<Base,boost::python::detail::held_instance<Base> >,const int &>::create_holder(boost::python::detail::extension_instance *, PyObject *, PyObject *) const'
/home/guest/jdhunter/c/lib/boost_1_23_0/boost/python/detail/extension_class.hpp:861: instantiated from here
/home/guest/jdhunter/c/lib/boost_1_23_0/boost/python/detail/extension_class.hpp:778: cannot declare field `boost::python::detail::instance_value_holder<Base,boost::python::detail::held_instance<Base> >::m_held' to be of type `boost::python::detail::held_instance<Base>'
/home/guest/jdhunter/c/lib/boost_1_23_0/boost/python/detail/extension_class.hpp:778: since the following virtual functions are abstract:
test_pure.cpp:7: int Base::get() const

Compilation exited abnormally with code 1 at Sun Aug 19 18:19:37

Boost list run by bdawes at, gregod at, cpdaniel at, john at