By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,440 Members | 1,436 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,440 IT Pros & Developers. It's quick & easy.

default parameter to NULL

P: n/a

I have a method with the last arguments having defaults like this

void filing(long, double,
const myType& r_mo=NULL,
const herType& r_tr=NULL);

is that acceptable, if not how is it done?
when I do the above, I get errors like

file.h:19: error: default argument for parameter of type ‘const myType&’ has type ‘int’
file.h:20: error: default argument for parameter of type ‘const herType&’ has type ‘int’
Dec 21 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Gary Wessle" <ph****@yahoo.comwrote in message
news:m3************@localhost.localdomain...
>
I have a method with the last arguments having defaults like this

void filing(long, double,
const myType& r_mo=NULL,
const herType& r_tr=NULL);

is that acceptable, if not how is it done?
when I do the above, I get errors like

file.h:19: error: default argument for parameter of type 'const myType&'
has type 'int'
file.h:20: error: default argument for parameter of type 'const herType&'
has type 'int'
A reference can't have a NULL value, one reason being becasue you can't
check for it. You can do that with a pointer, but not a reference.
Dec 21 '06 #2

P: n/a
Gary Wessle wrote:
I have a method with the last arguments having defaults like this

void filing(long, double,
const myType& r_mo=NULL,
const herType& r_tr=NULL);

is that acceptable, if not how is it done?
when I do the above, I get errors like
No, it is not reasonable to attempt to pass NULL for any parameter
declared a reference - a reference requires that the caller always
supply an object (of a suitable type) in order to call the function.

To indicate that values for myType and herType are not required in
order to call filing(), there are a few options available. One approach
is for filing to accept pointers instead of references for those
values:

void filing( long, double,
const myType* r_mo = NULL,
const herType* r_tr = NULL);

A better approach would probably be to overload filing() - with one
overload accepting the two reference parameters and another overload of
filing() that does not:

void filing( long, double, const myType& r_mo, const herType&);
void filing( long, double);

Greg

Dec 21 '06 #3

P: n/a

"Greg д
"
Gary Wessle wrote:
I have a method with the last arguments having defaults like this

void filing(long, double,
const myType& r_mo=NULL,
const herType& r_tr=NULL);

is that acceptable, if not how is it done?
when I do the above, I get errors like

No, it is not reasonable to attempt to pass NULL for any parameter
declared a reference - a reference requires that the caller always
supply an object (of a suitable type) in order to call the function.

To indicate that values for myType and herType are not required in
order to call filing(), there are a few options available. One approach
is for filing to accept pointers instead of references for those
values:

void filing( long, double,
const myType* r_mo = NULL,
const herType* r_tr = NULL);

A better approach would probably be to overload filing() - with one
overload accepting the two reference parameters and another overload of
filing() that does not:

void filing( long, double, const myType& r_mo, const herType&);
void filing( long, double);

Greg
I agree with you.

Dec 21 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.