各位大神看一下
//const #include
using namespace std; int main(void) { ? ? ? ? const int count = 3; ? ? const int *p = &count; ? ?//打印count次字符串Hello C++ for(int i = 0; i < *p; i++) { cout << "Hello imooc" << endl; } return 0; } 這段代碼的第二個const不加為什么無法運行
//const #include
using namespace std; int main(void) { ? ? ? ? const int count = 3; ? ? const int *p = &count; ? ?//打印count次字符串Hello C++ for(int i = 0; i < *p; i++) { cout << "Hello imooc" << endl; } return 0; } 這段代碼的第二個const不加為什么無法運行
2018-03-07
舉報
2018-03-07
count定義的是const類型的,說明count是不能改變值的,而你下面又定義了一個可變的*p指向了count,豈不是接下來就可以寫*p=?來改變count的值了嗎?所以編譯會報錯。指向常量的指針也必須定義成const int *p