Detailed explanation of the way of parameter passing in C++

The type of the formal parameter determines how the formal parameter and the actual parameter interact. If the formal parameter is a reference type, it will be bound to the corresponding actual parameter. Otherwise, it will be copied and assigned to the formal parameter.

One, pass the value parameter

When initializing a variable of a non-reference type, the initial value will be copied to the variable. The change to the variable at this time will not affect the initial value. This is also a way of passing parameters often encountered in textbooks.

void reset(int i) 
{
    
    
	i++;
}

Pointer parameter

The behavior of the pointer is the same as other non-reference types. When the pointer copy is performed, the value of the pointer is copied. After copying, the two pointers are different pointers. Because the pointer points to the address of the object, the value of the object it points to can be modified through the pointer.

void reset(int* ip)
{
    
    
	*ip = 0;//改变了指针ip所指对象的值
	ip = 0;//只改变了ip的局部拷贝
}

In C language, pointers are often used to access external variables. But in C++, we can use reference type parameters instead of pointers.

Two, value reference passing parameters

The reference parameter is directly related to the object to which it is bound, not a copy of these objects. When defining a reference, the reference must be initialized with the object bound to the reference. Reference parameters work exactly the same way. Every time a function is called, a reference parameter is created and associated with the corresponding actual parameter.

void swap(int &a,int &b)
{
    
    
	int c = b;
	b = a;
	a = c;
}

Use reference parameters to return additional information

Another use of reference parameters is to return additional results to the calling function. A function can only return a single value, but sometimes, a function has more than one content that needs to be returned.

int  search_(const std::string s1, char s, int& index)
{
    
    //找到s1中字符为s的返回最后一次出现的位置和次数。
	int res = 0;//次数
	for (int i = 0; i < s1.size(); i++)
	{
    
    
		if (s1[i] == s)
		{
    
    
			index = i;
			res++;
		}
	}
	return res;
}

In this way, multiple values ​​can be returned. Index is changed as a reference type, and the function returns a value representing the number of occurrences at the end. But it is actually equivalent to returning multiple values.

Use references to avoid copying

When passing large objects to a function, you need to use reference parameters, which is another case where reference parameters are applicable. Although copying arguments is not a problem for objects of built-in data types or smaller class type objects, it is (usually) too inefficient for most class types or large arrays. Using the reference parameter, the function can directly access the actual parameter object without copying it.

bool isShorter(const string &s1, const string &s2)
{
    
    
return s1.size() < s2.size();
}

Each of its formal parameters is a reference of type const string. Because the formal parameters are references, the actual parameters are not copied. And because the formal parameter is a const reference, the isShorter function cannot use this reference to modify the actual parameter.

If the sole purpose of using reference parameters is to avoid copying actual parameters, then the parameters should be defined as const references.

Lvalue references and rvalue references

&& defines an rvalue reference. Rvalue references support the realization of movement semantics, which can significantly improve the performance of the application. Because it allows transferring resources from temporary objects that cannot be referenced elsewhere in the program.

void change(int&& a)
{
    
    
	cout << a << endl;
	cout << "右值引用" << endl;
}
void change(const int& a)
{
    
    
	cout << a << endl;
	cout << "左值引用" << endl;
}
int main()
{
    
    
	int a;
	a = 5;
	change(a+1);//右值
	change(a);
}

Detailed explanation of right value, left value

Three, const actual participation in formal parameters

In C++, we are allowed to define several functions with the same name, provided that the parameter lists are clearly different. The top-level const is ignored.

void fun(const int i){
    
    }
//可以读取i,但无法改变
void fun(int i){
    
    }//重复定义,错误

Pointer or reference form participates in const

The initialization method of formal parameters is consistent with the initialization method of variables. We can initialize a low-level const object with a non-constant. At the same time, an ordinary reference must be initialized with an object of the same type.

Wrong wording : unable to initialize string reference with const constant type.

void change(std::string& s){
    
    }
int main()
{
    
    
	change("sss");
}

Try to use constant references

It is a very common mistake to define function parameters that do not change as (ordinary) references. Doing so is misleading, that is, the function can modify its value. In addition, the use of references instead of constant references will greatly limit the types of arguments the function accepts.

Bad design: The first parameter should be of const string type.

std::string::size_type find_char(std::string& s, char c);

In this case, find_char("hello",'o'); will report an error.

Guess you like

Origin blog.csdn.net/weixin_45605341/article/details/109279828