[C++-sig] How to derive from an abstract base class

Jim Bosch talljimbo at gmail.com
Sat Jul 24 00:55:39 CEST 2010


On 07/23/2010 03:09 PM, Lutz Maibaum wrote:
> Dear all,
>
> I am totally new to Boost.Python, and I am going through the tutorial to get up to speed. I apologize if this is a very obvious question.
>
<snip>
>
> class Base {
> public:
>    virtual int f() = 0;
> };
> class Derived : public Base {
> public:
>    int f() {return 1;}
> };
> class BaseWrap : public Base, public wrapper<Base>  {
> public:
>    int f() {return this->get_override("f")();}
> };
>
> int func(Base&  b) {
>    return b.f();
> }
>
> BOOST_PYTHON_MODULE(foo) {
>    class_<Derived, bases<Base>  >  ("Derived")
>      .def("f",&Derived::f);
>    class_<BaseWrap, boost::noncopyable>("Base")
>      .def("f", pure_virtual(&Base::f));
>    def("func", func, "Calls the method f of its argument");
> }
>

Try this:

BOOST_PYTHON_MODULE(foo) {
     class_<BaseWrap, boost::noncopyable>("Base")
       .def("f", pure_virtual(&Base::f));
     class_<Derived, bases<Base>  >  ("Derived")
       .def("f",&Derived::f);
     def("func", func, "Calls the method f of its argument");
}

(just swap the order of the class_ statements)


Jim Bosch





More information about the Cplusplus-sig mailing list