code:
#include
#include
using namespace std;
int main()
{
int a = 666;
int& b = a; // 编译器内部自动转化为 int* const b = & a;
b = 888; // 发现是引用,自动转化为 *b=888;
cout << "a的值:" << a << endl;
system("pause");
}
result:
code:
int a = 10;
cout << "address of a:" << &a << ", value of a:" << a << endl;
int x = 20;
cout << "address of x:" << &x << ", value of x:" << x << endl;
int & b = a; //引用声明
cout << "address of b:" << &b << ", value of b:" << b << endl;
b = x; //是变量赋值,而不是将该引用作为其它变量的引用
cout << "address of a:" << &a << ", value of a:" << a << endl;
cout << "address of b:" << &b << ", value of b:" << b << endl;
result:
address of a:000000D209FCFB34, value of a:10
address of x:000000D209FCFB54, value of x:20
address of b:000000D209FCFB34, value of b:10
address of a:000000D209FCFB34, value of a:20 // a的值变为20
address of b:000000D209FCFB34, value of b:20
值传递仅仅改变形参,实参不发生变化
code:
#include
#include
using namespace std;
void swap(int x, int y)
{
cout << "值传递swap之前:x, y分别为: " << x << "," << y << endl;
int temp;
temp = x;
x = y;
y = temp;
cout << "值传递swap之后:x, y分别为: " << x << "," << y << endl;
}
int main()
{
int a1 = 666;
int b1 = 888;
cout << "值传递swap之前:a1, b1分别为: " << a1 << "," << b1 << endl;
swap(a1, b1);
cout << "值传递swap之后:a1, b1分别为: " << a1 << "," << b1 << endl;
system("pause");
return 0;
}
result:
值传递swap之前:a1, b1分别为: 666,888
值传递swap之前:x, y分别为: 666,888
值传递swap之后:x, y分别为: 888,666
值传递swap之后:a1, b1分别为: 666,888
指针作为参数,传递的是实参的地址值,那么对该地址的操作,会影响到实参。
code:
#include
#include
using namespace std;
void swap_pt(int *x, int *y)
{
cout << "x, y地址分别为: " << x << "," << y << endl;
cout << "值传递swap之前:*x, *y分别为: " << *x << "," << *y << endl;
int temp;
temp = *x;
*x = *y;
*y = temp;
cout << "值传递swap之后:*x, *y分别为: " << *x << "," <<* y << endl;
}
int main()
{
int a1 = 666;
int b1 = 888;
cout << "a1, b1地址分别为: " << &a1 << "," << &b1 << endl;
cout << "值传递swap之前:a1, b1分别为: " << a1 << "," << b1 << endl;
swap_pt(&a1, &b1);
cout << "值传递swap之后:a1, b1分别为: " << a1 << "," << b1 << endl;
system("pause");
return 0;
}
result:
a1, b1地址分别为: 00000006F5D4F904,00000006F5D4F924
值传递swap之前:a1, b1分别为: 666,888
x, y地址分别为: 00000006F5D4F904,00000006F5D4F924
值传递swap之前:*x, *y分别为: 666,888
值传递swap之后:*x, *y分别为: 888,666
值传递swap之后:a1, b1分别为: 888,666
引用传递,实参和形参是同一变量
code:
#include
#include
using namespace std;
void swap_reference(int &x, int &y)
{
cout << "x, y地址分别为: " << &x << "," << &y << endl;
cout << "引用传递swap之前:x, y分别为: " << x << "," << y << endl;
int temp;
temp = x;
x = y;
y = temp;
cout << "引用传递swap之后:x, y分别为: " << x << "," << y << endl;
}
int main()
{
int a1 = 666;
int b1 = 888;
cout << "a1, b1地址分别为: " << &a1 << "," << &b1 << endl;
cout << "引用传递swap之前:a1, b1分别为: " << a1 << "," << b1 << endl;
swap_reference(a1, b1);
cout << "引用传递swap之后:a1, b1分别为: " << a1 << "," << b1 << endl;
system("pause");
return 0;
}
result:
a1, b1地址分别为: 0000003871CFFB14,0000003871CFFB34
引用传递swap之前:a1, b1分别为: 666,888
x, y地址分别为: 0000003871CFFB14,0000003871CFFB34
引用传递swap之前:x, y分别为: 666,888
引用传递swap之后:x, y分别为: 888,666
引用传递swap之后:a1, b1分别为: 888,666
//不要返回局部变量的引用,局部变量在栈区,调用完毕后释放,有可能第一次使用该引用时,编译器可能还没释放,但是后面会出错
code:
#include
#include
using namespace std;
int & test()
{
int x = 100;
return x;
}
int main()
{
int& ref = test();
cout << "ref的值1:" << ref << endl;
cout << "ref的值2:" << ref << endl;
system("pause");
}
result:
ref的值1:632365056
ref的值2:632365056
code:
#include
#include
using namespace std;
int& test_static()
{
static int x = 0;
x += 666;
return x;
}
int main()
{
int& ref = test_static(); // ref是函数中x的别名
test_static() = 2000; // test_static()返回的就是x的别名,相当对x=2000,
cout << "ref的值:" << ref << endl;
system("pause");
}
result:
ref的值:2000
code:
#include
#include
using namespace std;
int main()
{
int a = 666;
//int& b = 888; // 报错,必须引用合法的内存空间,888都不知道地址是什么
const int &b = 888; // 自动转化为 int temp = 888; const int &b = temp; 只能使用别名
//b = 30; // 不可再修改
cout << "b的值:" << b << endl;
system("pause");
}
code:
#include
#include
using namespace std;
void print_data(const int& a)
{
//a = 100; //错误,已经不可再修改
cout << "a = " << a << endl;
}
int main()
{
int a = 666;
print_data(a);
}
result:
a = 666
code:
#include
using namespace std;
float temp;
float fn1(float r) {
temp = r * r * 3.14;
return temp;
}
float& fn2(float r) { //&说明返回的是temp的引用,换句话说就是返回temp本身
temp = r * r * 3.14;
return temp;
}
int main() {
float a = fn1(5.0); //case 1:返回值
//float &b=fn1(5.0); //case 2:用函数的返回值作为引用的初始化值 [Error] invalid initialization of non-const reference of type 'float&' from an rvalue of type 'float'
//(有些编译器可以成功编译该语句,但会给出一个warning)
float c = fn2(5.0); //case 3:返回引用
float& d = fn2(5.0); //case 4:用函数返回的引用作为新引用的初始化值
cout << a << endl;//78.5
//cout<
cout << c << endl;//78.5
cout << d << endl;//78.5
system("pause");
return 0;
}
code:
int x=fn1(5.0); //x不释放
int &b=x;
注意:
本文中的部分内容来自伯乐在线