[C++-sig] init from free function instead of constructor

Stefan Seefeld seefeld at sympatico.ca
Wed Jan 24 19:42:02 CET 2007


John Reid wrote:
> Hi,
> 
> I can't find anything in the documentation that lets me do this:
> 
> class X { };
> X make_x( std::string );
> 
> using namespace boost::python;
> 
> class_<X>(
> 	"X",
> 	"This is X's docstring.",
> 	init<std::string>( make_x ) )
> ;
> 
> Is it true that I can only make python __init__ functions from 
> constructors as opposed to free functions such as make_x()?

No. Have a look at the 'make_constructor' function
(http://www.boost.org/libs/python/doc/v2/make_function.html#make_constructor-spec).

With that you can do

class_<X> x("X");
x.def("__init__", make_constructor(make_x));


HTH,
		Stefan

-- 

      ...ich hab' noch einen Koffer in Berlin...



More information about the Cplusplus-sig mailing list