469,903 Members | 2,187 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,903 developers. It's quick & easy.

How to make a "long&" parameter optional

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/"
Jul 22 '05 #1
6 2358
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 );
Jul 22 '05 #2
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;
Jul 22 '05 #3
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
Jul 22 '05 #4
Victor Bazarov <v.********@comAcast.net> 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
Jul 22 '05 #5
Otto Wyss wrote:
Victor Bazarov <v.********@comAcast.net> 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
Jul 22 '05 #6

"Otto Wyss" <ot*******@orpatec.ch> wrote in message
news:1gn02vo.1ikqh0gm0r3hwN%ot*******@orpatec.ch.. .
Victor Bazarov <v.********@comAcast.net> 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
Jul 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

23 posts views Thread by ian justice | last post: by
8 posts views Thread by Don Miller | last post: by
6 posts views Thread by MilanB | last post: by
6 posts views Thread by scottyman | last post: by
6 posts views Thread by Darin Johnson | last post: by
30 posts views Thread by Medvedev | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.