Overloaded Constructors?!?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • andrea_gavana@tin.it

    #1

    Overloaded Constructors?!?

    Hello NG,

    I am trying to port a useful class from wxWidgets (C++) to a pure Python/wxPython
    implementation. In the C++ source code, a unique class is initialized with
    2 different methods (???). This is what it seems to me. I have this declarations:

    class wxFoldWindowIte m
    {
    private:
    wxWindow *_wnd;
    int _type, _flags;
    int _leftSpacing,
    _rightSpacing,
    _ySpacing;
    int _lineWidth, _lineY;
    wxColour _sepLineColour;

    public:
    enum
    {
    WINDOW = 0,
    SEPARATOR
    };

    // wxWindow constructor. This initialises the class as a wxWindow type
    wxFoldWindowIte m(wxWindow *wnd, int flags = wxFPB_ALIGN_WID TH, int ySpacing
    = wxFPB_DEFAULT_Y SPACING,
    int leftSpacing = wxFPB_DEFAULT_L EFTSPACING, int rightSpacing
    = wxFPB_DEFAULT_R IGHTSPACING)
    : _wnd(wnd)
    , _type(WINDOW)
    , _flags(flags)
    , _leftSpacing(le ftSpacing)
    , _rightSpacing(r ightSpacing)
    , _ySpacing(ySpac ing)
    , _lineWidth(0)
    , _lineY(0)
    {
    };

    // separator constructor. This initialises the class as a separator
    type
    wxFoldWindowIte m(int y, const wxColour &lineColor = *wxBLACK, int ySpacing
    = wxFPB_DEFAULT_Y SPACING,
    int leftSpacing = wxFPB_DEFAULT_L EFTLINESPACING,
    int rightSpacing = wxFPB_DEFAULT_R IGHTLINESPACING )

    : _wnd(0)
    , _type(SEPARATOR )
    , _flags(wxFPB_AL IGN_WIDTH)
    , _leftSpacing(le ftSpacing)
    , _rightSpacing(r ightSpacing)
    , _ySpacing(ySpac ing)
    , _lineWidth(0)
    , _lineY(y)
    , _sepLineColour( lineColor)
    {
    };

    The 2 different initializations refers to completely different objects (the
    first one is a wx.Window, the second one is an horizontal line). Next, there
    are a lot of functions that, depending on the variable _type, return properties
    of the wx.Window or of the line. I would like to keep the same names for
    classes/methods, so it would be useful to have the same class with 2 different
    "initialization s".
    Does anyone know if is there a way to achieve the same thing in Python/wxPython?
    Someone else has talked about overloaded constructors, but I don't have
    any idea on how to implement this kind of "constructo rs" in Python. Does
    anyone have a small example of overloaded constructors in Python?
    I have no idea... Or am I missing something obvious?

    Thanks to you all.

    Andrea.


  • Kent Johnson

    #2
    Re: Overloaded Constructors?!?

    andrea_gavana@t in.it wrote:[color=blue]
    > Hello NG,
    >
    > I am trying to port a useful class from wxWidgets (C++) to a pure Python/wxPython
    > implementation. In the C++ source code, a unique class is initialized with
    > 2 different methods (???). This is what it seems to me. I have this declarations:
    >
    > class wxFoldWindowIte m
    > {
    > // wxWindow constructor. This initialises the class as a wxWindow type
    > wxFoldWindowIte m(wxWindow *wnd, int flags = wxFPB_ALIGN_WID TH, int ySpacing
    > = wxFPB_DEFAULT_Y SPACING,
    > int leftSpacing = wxFPB_DEFAULT_L EFTSPACING, int rightSpacing
    > = wxFPB_DEFAULT_R IGHTSPACING)
    > : _wnd(wnd)
    > , _type(WINDOW)
    > , _flags(flags)
    > , _leftSpacing(le ftSpacing)
    > , _rightSpacing(r ightSpacing)
    > , _ySpacing(ySpac ing)
    > , _lineWidth(0)
    > , _lineY(0)
    > {
    > };
    >
    > // separator constructor. This initialises the class as a separator
    > type
    > wxFoldWindowIte m(int y, const wxColour &lineColor = *wxBLACK, int ySpacing
    > = wxFPB_DEFAULT_Y SPACING,
    > int leftSpacing = wxFPB_DEFAULT_L EFTLINESPACING,
    > int rightSpacing = wxFPB_DEFAULT_R IGHTLINESPACING )
    >
    > : _wnd(0)
    > , _type(SEPARATOR )
    > , _flags(wxFPB_AL IGN_WIDTH)
    > , _leftSpacing(le ftSpacing)
    > , _rightSpacing(r ightSpacing)
    > , _ySpacing(ySpac ing)
    > , _lineWidth(0)
    > , _lineY(y)
    > , _sepLineColour( lineColor)
    > {
    > };
    >
    > The 2 different initializations refers to completely different objects (the
    > first one is a wx.Window, the second one is an horizontal line).[/color]

    This is a strange design. My first reaction is, why do you want to do that? Maybe you should split
    the class in two?

    Next, there[color=blue]
    > are a lot of functions that, depending on the variable _type, return properties
    > of the wx.Window or of the line. I would like to keep the same names for
    > classes/methods, so it would be useful to have the same class with 2 different
    > "initialization s".[/color]

    One way to do this in Python is to have a single constructor that looks at the type / number of
    arguments to figure out what it is supposed to do. Another way is to make two factory methods that
    create instances of the class and do the correct initialization.

    Kent

    Comment

    Working...