来看下面一段代码:
#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 lvalued
int a, b;
const int* p = &a;
// *p = 5; // expression must be a modifiable lvalued
a = 5; //OK
p = &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 lvalued
c = 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);
}