How to make a "long&" parameter optional

Discussion in 'C++' started by Otto Wyss, Nov 8, 2004.

  1. Otto Wyss

    Otto Wyss Guest

    I've the following function declaration:

    wxTree GetLastChild (const wxTree& item, long& cookie) const;

    I'd like to make the cookie parameter optional, i.e. "long& cookie =
    ....", without breaking any software using the old API. The
    implementation looks like

    wxTree wxTreeListMainWindow::GetLastChild (const wxTree& item,
    long& cookie) const {
    wxCHECK_MSG (item.IsOk(), wxTree(), wxT("invalid tree item"));
    wxArrayTreeListItems& children = ((wxTreeListItem*)
    item.m_pItem)->GetChildren();
    // it's ok to cast cookie to long, indices won't overflow "void*"
    long *pIndex = ((long*)&cookie);
    (*pIndex) = children.Count();
    return (!children.IsEmpty())? wxTree(children.Last()): wxTree();
    }

    BTW the cast is not from me! Is this optional parameter possible? If not
    what alternatives are there?

    Full source:
    "http://cvs.sourceforge.net/viewcvs.py/wxcode/wxCode/components/treelist
    ctrl/src/treelistctrl.cpp?view=markup"

    O. Wyss

    --
    See a huge pile of work at "http://wyodesktop.sourceforge.net/"
    Otto Wyss, Nov 8, 2004
    #1
    1. Advertising

  2. Otto Wyss wrote:
    ....
    > BTW the cast is not from me! Is this optional parameter possible? If not
    > what alternatives are there?
    >


    optional reference parameter example.

    static long g__foo; // you can make an extern if you wish ..

    void Func( long & x = g__foo );
    Gianni Mariani, Nov 8, 2004
    #2
    1. Advertising

  3. Otto Wyss

    Ron Natalie Guest

    Otto Wyss wrote:
    > I've the following function declaration:
    >
    > wxTree GetLastChild (const wxTree& item, long& cookie) const;
    >
    > I'd like to make the cookie parameter optional, i.e. "long& cookie =
    > ...", without breaking any software using the old API. The
    > implementation looks like


    class wxTreeListMainWindow {
    ....
    static long dummy;
    wxTree GetLastChild(const WxTree& item, long& cookie = wxTreeListMainWindow::dummy)const;
    Ron Natalie, Nov 8, 2004
    #3
  4. Otto Wyss wrote:
    > I've the following function declaration:
    >
    > wxTree GetLastChild (const wxTree& item, long& cookie) const;
    >
    > I'd like to make the cookie parameter optional, i.e. "long& cookie =
    > ...", without breaking any software using the old API. The
    > implementation looks like
    >
    > wxTree wxTreeListMainWindow::GetLastChild (const wxTree& item,
    > long& cookie) const {
    > wxCHECK_MSG (item.IsOk(), wxTree(), wxT("invalid tree item"));
    > wxArrayTreeListItems& children = ((wxTreeListItem*)
    > item.m_pItem)->GetChildren();
    > // it's ok to cast cookie to long, indices won't overflow "void*"
    > long *pIndex = ((long*)&cookie);
    > (*pIndex) = children.Count();
    > return (!children.IsEmpty())? wxTree(children.Last()): wxTree();
    > }
    >
    > BTW the cast is not from me! Is this optional parameter possible? If not
    > what alternatives are there?


    If you mean, some kind of "default argument value"? The problem with this
    is that your reference is to a non-const long object. So, it has to refer
    to (a) something valid, no 'null' references exist, and (b) be a reference
    to an l-value. The only way to do that, AFAIK, is to provide your own
    object and inside compare the address:

    -------------------------------------- in the header
    extern long default_cookie;
    ...
    class wxTreeListMainWindow {
    ...
    wxTree GetLastChild(const wxTree& item,
    long& cookie = default_cookie);
    ...
    };
    --------------------------------------- in the C++ file
    ...
    extern long default_cookie = 0; // definition!!!
    ...
    wxTree wxTreeListMainWindow::GetLastChild (const wxTree& item,
    long& cookie) const {
    ...
    if (&cookie == &default_cookie) {
    // default
    }
    ...
    }
    --------------------------------------------------------

    V
    Victor Bazarov, Nov 8, 2004
    #4
  5. Otto Wyss

    Otto Wyss Guest

    Victor Bazarov <> wrote:

    > Otto Wyss wrote:
    > > I'd like to make the cookie parameter optional, i.e. "long& cookie =
    > > ...", without breaking any software using the old API. The
    > > implementation looks like
    > >
    > > wxTree wxTreeListMainWindow::GetLastChild (const wxTree& item,
    > > long& cookie) const {

    >
    > If you mean, some kind of "default argument value"? The problem with this
    > is that your reference is to a non-const long object. So, it has to refer
    > to (a) something valid, no 'null' references exist, and (b) be a reference
    > to an l-value. The only way to do that, AFAIK, is to provide your own
    > object and inside compare the address:
    >
    > -------------------------------------- in the header
    > extern long default_cookie;


    I feared there might not be a nice solution, I don't like to declare a
    dummy object. I've considered to write wrapper functions but that
    doesn't appeal to me either. So what would be the best solution if I
    drop the "without breaking any software using the old API"?

    Full source:
    "http://cvs.sourceforge.net/viewcvs.py/wxcode/wxCode/components/treelist
    ctrl/src/treelistctrl.cpp?view=markup"

    O. Wyss

    --
    Development of frame buffer drivers: http://linux-fbdev.sf.net
    Sample code snippets for wxWidgets: http://wxcode.sf.net
    How to build well-designed applications: http://wxguide.sf.net
    Desktop with a consistent look and feel: http://wyodesktop.sf.net
    Otto Wyss, Nov 9, 2004
    #5
  6. Otto Wyss wrote:
    > Victor Bazarov <> wrote:
    >
    >
    >>Otto Wyss wrote:
    >>
    >>>I'd like to make the cookie parameter optional, i.e. "long& cookie =
    >>>...", without breaking any software using the old API. The
    >>>implementation looks like
    >>>
    >>>wxTree wxTreeListMainWindow::GetLastChild (const wxTree& item,
    >>> long& cookie) const {

    >>
    >>If you mean, some kind of "default argument value"? The problem with this
    >>is that your reference is to a non-const long object. So, it has to refer
    >>to (a) something valid, no 'null' references exist, and (b) be a reference
    >>to an l-value. The only way to do that, AFAIK, is to provide your own
    >>object and inside compare the address:
    >>
    >> -------------------------------------- in the header
    >> extern long default_cookie;

    >
    >
    > I feared there might not be a nice solution, I don't like to declare a
    > dummy object. I've considered to write wrapper functions but that
    > doesn't appeal to me either. So what would be the best solution if I
    > drop the "without breaking any software using the old API"?


    Make it a pointer and have a null pointer indicate "optionality".
    That's a very common solution.

    V
    Victor Bazarov, Nov 9, 2004
    #6
  7. Otto Wyss

    Jeff Flinn Guest

    "Otto Wyss" <> wrote in message
    news:1gn02vo.1ikqh0gm0r3hwN%...
    > Victor Bazarov <> wrote:
    >
    > > Otto Wyss wrote:
    > > > I'd like to make the cookie parameter optional, i.e. "long& cookie =
    > > > ...", without breaking any software using the old API. The
    > > > implementation looks like
    > > >
    > > > wxTree wxTreeListMainWindow::GetLastChild (const wxTree& item,
    > > > long& cookie) const {

    > >
    > > If you mean, some kind of "default argument value"? The problem with

    this
    > > is that your reference is to a non-const long object. So, it has to

    refer
    > > to (a) something valid, no 'null' references exist, and (b) be a

    reference
    > > to an l-value. The only way to do that, AFAIK, is to provide your own
    > > object and inside compare the address:
    > >
    > > -------------------------------------- in the header
    > > extern long default_cookie;

    >
    > I feared there might not be a nice solution, I don't like to declare a
    > dummy object. I've considered to write wrapper functions but that
    > doesn't appeal to me either. So what would be the best solution if I
    > drop the "without breaking any software using the old API"?


    The simple answer is an overload of GetLastChild without the long&
    parameter.

    wxTree wxTreeListMainWindow::GetLastChild (const wxTree& item )const
    {
    wxCHECK_MSG (item.IsOk(), wxTree(), wxT("invalid tree item"));

    wxArrayTreeListItems& children =
    ((wxTreeListItem*)item.m_pItem)->GetChildren();

    return (!children.IsEmpty())? wxTree(children.Last()): wxTree();
    }

    I favor a more functional approach, avoiding out parameters, by having
    GetlastChild return a std::pair<wxTree,long>. Then client can then ignore
    the long value:

    std::pair<wxTree,long> wxTreeListMainWindow::GetLastChild( const wxTree&
    item )const
    {
    wxCHECK_MSG (item.IsOk(), wxTree(), wxT("invalid tree item"));

    wxArrayTreeListItems& children =
    ((wxTreeListItem*)item.m_pItem)->GetChildren();

    return std::make_pair( (!children.IsEmpty())? wxTree(children.Last()):
    wxTree()
    , children.Count()
    );
    }

    Jeff F
    Jeff Flinn, Nov 9, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. George Marsaglia

    Assigning unsigned long to unsigned long long

    George Marsaglia, Jul 8, 2003, in forum: C Programming
    Replies:
    1
    Views:
    644
    Eric Sosman
    Jul 8, 2003
  2. Daniel Rudy

    unsigned long long int to long double

    Daniel Rudy, Sep 19, 2005, in forum: C Programming
    Replies:
    5
    Views:
    1,160
    Peter Shaggy Haywood
    Sep 20, 2005
  3. Mathieu Dutour

    long long and long

    Mathieu Dutour, Jul 17, 2007, in forum: C Programming
    Replies:
    4
    Views:
    447
    santosh
    Jul 24, 2007
  4. Bart C

    Use of Long and Long Long

    Bart C, Jan 9, 2008, in forum: C Programming
    Replies:
    27
    Views:
    766
    Peter Nilsson
    Jan 15, 2008
  5. veryhotsausage
    Replies:
    1
    Views:
    1,766
    veryhotsausage
    Jul 4, 2008
Loading...

Share This Page