C ++ - default parameters

A default parameter
in C ++, is the default parameters specify a default value for the parameter of the function declaration or the function is defined. When the function is called, if no argument is used the default value, otherwise specified arguments.

Full default parameters

void TestFunc(int a = 10, int b = 20, int c = 30) {
cout<<"a = "<<a<<endl;
cout<<"b = "<<b<<endl;
cout<<"c = "<<c<<endl; }

Semi default parameters

void TestFunc(int a, int b = 10, int c = 20) {
cout<<"a = "<<a<<endl;
cout<<"b = "<<b<<endl;
cout<<"c = "<<c<<endl; }

Second, the parameters of the default rule

  1. Semi default parameters must be given in sequence from right to left, not to the interval
  2. The default parameters can not both be in function declarations and definitions (can only be defined in a statement, but also in the definition, only choose one)
  3. The default value must be a constant or a global variable
Published 77 original articles · won praise 23 · views 7559

Guess you like

Origin blog.csdn.net/Hots3y/article/details/100524752