Why should default parameters be added last in C++ functions?

前端 未结 9 1661
轻奢々
轻奢々 2020-11-27 07:01

Why should default parameters be added last in C++ functions?

9条回答
  •  伪装坚强ぢ
    2020-11-27 07:52

    If you define the following function:

    void foo( int a, int b = 0, int c );
    

    How would you call the function and supply a value for a and c, but leave b as the default?

    foo( 10, ??, 5 );
    

    Unlike some other languages (eg, Python), function arguments in C/C++ can not be qualified by name, like the following:

    foo( a = 10, c = 5 );
    

    If that were possible, then the default arguments could be anywhere in the list.

提交回复
热议问题