[C++-sig] shared_ptr with custom destructor

David Abrahams dave at boostpro.com
Mon Dec 1 18:50:58 CET 2008


on Mon Nov 24 2008, "Renato Araujo" <renatox-AT-gmail.com> wrote:

> Hi all,
> I would like to specify during the class_ declaration use a shared_ptr
> with custom release function.
>
> shared_ptr has a constructor where you can pass a function to call in
> the release, I would like to use this in a generic form.
>
> I already tried some like that:
>
> shared_ptr<BaseClass> _custom_constructor()
> {
>       return shared_ptr<BaseClass>(new BaseClass(), _release_func);
> }
>
> class_ <BaseClass, shared_ptr<BaseClass> >
> ("BaseClass").def("__init__", make_constructor(_custom_constructor));
>
> but this not work to class derived from BaseClass: my

Derived in C++ or in Python?

> _custom_constructor is not called to others class.
>
> I would like something more generic where I can declare in class_
> template, something like that:
>
> class_<MyClass, boost::shared_ptr<MyClass, release_func> ("MyObject")
> .......
>
> There is something like that? How is the best way to do this?

Write a function template that you can use with make_constructor, e.g.:

      make_constructor(&_custom_constructor<DerivedClass>)

HTH,

-- 
Dave Abrahams
BoostPro Computing
http://www.boostpro.com


More information about the Cplusplus-sig mailing list