472,354 Members | 2,174 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,354 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 2508
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
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made but the http to https rule only works for...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. header("Location:".$urlback); Is this the right layout the...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...

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.