const修饰参数为不可修改类
文字量,常量和需要进行类型转换的参数只能传递给const的引,而不能传递给非const类型的引用参数
#include <cstdlib>
#include <iostream>
using namespace std;
void print(const char &);
int main(int argc, char *argv[])
{
char a='a';
int b=59;
char *p=&a;
print(a);
print(b);//允许,调用print(char(b));
//print(p);
system("PAUSE");
return EXIT_SUCCESS;
}
void print(const char &x){
cout<<x<<endl;
}
#include <cstdlib>
#include <iostream>
using namespace std;
void print( char &);
int main(int argc, char *argv[])
{
char a='a';
int b=59;
char *p=&a;
print(a);
print(b);//不允许,非const引用参数不能进行类型转换;
//print(p);
system("PAUSE");
return EXIT_SUCCESS;
}
void print( char &x){
cout<<x<<endl;
}