[C++-sig] Shared Pointers and Boost.Python

Brian Hall bhall at gamers-fix.com
Fri Jul 23 20:50:47 CEST 2004


What is the proper way to pass shared pointers to constructors of exposed
objects?
What about returning them?

For instance, lets say I have the following classes:

Class A:
{
// some stuff
};

Class B:
{
	B(shared_ptr<A> a) : myA(a) {}
	shared_ptr<A> getA() { return myA; }

	shared_ptr<A> myA;
}

And exposed as so:

BOOST_PYTHON_MODULE(GI_AISDK)
{
	// Expose the State class to Python
	class_<A>("A");
	class_<B>("B", init<shared_ptr<A> >())
		.def("getA", &B::getA);
}

With some python script like so:

class C(B):
	def do_some_stuff(self):
		pass

Now say I want to create an instance of class C from within C++:

shared_ptr<A> a(new A);

object myClassCType(maindictionary["C"]);

object newClassCObject = myClassCType(a);

shared_ptr<A> theA = extract<shared_ptr<A>
>(newClassCObject.attr("getA")());

Is this right?

Thanks!  

Brian Hall






More information about the Cplusplus-sig mailing list