[C++-sig] Re: Wrapping Just an Abstract Base

David Abrahams dave at boost-consulting.com
Sat Jul 26 20:31:47 CEST 2003


Thomas Muldowney <temas at box5.net> writes:

> *sigh* no more 2am emails.
>
> Here's some code that is similar:
>
> #include <iostream>
> #include <boost/python.hpp>
>
> using namespace boost::python;
>
> class Base
> {
> public:
>     virtual void foo() = 0;
> };
>
> class Base_Wrap : public Base
> {
> public:
>     void foo()
>     { std::cout << "Base_Wrap foo()" << std::endl; }
> };
>
> class Deriv : public Base
> {
> public:
>     void foo()
>     { std::cout << "Deriv foo()" << std::endl; }
> };
>
> Base* createBase()
> { 
>     Base* b = new Deriv;
>     return b;
> }
>
> BOOST_PYTHON_MODULE(testA)
> {
>     class_<Base, Base_Wrap, boost::noncopyable>("Base", no_init);
>     
>     def("createBase", createBase,
> return_value_policy<manage_new_object>());
> }

This code won't compile, will it?

> It seemed implied to me by the docs and some other emails that foo
> should not be defined in Base.  

What implied that?  Ideally Boost.Python shouldn't have any effect on
the design of the classes that it wraps.  Base_Wrap should only be
used to dispatch virtual functions to Python with call_method.

> I had done that in the past, but changed on this refactoring of the
> code.  Although not demonstrated in this code, when I say plugin I
> mean a dynamic library/shared object that is loaded using dynlib.
> It actually provides the implementation of the factory.

Your code isn't demonstrating much at all, since it doesn't show an
example of what you're trying to accomplish.  Maybe someone else can
help you; I have no clue what you're after, sorry!

P.S. Did you try what I suggested in my previous reply?

  class_<Abstract, non_copyable>("Abstract", no_init)
       .def("pure_virtual_function1", &Abstract::pure_virtual_function1)
       .def("pure_virtual_function2", &Abstract::pure_virtual_function2)
       ...
       ;

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com





More information about the Cplusplus-sig mailing list