C++除了指针给函数传递地址,还有引用传递 pass-by-reference
带引用的函数调用比带指针的函数调用更清晰
//: C03:PassReference.cpp
// From Thinking in C++, 2nd Edition
// Available at http://www.BruceEckel.com
// (c) Bruce Eckel 2000
// Copyright notice in Copyright.txt
#include <iostream>
using namespace std;
void f(int& r) {
cout << "r = " << r << endl;
cout << "&r = " << &r << endl;
r = 5;
cout << "r = " << r << endl;
}
int main() {
int x = 47;
cout << "x = " << x << endl;
cout << "&x = " << &x << endl;
f(x); // Looks like pass-by-value,
// is actually pass by reference
cout << "x = " << x << endl;
getchar();
} ///:~
在函数f()参数列表中,不用int*传递指针,而是用int&传递引用
已经不是值的拷贝在传递参数了
引用是使用地址传递参数
引用传递是语法糖 相当于指针
输出
x = 47
&x = 0133F838
r = 47
&r = 0133F838
r = 5
x = 5