const那些事-初始化
生活随笔
收集整理的這篇文章主要介紹了
const那些事-初始化
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
常類型是指使用類型修飾符const說明的類型,常類型的變量或對象的值是不能被更新的。
#include<iostream> using namespace std; int main(){const int b = 10;b = 0; //error ,不能對const 變量進行更新const string s = "helloworld";const int i,j=0; }上述有兩個錯誤,第一:b為常量,不可更改!第二:i為常量,必須進行初始化!(因為常量在定義后就不能被修改,所以定義時必須初始化。)
#include<iostream> using namespace std; int main(){const int b = 10;//b = 0; //error ,不能對const 變量進行更新const string s = "helloworld";const int i=20;const int j=30;cout<<"OK" <<endl; }總結
以上是生活随笔為你收集整理的const那些事-初始化的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Pangolin在cmake时报“Cou
- 下一篇: c++ const 类型检查