在 C++ 函数 教程中,我们学习了向函数传递参数。这种方法称为值传递,因为传递的是实际值。
然而,还有另一种传递参数的方法,称为引用传递。
引用传递是函数中一种参数传递方法,函数接收的是实际参数的引用,而不是其值。
例如,
// function that takes value as parameter
void func1(int num_val) {
// code
}
// function that takes reference as parameter
// notice the & before the parameter
void func2(int& num_ref) {
// code
}
int main() {
int num = 5;
// pass by value
func1(num);
// pass by reference
func2(num);
return 0;
}
注意 void func2(int& num_ref)
中的 &
。这表示我们将变量的引用作为参数。
因此,当我们在 main()
中通过将变量 num 作为参数传递来调用 func2()
函数时,我们实际上传递的是 num 变量的引用,而不是值 5。
示例:引用传递
#include <iostream>
using namespace std;
// function definition to swap values
void swap(int& n1, int& n2) {
int temp;
temp = n1;
n1 = n2;
n2 = temp;
}
int main() {
// initialize variables
int a = 1, b = 2;
cout << "Before swapping" << endl;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
// call function to swap numbers
swap(a, b);
cout << "\nAfter swapping" << endl;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
return 0;
}
输出
Before swapping a = 1 b = 2 After swapping a = 2 b = 1
在此程序中,我们将变量 a 和 b 传递给 swap()
函数。请注意函数定义,
void swap(int& n1, int& n2)
这里,我们使用 &
来表示函数接受引用作为其参数。
因此,编译器可以识别出传递给函数参数的是变量的引用,而不是实际值。
在 swap()
函数中,函数参数 n1 和 n2 分别指向变量 a 和 b 的值。因此,交换操作作用于实际值。
const 引用传递
当不需要更改变量的值时,我们可以将它们作为 const
引用传递。
让我们看一个例子。
#include <iostream>
using namespace std;
// function to add two numbers
// using const references
int add(const int& num1, const int& num2) {
return num1 + num2;
}
int main() {
int number1, number2;
// take input
cout << "Enter the first number: ";
cin >> number1;
cout << "Enter the second number: ";
cin >> number2;
// call add function
int sum = add(number1, number2);
// displaying the result
cout << "The sum of " << number1 << " and " << number2 << " is " << sum << endl;
return 0;
}
输出
Enter the first number: 1 Enter the second number: 2 The sum of 1 and 2 is 3
在这里,我们使用 const
关键字通过 const
引用传递值。
使用 const
引用可以防止在函数内部更改值。例如,让我们尝试使用 const 引用交换两个数字。
#include <iostream>
using namespace std;
// function definition to swap values
// using const references
void swap(const int& n1,const int& n2) {
int temp;
temp = n1;
n1 = n2;
n2 = temp;
}
int main() {
// initialize variables
int a = 1, b = 2;
cout << "Before swapping" << endl;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
// call function to swap numbers
swap(a, b);
cout << "\nAfter swapping" << endl;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
return 0;
}
输出
ERROR! /tmp/RXT9OkJpWq.cpp: In function 'void swap(const int&, const int&)': /tmp/RXT9OkJpWq.cpp:9:8: error: assignment of read-only reference 'n1' 9 | n1 = n2; | ~~~^~~~ /tmp/RXT9OkJpWq.cpp:10:8: error: assignment of read-only reference 'n2' 10 | n2 = temp; | ~~~^~~~~~
我们遇到了错误,因为我们试图更改通过 const
引用传递的变量的值。
注意:通过使用 const
引用,我们可以更好地控制我们的代码,因为我们可以明确指定哪些函数可以更改变量的值,哪些不能。这提高了代码的安全性,并使其更易于调试。
指针传递 (不推荐)
上述任务可以使用指针完成。要了解指针,请访问 C++ 指针。
让我们看一个例子。
#include <iostream>
using namespace std;
// function prototype with pointers as parameters
void swap(int*, int*);
int main() {
// initialize variables
int a = 1, b = 2;
cout << "Before swapping" << endl;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
// call function by passing variable addresses
swap(&a, &b);
cout << "\nAfter swapping" << endl;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
return 0;
}
// function definition to swap numbers
void swap(int* n1, int* n2) {
int temp;
temp = *n1;
*n1 = *n2;
*n2 = temp;
}
输出
Before swapping a = 1 b = 2 After swapping a = 2 b = 1
这里,我们可以看到输出与上一个示例相同。请注意这行:
// &a is address of a
// &b is address of b
swap(&a, &b);
这里,在函数调用期间传递的是变量的地址,而不是变量本身。
由于传递的是地址而不是值,因此必须使用解引用运算符 *
来访问该地址中存储的值。
temp = *n1;
*n1 = *n2;
*n2 = temp;
*n1
和 *n2
分别给出存储在地址 n1 和 n2 的值。
由于 n1 和 n2 包含 a 和 b 的地址,因此对 *n1
和 *n2 的任何操作都会更改 a 和 b 的实际值。
因此,当我们在 main()
函数中打印 a 和 b 的值时,这些值已被更改。
警告:通常使用引用而不是指针更简单,出错的可能性也更小,因为它不涉及直接的指针操作。
只有在特定上下文中需要指针,或者与 C 库进行交互时,才应使用指针来传递参数。