This is valid C++ code, but invalid C code.
int f(char*, int);
int f(char*, int x) // note char* is not named.
C/C++ allows declarations without parameter names, but C requires that
parameters of definitions are named. Why does C++ not have this
behaviour? What value is added by allowing formal parameters in
definitions not to be named?
I initially thought that this was a hangover from C, where concievably,
one might be doing inline assembly and therefore did not need the names
of variables, only that one knows they are on the stack. However, having
found that C does not support this, it made me wonder...
Personally, I can see no value in allowing this syntax - even if a
parameter is not used, what is the harm in giving it a name?
This quirk initially came to light while I was playing with
constructors, an excerpt of which follows:
Animal::Animal(char*, bool isHungry): // Again, note char* is not named.
// Does some stuff here.
The error is subtle; because of the behaviour outlined above, the
compiler (tested with G++ 3.2.3 and VS7.1) does not throw an error, as
Animal::name is defined and thus re-defined in terms of itself
(producing a runtime error that was VERY difficult to trace).
Of course, I have learnt that one must use different names for formal
parameters and locals/members, in order to catch this kind of error at
compile time... But why should I, when, surely, the copiler/language
should provide this basic sanity checking?
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:email@example.com ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.yqcomputer.com/