Pass by value and pass by reference in C++

pass-by-value and pass-by-reference


pass by value

When the value of the actual parameter is copied to the formal parameter, the actual parameter and the formal parameter are two independent variables, we say that the actual parameter is passed by value or the function is called by value.

- Ordinary formal parameters The
formal parameters are independent of the actual parameters, and changes to the formal parameters will not affect the actual parameters.

 int i = 0;
 void change(int z)
 {
     z = 43;  //i的值仍为0
 } 
change(i);

After the above code is executed, the value of i is still 0;

- Pointer parameter
When a pointer copy operation is performed, the value of the pointer's value pointer is copied, and the two pointers are different pointers after copying. The object pointed to by the pointer can be modified indirectly through the pointer.

int i = 0;
void change(int  *p)
{
    *p = 43;    //i的值为43
    p = 0;      //只改变指针p的局部拷贝,实参未改变
} 

pass by reference

By binding a formal parameter to an actual parameter, the operation on the reference actually acts on the object to which the reference refers. Advantages of using pass by reference:

  • Use references to avoid copying - if the function does not need to modify the value of the reference parameter, it is better to declare it as a constant reference;
bool isShorter(const string &s1, const string &s2)
{
    return s1.size() < s2.size();
}
  • Using a reference parameter can return additional information, since modifying the parameter is valid for the actual parameter passed in.
int i = 0;
void change(int &p)
{
    p = 43;    //i的值为43
} 

const formal and actual parameters

top-level const & bottom-level const

Top-level const: The pointer itself is a constant, and the object pointed to by the pointer is not a constant.
Underlying const: The object pointed to by the pointer is a constant.

 int i = 0;
 const int ci = 4;  //顶层const,不允许改变ci的值
 int *const p1 = &i; //顶层const,不能改变p1值
 const int *p2 = &ci; //底层const, 允许修改p2的值

Pointer or reference form participates in const

    void reset(int *i)
    {
        *i = 0; //改变i指向的值
    }
    void reset(int &k)
    {
        k = 0; //改变k指向的值
    }

Parameters are initialized in the same way as variables are initialized.

How to initialize variables

    int i= 0const int* cp = &i; //底层const,cp不能改变i的值
    const int &r = i;  //底层const, 但是r不能改变i
    const int &r2 = 42//字面量初始化一个常量引用

    int *p = cp;  //错误,p的类型不是常量指针
    int &r3 = r; //错误,r3得类型和r的类型不匹配
    int &r4 = 42; //错误,不能用字面量初始化一个非常量引用 

Form parameter initialization method

    int i= 0
    const int ci = i;
    string::size_type ctr = 0;
    reset(&i);    //调用形参类型是int*的reset函数
    reset(&ci);   //错误,不能用指向const int对象的指针初始化int*
    reset(i);   //调用形参类型是int&的reset函数 
    reset(4);    //不能把字面量绑定至普通类型上
    reset(ctr);    //类型不匹配

Summary: When the function does not make changes to the incoming parameter, the parameter should be set to a constant reference. Defining a formal parameter that does not change as a normal reference can easily mislead the caller that the parameter can be modified. And we can't pass const objects, literal values ​​to ordinary reference parameters. Example: reset(4); //Cannot bind literals to ordinary types

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325940140&siteId=291194637
Recommended