C++/default arguments
Expert: vijayan - 7/21/2009
QuestionHI
i have a question about default arguments in c++.
could you plz tell me what's the implication of"Default arguments must be the rightmost (trailing) arguments in a function's parameter list"
actually i don't know about rightmost(trailling) arguments.
thanx
Bita
AnswerAny parameters after a parameter having a default argument value must have default argument values. That is, you can’t have a default argument followed by a non-default argument.
For example:
void foo( int a, int b = 2, int c = 3 ) ; // ok, trailing defaults
void bar( int a = 1, int b = 2, int c ) ; // error, leading defaults, c must have a default
void baz( int a, int b = 3, int c ) ; // error, default in middle, c must have a default
Also, if you use the default for an argument in a particular function call, you must use the default for all the arguments that follow it in the function’s argument list.
For example:
void foo( 7 ) ; // ok, a specified, b and c are defaulted
void foo( 7, 23 ) ; // ok, a and b specified, c is defaulted
void foo( 7, , 23 ) ; // error, b is defaulted, c must also be defaulted