[C++-sig] Re: FW: Injected constructors
nicodemus at globalite.com.br
Thu Jul 24 17:07:26 CEST 2003
Roman Yakovenko wrote:
> 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 );
>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.
>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.
On a side note, this is a good reason to use classmethods in Python:
'''loads configuration from a filename or a file-like object.
def __init__(self, file_object):
self.file = file_object
def FromFileName(cls, filename):
self = cls(file(filename))
FromFileName = classmethod(FromFileName)
config = ConfigurationLoader.FromFileName('test')
This avoids having to do type checking in the __init__ method, plus it
makes the code a little more readable. 8)
More information about the Cplusplus-sig