[C++-sig] confused...

Paul Rudin paul.rudin at ntlworld.com
Thu May 15 00:08:13 CEST 2003


Should this work? If not is there an obvious way to make it work? TIA.


Say subclassTest.cpp contains:

---------------------
#include <boost/python.hpp>
using namespace boost::python;

class Base
{
public:
  virtual int foo(int x) { return x+1;}
};

class Bar 
{
public:
  Base* thing;
  void setThing(Base* bptr) { thing=bptr;};
  int callThing(int x) {return thing->foo(x);};
};

class BaseWrap : public Base
{
public:
  BaseWrap(PyObject* self_):self(self_){}

  int foo(int x)
  {
    return call_method<bool>(self,"foo",x);
  }
  PyObject* self;
};


BOOST_PYTHON_MODULE(subclassTest)
{


  class_<Base, BaseWrap, boost::noncopyable >("Base")
    .def("foo", &BaseWrap::foo)
    ;

  class_<Bar>("Bar")
    .def("setThing", &Bar::setThing)
    .def("callThing", &Bar::callThing)
    ;

};

---------------------

Then in python:

>>> from subclassTest import *
>>> class sub(Base):
... 	 def foo(z):
... 	     z=z+7		
... 
>>> s=sub()
>>> bar=Bar()
>>> bar.setThing(s)
>>> bar.callThing(2)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: foo() takes exactly 1 argument (2 given)
>>> 




More information about the Cplusplus-sig mailing list