[prog] C++, global function with optional arguments
Robert J. Hansen
rjh at sixdemonbag.org
Sat Apr 19 08:43:46 EST 2003
> I have written a golbal function with the following declaration
> void trim (char str[], int left = 1, int right = 1);
I think you'll find an interesting bug here, but I'm not going to tell
you about it in advance--you'll learn more this way. :)
> It removes leading and trailing whitespace, if left and right, resectively,
> are true.
The function declaration should read, if you want to be C++ish about it,
void trim(char *str, bool left = true, bool right = true);
You can use integers and int literals as boolean values, but using the
dedicated bool type is thought of as more "correct style".
> When I have the function present without a separate declaration it works Ok,
> but when I remebered that I needed the function deaclaration as well as the
> function itself I got the following errors.
One of C++'s lesser-publicized quirks: when listing default arguments,
they can be in either the declaration or the definition, but not both.
For instance:
// my_header.h
int func_with_default_args(int x = 100);
// my_header.cc
int func_with_default_arts(int x) { ... }
--
Robert J. Hansen <rjh at sixdemonbag.org>
More information about the Programming
mailing list