const

来源:互联网 发布:白酒网络营销策划书 编辑:程序博客网 时间:2024/06/07 00:05

来看下面一段代码:

#include <iostream>using namespace std;int main(){const int MAX_NUM = 731;const string EMAIL = "x-lion@xxx.edu.cn";//MAX_NUM = 130;// expression must be a modifiable lvaluedint a, b;const int* p = &a;//*p = 5;// expression must be a modifiable lvalueda = 5;//OKp = &b;int* pp = new int;p = pp;//pp = p;//a value of type "const int*" cannot be assigned to an entity of type "int *" 为什么? 因为我们倾向于不修改const int* 指向的值。pp = (int*) p;//当然我们还是可以强制赋值的。int c;const int& d = c;//d = 99;// expression must be a modifiable lvaluedc = 99;return 0;}void MyPrintf(const char* p){//strcpy(p, "C++");//argumrnt of type "const char*" is incompatible with parameter of type "char*"printf("%s", p);}


顺利地阅读这段代码,const的有关基本问题就掌握咯! 不懂的就求求度娘咯。

0 0
原创粉丝点击