Skip to main content
GameDev.net gamedev.net
🔒 Locked

Inheritance in Boost.Python

Started by Ezbez May 2, 2007 at 4:31 AM 0 replies 1.7k views
Original Post
Ezbez
Ezbez
I'm having troubles using inheritance with Boost.Python. The documentation only gives examples for half of the inheritance tree if you've got virtual functions: the base class. It has no information on derived classes that have virtual functions. I've tried all the logic combinations, but it still doesn't work. Either I get an error that it can't convert a Derived into a Base, or I get an error that I'm calling a pure virtual function.
struct RenderableWrap : Renderable, wrapper<Renderable>
{
public:
	void Draw()
	{
		this->get_override("Draw")();
	}
};

...

	//This part I think is right
	class_<RenderableWrap, boost::noncopyable>("Renderable")
		.def("Draw",pure_virtual(&Renderable::Draw))
	;
	//This part I haven't the slightest clue
	//Should it inherit from Renderable? Should it Use it's own wrapper class?
	//Should it inherit from BOTH Renderable and RenderableWrap?
	//Should it's wrapper inherit from Renderable or Quad or RenderableWrap or some combination there-of?
	class_<Quad, bases<RenderableWrap> >("Quad",init<float, float, float, float>())
		.def("SetColor",&Quad::SetColor)
		.def("Draw",&Quad::Draw)
	;
The comments basically sum it up. Draw is pure virtual in Renderable, and it is implemented in Quad. I want to be able to make a quad inside a Python script and pass it to a C++ function that takes a Renderable* and then calls renderable->Draw(). Depending on what I use, I get either an error that it can't convert a Quad into a Renderable* or that I'm calling a pure virtual function. Any help would be appreciated!
Ezbez
Ezbez
In the interest of providing an answer to what I needed an answer to:

First off, I used the C++-SIG at Python.org to get an answer. I recommend asking Boost.Python related questions there, since I got very good responses. Instructions for its use are available in the Guidlines link to the left.

Now, thanks to a lot of help from Roman Yakovenko, I know that my problem was with the Python object being garbage collected before it was used in C++ (read: my function took control of it, but I didn't tell Python). However, here's the full Py++-generated code for the inheritance:

#include "boost/python.hpp"#include "1.h"namespace bp = boost::python;struct Renderable_wrapper : Renderable, bp::wrapper< Renderable > {   Renderable_wrapper( )   : Renderable( )     , bp::wrapper< Renderable >(){       // null constructor   }   virtual void Draw(  ){       bp::override func_Draw = this->get_override( "Draw" );       func_Draw(  );   }};struct Quad_wrapper : Quad, bp::wrapper< Quad > {   Quad_wrapper(Quad const & arg )   : Quad( arg )     , bp::wrapper< Quad >(){       // copy constructor   }   Quad_wrapper(float x, float y, float h, float w )   : Quad( x, y, h, w )     , bp::wrapper< Quad >(){       // constructor   }   virtual void Draw(  ) {       if( bp::override func_Draw = this->get_override( "Draw" ) )           func_Draw(  );       else           this->Quad::Draw(  );   }   void default_Draw(  ) {       Quad::Draw( );   }};BOOST_PYTHON_MODULE(pyplusplus){   bp::class_< Renderable_wrapper, boost::noncopyable >( "Renderable" )       .def( bp::init< >() )       .def(           "Draw"           , bp::pure_virtual( &::Renderable::Draw ) );   bp::class_< Quad_wrapper, bp::bases< Renderable > >( "Quad",bp::init< float, float, float, float >(( bp::arg("x"), bp::arg("y"),bp::arg("h"), bp::arg("w") )) )       .def(           "Draw"           , &::Quad::Draw           , &Quad_wrapper::default_Draw );}


Hopefully this will help someone else out.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.