[C++-sig] Passing inherited classes back to C++

Tom Brooks tgbrooks at gmail.com
Sat May 5 20:46:21 CEST 2007


I currently have these C++ classes:

class Renderable
{
public:
    Renderable();
    virtual ~Renderable();
    virtual void Draw() = 0;
};

class Quad : public Renderable
{
public:
    Quad(float x, float y, float h, float w);
    ~Quad();
    void Draw();
};

And I need to expose them to Python. Then, I need to be able to make one in
Python, and pass it back to a C++ function  as a Renderable*.

I made this wrapper class for Renderable:

struct RenderableWrap : Renderable, wrapper<Renderable>
{
public:
    void Draw()
    {
        this->get_override("Draw")();
    }
};

And I have this Boost.Python code for the Python side:

    class_<RenderableWrap, boost::noncopyable>("Renderable")
        .def("Draw",pure_virtual(&Renderable::Draw))
    ;

    class_<Quad, bases<RenderableWrap> >("Quad",init<float, float, float,
float>())
        .def("Draw",&Quad::Draw)
    ;

But when I pass in a Quad() to my C++ function, I get an error that it can't
convert from a Quad to a Renderable*. I have next to no clue how the base
class should have been done, since the tutorial and anything else I can find
online don't go over what to do for the base class (when there are virtual
functions, at least). For example, I don't know if I inherit from
Renderable, or RenderableWrap (or both?). I don't know whether the base
class needs a wrapper itself (and would it if it had a virtual function?).

I'd appreciate any help!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20070505/74843b3c/attachment.htm>


More information about the Cplusplus-sig mailing list