C++ Primer reading notes--parameter passing

Table of contents

1--Three basic delivery methods

2--Array parameter

3--main function passing parameters

4-- Pass variable parameters


1--Three basic delivery methods

① Value transfer:

        When passing by value, the initial value will be copied to the variable, and changes to the variable will not change the value of the initial value;

② Pointer transfer:

        When passing by pointer, a copy operation will also occur, and the copied content is the value of the pointer ; after copying, the initial pointer and the copy pointer are two different pointers , and both pointers can modify the value of the pointed object, so pointer passing The initial value can be modified;

③ Pass by reference:

        When passing by reference, no copy operation will occur , the formal parameter is equivalent to an alias of the initial variable, modifying the formal parameter will modify the initial value;

        It is generally recommended to use parameter passing by reference to avoid unnecessary copying costs;        

        When the function does not need to change the value of the reference parameter, it is generally declared as a constant reference ;

2--Array parameter

        Arrays have two special properties: they are not allowed to be copied and converted to pointers when using an array ; therefore, when passing an array in a function, what is actually passed is a pointer to the first element of the array ;

3--main function passing parameters

// 方式1
int main(int argc, char *argv[]){...}
// 方式2
int main(int argc, char **argv){...}

        The above two methods are functionally equivalent. The first formal parameter argc indicates the number of strings in the array , and the second formal parameter argv indicates an array whose elements are pointers to C-style strings; where argv[0] indicates the program The name;

4-- Pass variable parameters

        The new C++11 standard provides two methods to handle functions with different numbers of actual parameters. The first is to use the standard library type of initializer list, which is suitable for passing different numbers of actual parameters of the same type; the second is to write variable Parameter templates, used to handle the passing of different numbers and types of actual parameters;

#include <iostream>

// 打印输入的参数列表,其元素类型为 string 类型
void errorPrint(std::initializer_list<std::string> lst)
{
    for(auto beg=lst.begin();beg!=lst.end();++beg){
        std::cout << *beg << " ";
    }
    std::cout << std::endl;
}

void test()
{
    std::string s1, s2;
    std::cout << "Input s1: " << std::endl;
    std::getline(std::cin, s1);
    std::cout << "Input s2: " << std::endl;
    std::getline(std::cin, s2);
    if(s1 != s2){
        errorPrint({"functionX", s1, s2}); // 当 s1 != s2时,将打印输入的所有参数 
    }
    else{
        errorPrint({"functionX", "okay"}); // 传入 2 个参数
    }
}

int main(int argc, int *argv[]){
    test();
}

 

Guess you like

Origin blog.csdn.net/weixin_43863869/article/details/130496218