请思考
- 有哪些引用类型?
- 有万能引用这个说法吗?
- 深思c++引用的作用
代码1
#include<iostream>
using namespace std;
void func1(const int &x) {
cout << __PRETTY_FUNCTION__ << " called" <<endl;
return ;
}
int main() {
int n;
const int y = 123;
func1(n);
func1(y);
func1(123 + 456);
return 0;
}
运行结果
void func1(const int&) called
void func1(const int&) called
void func1(const int&) called
代码2
#include<iostream>
using namespace std;
void func1(int &x) {
cout << __PRETTY_FUNCTION__ << " called" <<endl;
return ;
}
void func1(const int &x) {
cout << __PRETTY_FUNCTION__ << " called" <<endl;
return ;
}
void func1(int &&x) {
cout << __PRETTY_FUNCTION__ << " called" <<endl;
return ;
}
void func1(const int &&x) {
cout << __PRETTY_FUNCTION__ << " called" << endl;
}
int main() {
int n;
const int y = 123;
func1(n);
func1(y);
func1(123 + 456);
return 0;
}
运行结果
void func1(int&) called
void func1(const int&) called
void func1(int&&) called