Boost logo

Boost :

From: David Abrahams (dave_at_[hidden])
Date: 2004-04-01 07:41:46

Hi David,

First of all, please bring Boost.Python questions to the C++-sig:

"David Rostowsky" <davros1_at_[hidden]> writes:

> I have a couple of really simple classes:
> class MyBase
> {
> public:
> int x;
> virtual int foo(int new_x) { }; // our derived classes may or may
> not override this function. Coders choice.
> };
> class MyDerived: public MyBase
> {
> public:
> int y;
> MyDerived() { y = 0; };
> int Get(void) { return y; };
> void Set(int val) { y = val; };
> };
> My main goal in life is to create an instance of MyDerived class in
> C++:
> MyDerived my_derived = new MyDerived();
> and then pass the pointer to a Python script where the Python script
> can call any of the public member functions. Yes, I am careful to keep
> the pointer around in the C++ code while the Python script uses it.
> So I expose the class (this may or may not be the source of my
> problems, need help):
> {
> using namespace boost::python;
> class_<MyDerived>("MyDerived")
> .def("Get", &MyDerived::Get)
> .def("Set", &MyDerived::Set)
> ;
> } /* test*/
> My impasse is that the virtual function seems to really give Boost
> hell when Im trying to pass the pointer to the class over to the
> python script. Heres the code snippet for that.

Please post a complete, minimal, reproducible example.

> Its the conversion thats just KILLING me.

Chill, dude. You're still alive. At least someone's sending email
on your behalf ;-)

> If I comment out the virtual function, everything works perfectly! I
> can pass the pointer over to my script, it calls the public member
> functions, and Im happy. HOWEVER, if I put the virtual function back
> in, the following line of code

Well you've posted several lines there; can you be specific about
which one you're having problems with?

> just EXPLODES!

What does "EXPLODES" mean? Do you get a compilation error, a runtime
error, or something else?

> // It crashes and burns in make_ptr_instance.cpp, function
> get_derived_class_object()
> boost::python::object my_arg(boost::ref(my_derived));
> // pass the pointer over to the script now.
> int rval = callPython("myTestFunc", "Test String argument from c++",
> my_arg);

Dave Abrahams
Boost Consulting

Boost list run by bdawes at, gregod at, cpdaniel at, john at