[C++-sig] Boost.Python and lifetime of instances of C++ classes
David Abrahams
david.abrahams at rcn.com
Wed Jan 23 23:04:23 CET 2002
----- Original Message -----
From: "Hugo van der Merwe" <s13361562 at bach.sun.ac.za>
> True, I didn't bother to have data members, etc. But anyway, that is
> irrelevant. The question is how can I go about "solving" this? (I
> mentioned this in a previous thread, but without the code it was
> probably not clear what I meant.)
include <iostream.h>
include <boost/smart_ptr.hpp>
class test {
public:
test() { std::cout << "test Constructor called." << std::endl; }
~test() { std::cout << "test Destructor called." << std::endl; }
void print() { std::cout << "HELLO!" << std::endl; }
static boost::shared_ptr<test> create() {
return boost::shared_ptr<test>(new test);
}
};
class foo {
public:
void add(boost::shared_ptr<test> t) { sav = t; }
void print() { sav->print(); }
private:
boost::shared_ptr<test> sav;
};
#include <boost/python/class_builder.hpp>
BOOST_PYTHON_MODULE_INIT(test)
{
boost::python::module_builder this_module("test");
boost::python::class_builder<test> test_class(this_module, "test");
this_module.def(&test::create, "testclass");
test_class.def(&test::print, "printit");
boost::python::class_builder<foo> foo_class(this_module, "foo");
foo_class.def(boost::python::constructor<>());
foo_class.def(&foo::add, "add");
foo_class.def(&foo::print, "printit");
}
...or you can wait for Boost.Python V2, coming soon, which will handle this
problem much better.
More information about the Cplusplus-sig
mailing list