一听就懂:引用做函数参数的注意点

1.引用做函数参数的注意点

 1 #include <iostream>
 2 using namespace std;
 3 void show(int& ref)
 4{
 5     cout << ref << endl;
 6 }
 7 void show01(const int& mRef)
 8 {
 9     cout << mRef << endl;
10 }
11 void show02(int&& MRef)
12 {
13     MRef++;
14     cout << MRef << endl;
15 }
16 int main()
17 {
18     int age = 18;
19     show(age);
20     //show(22);//无法将参数从“const int”转换为“int &”
21     show01(33);
22     show02(44);
23     //show02(age);//无法将参数从“int”转换为“int &&”
24     show02((int&&)age);//(int&&)强制转换不合规范
25     show02(std::move(age));//使用std::move()把左值转换成右值
26     return 0;
27 }

 

2.

 

posted @ 2025-02-13 00:12  java帝国  阅读(2)  评论(0)    收藏  举报