2

how to use pure virtual function for Multiple inhertiance by using boost python. Error i got are that 'Derived1' cannot instaniate abstract class. and 'Derived2' cannot instantiate abstract class. this code is working if there is only one derived class but more than one derived classs its not working. thanks for help.

class Base
{
  public:
   virtual int test1(int a,int b) = 0;
   virtual int test2 (int c, int d) = 0;
   virtual ~Base() {}
 };

class Derived1
  : public Base
 {
   public:
   int test1(int a, int b) { return a+b; }
};

class Derived2
 : public Base
{
  public:
  int test2(int c, int d) { return c+d; }
};
struct BaseWrap
  : Base, python::wrapper<Base>
{
  int test1(int a , int b) 
  {
     return this->get_override("test")(a, b);
   }
  int test2(int c ,int d)
   {
     return this->get_override("test")(c, d);
    }
};

BOOST_PYTHON_MODULE(example) 
{
  python::class_<BaseWrap, boost::noncopyable>("Base")
   .def("test1", python::pure_virtual(&BaseWrap::test1))
   .def("test2", python::pure_virtual(&BaseWrap::test2))
   ;

  python::class_<Derived1, python::bases<Base> >("Derived1")
   .def("test1", &Derived1::test1)
   ;

   python::class_<Derived2, python::bases<Base> >("Derived2")
   .def("test2", &Derived2::test2)
   ;   
}
4
  • 1
    what exactly errors are? Commented Nov 1, 2013 at 13:40
  • 1
    Errors are error C2259: 'Derived1' : cannot instantiate abstract class error C2259: 'Derived2' : cannot instantiate abstract class Commented Nov 1, 2013 at 13:41
  • "this code is working if there is only one derived class" Does this mean that you can have the same exact definition for Base, have the same exact definition for Derived1, but take out the definition for Derived2, and keep the definition for BaseWrap, and it works? Commented Nov 1, 2013 at 13:50
  • yes if there in only one pure virtual function and one derived class then its works. but 2 pure virtual function and they implemented in different derived classes, then i got error Commented Nov 1, 2013 at 13:56

1 Answer 1

2

The error messages indicate that neither Derived1 nor Derived2 can be instantiated, as they are abstract classes:

  • Derived1 has a pure virtual function: int Base::test2(int, int).
  • Derived2 has a pure virtual function: int Base::test1(int, int).

When either of these are exposed via boost::python::class_, a compiler error should occur. class_'s HeldType defaults to the type being exposed, and the HeldType is constructed within a Python object. Hence, python::class_<Derived1, ...> will instantiate Boost.Python templates that attempt to create an object with a dynamic type of Derived1, resulting in the compiler error.

This error does not present itself with BaseWrap, as BaseWrap implements all pure virtual functions. The boost::python::pure_virtual() function specifies that Boost.Python will raise a "pure virtual called" exception during dispatch if the function has not been overridden in either C++ or Python.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.