[C++-sig] wrapping constructor

Ralf W. Grosse-Kunstleve rwgk at yahoo.com
Thu Apr 8 14:11:43 CEST 2004


--- "Neal D. Becker" <ndbecker2 at verizon.net> wrote:
> Using boost-python I have a class with non-default constructor.  I don't
> want to expose this constructor to python, but want to wrap it in a new
> interface.  Is this OK?  (No ownership issues...)  I'm assuming copying the
> object is not terribly expensive, or won't happen that often.
> 
> class X {
>   X (something something)
> };
> 
> X MakeX (some args more suitable for python) {
>   return X (use above constructor);
> }

X* MakeX(...)
{
  return new X(...);
}

> BOOST_PYTHON_MODULE(X_wrap) {
>   class_<X> ("X", no_init);
> 
>   def ("MakeX", MakeX);

    def("__init__", boost::python::make_constructor(MakeX))
    ;
> }

boost::python::make_constructor is a fairly recent addition to the library. For
this to work you'll need the 1.31 release or the current CVS.

See also:

 
http://www.boost.org/libs/python/doc/v2/make_function.html#make_constructor-spec

  boost/libs/python/test/injected.cpp

Ralf


__________________________________
Do you Yahoo!?
Yahoo! Small Business $15K Web Design Giveaway 
http://promotions.yahoo.com/design_giveaway/




More information about the Cplusplus-sig mailing list