[C++-sig] C++ static function as __init__

Alex Mohr amohr at pixar.com
Thu Jun 14 02:29:27 CEST 2007


> I want to do possibly strange thing:
> 
> struct Foo {
> 	Foo() {}
> 	static void init_me(PyObject * self) {
> 		??? (I want to initialize self with a new Foo object using its constructor)
> 	}
> };
> (...)
> bp::class_<Foo> foo("Foo", bp::no_init);
> foo.def("__init__", &Foo::init_me);

Does something like this work for you?

struct Foo {
     Foo() {}
     static Foo *init_me() {
         return new Foo;
     }
};

class_<Foo>("Foo", no_init)
     .def("__init__", make_constructor(Foo::init_me));

I forget if you need manage_new_object in there or not...

Alex



More information about the Cplusplus-sig mailing list