为什么这段代码在编译时会显示错误?
#include <iostream>
using namespace std;
void foo(int& x){
// cout<<x;
}
int main(){
//int x=3;
foo(3);
return 0;
}但是,通过将参数更改为const,它可以正确编译
#include <iostream>
using namespace std;
void foo(const int& x){
// cout<<x;
}
int main(){
//int x=3;
foo(3);
return 0;
}但是我仍然在传递一个整数,那么如何通过在参数中添加const来编译它呢?
发布于 2016-06-24 06:12:43
int& x是可以改变的,所以它不能像3一样引用const int。
const int& x不能更改,类型与const int完全匹配,就像3一样,那么为什么您认为它会失败呢?
https://stackoverflow.com/questions/38002706
复制相似问题