実装例

int main(void){

    const int a = 1;

    a = 2;

}

上記は定数に対して値を代入しようとしているので、コンパイル時にエラーとなる。

 

エラー

> g++ -o test test.cpp
test.cpp: In function 'int main()':
test.cpp:8:7:
error: assignment of read-only variable 'a'
    8 |     a = 2;
      |     ~~^~~