[C++-sig] Passing inherited classes back to C++
Tom Brooks
tgbrooks at gmail.com
Sat May 5 22:15:40 CEST 2007
Thank you for the fast reply. I would have tried Py++ myself, but I'm unable
to install it. I get an error that the .NET framework SDK must be installed
in order to build Python extensions when I try to install GCC_XML. I tried
to download and install the .NET SDK, but I'm sure if I've really got all of
it. I didn't select much for the installation process. Though it is
off-topic, any help with this would be nice too. I use Visual Studios C++
express on Windows XP. I have Python 2.4 and the Windows Platform SDK.
As for the code, I haven't looked at it in a lot of detail yet, but a simple
copy-and-paste gives me a run-time error that I'm calling a pure-virtual
function when I call Draw() from the passed in Renderable*. Any more
thoughts?
On 5/5/07, Roman Yakovenko <roman.yakovenko at gmail.com> wrote:
>
> On 5/5/07, Tom Brooks <tgbrooks at gmail.com> wrote:
> > 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,
>
> Quad derives from Renderable, not RenderableWrap
>
> > 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 code was generated by Py++ GUI
> (
> http://language-binding.net/pyplusplus/documentation/tutorials/pyplusplus_gui.html
> )
>
> #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 );
> }
>
>
> --
> Roman Yakovenko
> C++ Python language binding
> http://www.language-binding.net/
> _______________________________________________
> C++-sig mailing list
> C++-sig at python.org
> http://mail.python.org/mailman/listinfo/c++-sig
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20070505/6067fa59/attachment.htm>
More information about the Cplusplus-sig
mailing list