[C++-sig] How to manage smart pointer as return value of a factory?

Julien swell at netcourrier.com
Thu Feb 16 10:34:42 CET 2006


David Abrahams <dave <at> boost-consulting.com> writes:
> 
> All you should need is
> 
>   boost::python::register_ptr_to_python<std::auto_ptr<X> >();
> 
> And *nothing* else.
> 
> http://www.boost.org/libs/python/doc/v2/register_ptr_to_python.html
> 
> HTH,

Ok , i reverted my code to test this and have a new pb now , the python code 
throw an exception telling "RuntimeError: Bad read pointer - no RTTI data!" 
when i try to create an object from the factory "y = hello.FactY.create()". 
However if i create an object directly with y = hello.Y() it works!


struct X
{
	virtual int fun1() = 0;
	int fun2() { return 2;}
	virtual int fun3() { return -1; }
};
struct Y : public X
{
	int fun1() {return 1;}
	virtual fun3() { return 3; }
};
struct FactY
{
	static std::auto_ptr<X> create() { return std::auto_ptr<X>( new Y
() ) ;}
};

BOOST_PYTHON_MODULE(hello)
{
	class_<X,  boost::noncopyable >("X",no_init);
	register_ptr_to_python< std::auto_ptr<X> >();
	class_<Y,  boost::python::bases<X> >("Y")
		.def("fun1",&Y::fun1)
		.def("fun2",&Y::fun2)
		.def("fun3",&Y::fun3)
	;
	class_<FactY>("FactY",no_init)
		.def("create",&FactY::create)
		.staticmethod("create")
	;
}







More information about the Cplusplus-sig mailing list