[C++-sig] Re: FW: Injected constructors

David Abrahams dave at boost-consulting.com
Thu Jul 24 14:37:48 CEST 2003


"Roman Yakovenko" <romany at actimize.com> writes:

>> > Also in Python code it will be written explicit what programmer is
>> > going to do.  Did I missed something?
>> 
>> I don't know.  What point are you trying to make with this remark?
>
> class MyClass
> {
> ...
> 	MyClass(); //after constructing object stays in state 1
> 	MyClass( int i ); //after constructing object stays in state 2
> 	MyClass( const YourClass &x); //after constructing object stays in state 3
> ...
> };
>
> Now if I understand right there is a need in other state of the MyClass object.
>
> MyClass make_myclass_from_other( const SomeOther &y );
>
> BOOST_PYTHON_MODULE(staticmethod_ext)
> {
>     class_<MyClass>
> 		...
>         .staticmethod(make_myclass_from_other)
> ...
>
> While in python I'll write
> obj = MyClass.make_myclass_from_other( third_party_class )
>
> As for me it is much clear then adding an other constructor for the
> class.

Here's an example:

    struct X { X(int a, int b, int c); };

Now, in Python, maybe it's idiomatic to construct X from a tuple of
three elements.  It would be nice to let people support that directly
rather than having to derive a class from X in Python just to support
it.

> At the moment I wrote I thought about class that already has a few
> constructors.  Now I understand that it is not the only case. May be
> you have nice small class with one constructor and you want to add
> an other one.

Yup.

> I propose those names: make_object / make_instance

Those completely break the analogy with "make_function" and
"make_getter", though.  make_init would not make a new
object/instance, but a new __init__ function.  Even the __init__
function does not make a new Python object; it just injects a new C++
instance into it.


Anyone else have objections to "make_init"?  Going once, going
twice...

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com





More information about the Cplusplus-sig mailing list