473,287 Members | 1,580 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

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 2583
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: ian justice | last post by:
Before i post actual code, as i need a speedyish reply. Can i first ask if anyone knows off the top of their head, if there is a likely obvious cause to the following problem. For the moment i've...
8
by: Don Miller | last post by:
I've been extensively modifying a web application (ASP, COM+) to take advantage of the XMLHTTP object for asynchronous requests (e.g. Ajax) on my dev machine (Win2KPro). I test the application from...
2
by: kma | last post by:
I am designing an Access 2000 database on a computer running Windows 98. I have one form with several tabs; all of which have sub forms, some with a subform on a subform. For example, on my...
6
by: MilanB | last post by:
Hello What "0&" means in this function call? ret = InternetQueryOption(0&, INTERNET_OPTION_CONNECTED_STATE, _ ci, ci_len) Thanks Milan
6
by: scottyman | last post by:
I can't make this script work properly. I've gone as far as I can with it and the rest is out of my ability. I can do some html editing but I'm lost in the Java world. The script at the bottom of...
13
by: Ragnar | last post by:
Hi, 2 issues left with my tidy-work: 1) Tidy transforms a "&amp;" in the source-xml into a "&" in the tidied version. My XML-Importer cannot handle it 2) in a long <title>-string a wrap is...
3
by: Pappy | last post by:
SHORT VERSION: Python File B changes sys.stdout to a file so all 'prints' are written to the file. Python file A launches python file B with os.popen("./B 2>&^1 >dev/null &"). Python B's output...
6
by: Darin Johnson | last post by:
I keep running across that I'm maintaining that likes to define function parameters as "const char &" or "const int &", etc. Ie, constant reference parameters to a primitive type. This is for...
30
by: Medvedev | last post by:
i see serveral source codes , and i found they almost only use "new" and "delete" keywords to make they object. Why should i do that , and as i know the object is going to be destroy by itself at...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.