[C++-sig] bpl v2: classes with private constructors

David Abrahams dave at boost-consulting.com
Thu Aug 22 04:02:18 CEST 2002

From: "Peter Schoen" <Peter.Schoen1 at epost.de>

> the constructors of some classes I am trying to wrap are private because
> instances must be created by using a factory. Is it possible to wrap such
> classes using bpl v2?

Sure. Of course you can only create the instances using the factory...

> Any links to examples, etc. would be highly appreciated.

if you look at libs/python/test/test_pointer_adoption.cpp you'll see the
factory function "create" is being used to generate new instances of class
A. It uses return_value_policy<manage_new_object> to instruct Python to
adopt the A* to hold the instance.

> In an older mail I read something about specifying the HeldType template
> argument of the class_<> template to solve such a problem, but I did not
> really understand this solution, yet, sorry.

Factory functions really ought to be generating smart pointers instead of
raw pointers. If you pass the smart pointer type as the HeldType parameter
to class_<> it ought to allow them to be returned from wrapped functions
such that the appropriate Python object gets built around them... however,
I'm not sure whether that's actually implemented yet...

           David Abrahams * Boost Consulting
dave at boost-consulting.com * http://www.boost-consulting.com

More information about the Cplusplus-sig mailing list