この内容は古いバージョンです。最新バージョンを表示するには、戻るボタンを押してください。
バージョン:1
ページ更新者:T
更新日時:2020-04-01 23:54:48

タイトル: 定数の宣言
SEOタイトル: 【C++】定数の宣言

実装例

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;
      |     ~~^~~