const指針顧名思義就是常量指針的意思。下面將經過一些具體例子說明const指針的幾種用法。ios
1 #include <iostream> 2 3 using namespace std; 4 5 int main() 6 { 7 int yes = 100; 8 const int *p = &yes; 9 10 *p = 10; //Error assign! 11 12 return 0; 13 }
用g++編譯的結果是:c++
error: assignment of read-only location ‘* p’.
7 const int yes = 100; 8 int *p = &yes;
用g++編譯的錯誤爲:invalid conversion from ‘const int*’ to ‘int*’ .