下面这个c++程序
-
- #include <iostream>
- using namespace std;
- int test(int & i)
- {
- return i;
- }
- int main()
- {
- short i=10;
- cout<<test(i)<<endl;
- return 0;
- }
不能编译通过,以为connot convert parameter form 'short' to ‘int&’
但是加上const就可以了
-
- #include <iostream>
- using namespace std;
- int test(const int & i)
- {
- return i;
- }
- int main()
- {
- short i=10;
- cout<<test(i)<<endl;
- return 0;
- }
谁能解释一下,为什么加了const就可以编译成功了
|