[C++-sig] pointer arguments

Michael Schuitema michaelschuitema at googlemail.com
Tue Feb 9 11:41:28 CET 2010


Hi all,

I am new to boost.python (and relatively new to python) so my question might
be rather simple (or stupid).

I have an abstract C++ base class and a derived class DerivedA which I
exposed to Python. Works beautifully

class Base
{
public:
    virtual double compute(void) = 0;
};

class DerivedA : public Base
{
public:
     virtual double compute(void) { return 5.0; }
};

added my own DerivedB in python
class DerivedB(Base):
    def compute(self):
        return 25.0;

which also works.
>>> d_a = DerivedA()  // my C++ derived class; works fine
>>> d_b = DerivedB()  // my python derived class; works fine
>>> d_a.compute()     // works fine
5.0
>>> d_b.compute()     // works fine
25.0

Now I have added a funtion testBase in C++ which looks as follows
double testBase(Base* b)
{
    return b->compute() + 11.5;
}

which I exposed to python using (with help of Py++)

 typedef double (*testBase_function_type)(Base *);

def("testBase", testBase_function_type( &::testBase ),
       ( arg("b") ) );

What I was hoping to work was

>>> testBase(d_a) // on the derivedA C++ class
Traceback (most recent call last):
ArgumentError: Python argument types in
    hello.testBase(DerivedA)
did not match C++ signature:
    testBase(class Base * b)

does not work. Any help would be appreciated.
My more general aim is to write C++ code in terms of abstract base classes.
>From time to time I need to derive classes in python from the abstract base
classes, but I would still like to be able to use the functions written in
terms of the C++ base classes. How would I need to go about that using
boost.python

Thanks,
Mike
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20100209/2501258f/attachment.htm>


More information about the Cplusplus-sig mailing list