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

Thomas Muldowney temas at box5.net
Sat Jul 26 19:05:44 CEST 2003


*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>());
}

It seemed implied to me by the docs and some other emails that foo
should not be defined in Base.  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.

--temas


On Sat, 2003-07-26 at 06:29, David Abrahams wrote:
> Thomas Muldowney <temas at box5.net> writes:
> 
> > I'm having a mental breakdown on this one.  I have a factory returning a
> > pointer to an abstract base class that I've wrapped.  
> 
> 
>   Abstract* factory();
> 
> > However, I can't logically wrap the actual derived class because
> > it's generated in a plugin.  
> 
> What do you mean by "plugin"?  Some other extension module?
> 
> > So, when I go to make a call on one of the abstract functions it
>             "pure virtual"-------------------^^^^^^^^?
> > fails with an AttributeError exception.  Is there anyway to handle
> > this in a clean manner that keeps the API near the C++ API?  
> 
> 
>   class_<Abstract, non_copyable>("Abstract", no_init)
>        .def("pure_virtual_function1", &Abstract::pure_virtual_function1)
>        .def("pure_virtual_function2", &Abstract::pure_virtual_function2)
>        ...
>        ;
> 
> ??
> 
> > I've played around with writing wrapper functions (taking the base
> > class pointer) that are defined on the class using the same name as
> > the virtual function.  Sadly, from my mailing list searches on this
> > subject it seems this is a bad idea and can possibly lead to lookup
> > loops?  
> 
> Please post some code which illustrates what you're doing.  I'm sure
> it's unneccessary, but I can't quite understand it either.
> 
> > It does seem to run properly, though.  Would love some feedback or a
> > kick in the head if I'm missing something in the docs and sites.
> 
> Sorry, remote cranial kicks cost extra.





More information about the Cplusplus-sig mailing list