In article <1149822787.892001.307610
@g10g2000cwb.googlegroups.com>,
tb****@gmail.com says...
hello ,
I want to create a new function that way:
void foo(int &x,int &y=0) {}
I mean that, I would use it both the way foo(x) and foo(x,y).
but the complier says that was invalid.
A couple of possibilities. One is to simply overload the
function:
void foo(int &x, int &y) {
// whatever
}
void foo(int x) {
int y = 0;
foo(x, y);
}
Another is to create a variable and pass a reference to
it:
int default = 0;
void foo(int &x, int &y = default) {
// whatever
}
Just keep in mind that this probably doesn't make a whole
lot of sense. The primary reason to pass an int by
reference is so the function can modify the variable that
was passed -- and modifying 'default' won't accomplish
much (and may cause your program to do something strange
when/if default is changed to some non-zero value.
--
Later,
Jerry.
The universe is a figment of its own imagination.