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

Mathieu Lacage Mathieu.Lacage at sophia.inria.fr
Wed Feb 15 10:50:52 CET 2006


On Tue, 2006-02-14 at 10:02 +0000, Julien wrote:
> Hello, 
>     I use the Boost.Python lib for a couple of weeks now and i am 
> facing a pb. 
> To sum up i have a small hierarchy of objects and have a factory to 
> build concrete object. The pb is i think aroud the auto_ptr object that 
> is not correctly managed by my code ( i use version 1.33 ) 
> I think that the solution is obvious but i can't sort it out. If you 
> could have a look at this small test 
> 
> 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); 
>  class_<Y,  boost::python::bases<X> >("Y") 

I think you want this instead:
class_<Y,  boost::python::bases<X>, auto_ptr<Y> >("Y") 

>   .def("fun1",&Y::fun1) 
>   .def("fun2",&Y::fun2) 
>   .def("fun3",&Y::fun3) 
>  ; 
> 
>  class_<FactY>("FactY",no_init) 
>   .def("create",&FactY::create) 
>   .staticmethod("create") 
>  ; 
> 
> 
> 
> } 
> 
> 
> Many Thx 
> 
> 
> 
> _______________________________________________
> C++-sig mailing list
> C++-sig at python.org
> http://mail.python.org/mailman/listinfo/c++-sig
-- 




More information about the Cplusplus-sig mailing list