473,890 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 wxTreeListMainW indow::GetLastC hild (const wxTree& item,
long& cookie) const {
wxCHECK_MSG (item.IsOk(), wxTree(), wxT("invalid tree item"));
wxArrayTreeList Items& children = ((wxTreeListIte m*)
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.IsEm pty())? 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.cp p?view=markup"

O. Wyss

--
See a huge pile of work at "http://wyodesktop.sour ceforge.net/"
Jul 22 '05 #1
6 2627
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 wxTreeListMainW indow {
....
static long dummy;
wxTree GetLastChild(co nst WxTree& item, long& cookie = wxTreeListMainW indow::dummy)co nst;
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 wxTreeListMainW indow::GetLastC hild (const wxTree& item,
long& cookie) const {
wxCHECK_MSG (item.IsOk(), wxTree(), wxT("invalid tree item"));
wxArrayTreeList Items& children = ((wxTreeListIte m*)
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.IsEm pty())? 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 wxTreeListMainW indow {
...
wxTree GetLastChild(co nst wxTree& item,
long& cookie = default_cookie) ;
...
};
--------------------------------------- in the C++ file
...
extern long default_cookie = 0; // definition!!!
...
wxTree wxTreeListMainW indow::GetLastC hild (const wxTree& item,
long& cookie) const {
...
if (&cookie == &default_cookie ) {
// default
}
...
}
--------------------------------------------------------

V
Jul 22 '05 #4
Victor Bazarov <v.********@com Acast.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 wxTreeListMainW indow::GetLastC hild (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.cp p?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.********@com Acast.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
implementati on looks like

wxTree wxTreeListMainW indow::GetLastC hild (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 "optionalit y".
That's a very common solution.

V
Jul 22 '05 #6

"Otto Wyss" <ot*******@orpa tec.ch> wrote in message
news:1gn02vo.1i kqh0gm0r3hwN%ot *******@orpatec .ch...
Victor Bazarov <v.********@com Acast.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 wxTreeListMainW indow::GetLastC hild (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 wxTreeListMainW indow::GetLastC hild (const wxTree& item )const
{
wxCHECK_MSG (item.IsOk(), wxTree(), wxT("invalid tree item"));

wxArrayTreeList Items& children =
((wxTreeListIte m*)item.m_pItem )->GetChildren( );

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

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

std::pair<wxTre e,long> wxTreeListMainW indow::GetLastC hild( const wxTree&
item )const
{
wxCHECK_MSG (item.IsOk(), wxTree(), wxT("invalid tree item"));

wxArrayTreeList Items& children =
((wxTreeListIte m*)item.m_pItem )->GetChildren( );

return std::make_pair( (!children.IsEm pty())? 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
5704
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 reduced my form request to a simple text string entry, instead of my desired optional parameters. As i have been stuck with a single unfathomable glitch for over a year. Basically, if i enter queries such as ; "select * from table" "select * from...
8
6021
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 another machine on the network (or using IE, Firefox, etc. on the same machine). Everything seems to work fine (no errors anywhere) and the web application is snappy as intended. However, at the end of the day when I turn off my dev machine,...
2
4683
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 main Job form I have a tab for PO's to keep track of all purchase orders pertaining to a specific job. The first subform is for the PO numbers related to the jobs on the main form and the next subform is for all of the PO lines for each PO. ...
6
7584
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
4912
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 the html page controls the form fields that are required. It doesn't function like it's supposed to and I can leave all the fields blank and it still submits the form. Also I can't get it to transfer the file in the upload section. The file name...
13
2811
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 produced like: <title>my very long title blab la blab la Blabla bla </title> Importer also has got problems with it
3
2205
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 disappears into never-never land. LONG VERSION: I am working on a site that can kick off large-scale simulations. It will write the output to an html file and a link will be emailed to the user. Also, the site will continue to display...
6
5606
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 normal functions, not operators. I keep changing these to just have the plain old type, which is more efficient (I'm using embedded systems) and less obtuse. I'm puzzled why this one programmer insisted on odd style everywhere. Maybe he's just...
30
3881
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 the end of the app for example: class test { public: int x;
0
9975
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9812
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10799
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10899
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
8004
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7154
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5832
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6032
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4255
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.