[C++-sig] Inheritance question

Koen Van Herck koen_van_herck at yahoo.com
Fri Feb 10 13:11:14 CET 2006


Hello,

I want to expose the following classes in Python:

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

class Derived: public Base
{
public:
    void f() { std::cout << "Derived::f" << std::endl; }
    void g() { std::cout << "Derived::g" << std::endl; }
};

class User
{
public:
    void useBase(Base & b) { b.f(); }
};

I've created the following code in my BOOST_PYTHON_MODULE

    class_<User>("User")
        .def("useBase", &User::useBase)
    ;

    class_<Derived>("Derived")
        .def("g", &Derived::g)
    ;

However, when I do in Python:

u = User()
d = Derived()
u.useBase(d)

I get an error:

    u.useBase(d)
Boost.Python.ArgumentError: Python argument types in
    User.useBase(User, Derived)
did not match C++ signature:
    useBase(class User {lvalue}, class Base {lvalue})

I also tried to create a wrapper for Derived as follows:

class DerivedWrap: public Derived, public wrapper<Base>
{
};

and

    class_<DerivedWrap, boost::noncopyable>("DerivedWrap")
        .def("g", &Derived::g)
    ;

But then I get following error when calling d.g():

    d.g()
Boost.Python.ArgumentError: Python argument types in
    DerivedWrap.g(DerivedWrap)
did not match C++ signature:
    g(class Derived {lvalue})

What is the proper way to expose Derived and User ?

Regards,
Koen Van Herck.



More information about the Cplusplus-sig mailing list