【C++】Function parameter expansion② (placeholder parameter | rule of placeholder parameter - the actual parameter must be passed in for the placeholder parameter | the default parameter is used in combination with the placeholder parameter)


Blog summary:

  • Default parameter: When declaring a function, define a default value for the function parameter;
  • Default parameter rules: "default parameter" must be defined at the end of the parameter list;
  • Placeholder parameters: only declare the parameter type, do not declare the parameter name;
  • Placeholder parameter rules: placeholder parameters must be passed in actual parameter values;




1. Placeholder parameters




1. Introduction to placeholder parameters


Placeholder parameter concept: In C++ language, "placeholder parameter" is a special function parameter, which is used to reserve a parameter position when the function is defined, and only declares the parameter type, not the parameter name;

Inaccessible: Since the placeholder parameter has no parameter name, the placeholder parameter cannot be accessed in the function body of the function;


The role of placeholder parameters: "placeholder parameters" leave room for expansion for future functions;


2. Rules for placeholder parameters - you must pass in actual parameters for placeholder parameters


Use of function placeholder parameters: If a "placeholder parameter" is defined for the function, when using the function, the actual parameter must be passed in for the placeholder parameter, otherwise an error will be reported if there is one less function;


In the function, 2 normal parameters and 1 placeholder parameter are defined:

// 为函数设置占位参数
void fun(int num, int num1, int) {
    
    
    cout << "num = " << num << " , num1 = " << num1 << endl;
}

If only two common parameters are passed in when calling the above fun function, an error will be reported:

    // 传入 2 个参数
    fun(1, 2);

The error message is as follows:

  • 'fun': function does not accept 2 arguments
1>------ 已启动生成: 项目: HelloWorld, 配置: Debug Win32 ------
1>Hello.cpp
1>Y:\002_WorkSpace\002_VS\HelloWorld\HelloWorld\Hello.cpp(13,13): error C2660: “fun”: 函数不接受 2 个参数
1>Y:\002_WorkSpace\002_VS\HelloWorld\HelloWorld\Hello.cpp(7,6): message : 参见“fun”的声明
1>已完成生成项目“HelloWorld.vcxproj”的操作 - 失败。
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0==========

Error code example:

// 导入标准 io 流头文件 其中定义了 std 命名空间
#include <iostream>
// 导入 std 命名空间
using namespace std;

// 为函数设置占位参数
void fun(int num, int num1, int) {
    
    
    cout << "num = " << num << " , num1 = " << num1 << endl;
}

int main() {
    
    
    // 传入 2 个参数
    fun(1, 2);

    // 传入 3 个参数
    fun(1, 2, 3);
    

    // 控制台暂停
    system("pause");

    return 0;
}

insert image description here





2. The combination of default parameters and placeholder parameters




1. Combined usage


Once the "occupancy parameter" is defined for the function, when using the function, the actual parameter must be passed in for the occupancy parameter, otherwise an error will be reported if there is one less function;

But the placeholder is not the parameter we need, and it cannot be accessed in the method body;

During normal development, it always carries an unnecessary parameter, which is meaningless;

Here you can use placeholder parameters in combination with default parameters,


Set the default of only 2 for the last placeholder parameter of the function;

// 为函数设置占位参数
void fun(int num, int num1, int = 2) {
    
    
    cout << "num = " << num << " , num1 = " << num1 << endl;
}

When calling the above fun function, both 2 parameters and 3 parameters can be passed in;

  • If 2 parameters are passed in, the last placeholder parameter uses the default value 2;
  • If 3 parameters are passed in, the last placeholder parameter uses the value 3 passed in;
    // 传入 2 个参数
    fun(1, 2);

    // 传入 3 个参数
    fun(1, 2, 3);

Regardless of whether the placeholder parameter is passed in, the placeholder parameter is in the function body and cannot be accessed;


2. Code example - combined use of placeholder parameters and default parameters


Code example:

// 导入标准 io 流头文件 其中定义了 std 命名空间
#include <iostream>
// 导入 std 命名空间
using namespace std;

// 为函数设置占位参数
void fun(int num, int num1, int = 2) {
    
    
    cout << "num = " << num << " , num1 = " << num1 << endl;
}

int main() {
    
    
    // 传入 2 个参数
    fun(1, 2);

    // 传入 3 个参数
    fun(1, 2, 3);
    

    // 控制台暂停
    system("pause");

    return 0;
}

Results of the :

num = 1 , num1 = 2
num = 1 , num1 = 2
请按任意键继续. . .

insert image description here

Guess you like

Origin blog.csdn.net/han1202012/article/details/132678857