[C++-sig] wrapping class with pure virtual function & non-default constructor

Faheem Mitha faheem at email.unc.edu
Fri Mar 25 22:46:43 CET 2005


Hi,

Please consider the following variation of the example in the
tutorial. The main difference here is that the class that is being
wrapped has a non-default destructor.

Without a destructor declared in BaseWrap, this gives the error

error: base `World' with only non-default constructor in class without
a constructor

Adding a default constructor to the wrapper class makes the error go away, eg.

BaseWrap():Base(0){}

I can't find an example of this anywhere. I've looked at the docs, but
don't understand what is going on well enough. So, I'm asking here.
Is this Ok, and can I use any default constructor for the wrapper
class without it making any difference?

One other thing. Am I correct in thinking that wrapping the base class
in this fashion is necessary if I want to wrap the derived classes?

Clarifications appreciated. I'm not subscribed, so please cc me at
faheem at email.unc.edu if possible.

Thanks.                                                 Faheem.

*************************************************************************
#include <boost/python.hpp>
#include <boost/python/module.hpp>
#include <boost/python/class.hpp>
#include <boost/python/return_value_policy.hpp>
#include <boost/python/manage_new_object.hpp>
#include <boost/python/reference_existing_object.hpp>
#include <boost/python/pure_virtual.hpp>
#include <boost/python/wrapper.hpp>
#include <boost/python/def.hpp>
#include <boost/python/call.hpp>

#include <boost/utility.hpp>
#include <iostream>
#include <string>
using namespace boost::python;

struct Base
{
  int data;
  Base(int _data):data(_data){}
  virtual ~Base() {}
  virtual int f() = 0;
};

struct BaseWrap : Base, wrapper<Base>
{
  BaseWrap():Base(0){}
  int f()
  {
    return this->get_override("f")();
  }
};

BOOST_PYTHON_MODULE_INIT(polymorphism2_ext)
{
  class_<BaseWrap, boost::noncopyable>("Base")
    .def("f", pure_virtual(&Base::f))
    ;
}




More information about the Cplusplus-sig mailing list