C++普通變數、C++靜態成員變數、C++成員常量、C++靜態成員常量的初始化方法
阿新 • • 發佈:2018-12-30
最近複習C++的一些知識,遇到了一個我比較容易混淆、傻傻分不清的知識點,那就是C++的變數的型別初始化或賦初值,大致有普通成員變數、靜態成員變數、成員常量這三種,還有一種組合的靜態成員常量。
看到這幾種資料成員時很尷尬,經常就把某兩種搞混了,所以為了方便和好理解,在前輩的基礎上做個小總結。
為了直觀簡潔的呈現這種對比,採用程式碼對比的方式完成這個事情。包括四種資料成員,進行四種初始化、賦初值作對比。
程式碼如下。
#include<iostream> #include<string.h> using namespace std; // 四種資料成員:1.常量資料成員const 2.靜態資料成員static 3.普通資料成員(normal) 4.靜態成員常量static const // 四種賦值方式:1.直接初始化(在宣告時就賦值) 2.先宣告再通過初始化列表賦初值 3.先宣告再在建構函式體裡賦初值 4.先宣告再在類外賦初值 class Example{ public: const int i_const_1 = 10; const int i_const_2; const int i_const_3; const int i_const_4; //static int i_static_1 = 10;// Error:帶有類內初始值設定項的成員必須為常量 static int i_static_2; static int i_static_3; static int i_static_4; int i_normal_1 = 10; int i_normal_2; int i_normal_3; int i_normal_4; static const int i_sta_con_1 = 10;// static const int i_sta_con_1 = 10; 在這裡初始化也可以 static const int i_sta_con_2; static const int i_sta_con_3; static const int i_sta_con_4; Example(int t) : i_const_2(t) //, i_static_2(t)// Error:不是類"Example"的非靜態資料成員或基類 , i_normal_2(t) // /* // Error:"Example::Example(int t)"未提供初始值設定項:常量 成員"Example::i_const_3" 常量 成員"Example::i_const_4" , i_const_3(t) , i_const_4(t) // , i_sta_con_2(t) //ERROR:因為是靜態資料成員,所以不能在建構函式的初始化列表處初始化靜態常量,因為建構函式可能會被多次呼叫的,而靜態資料只能被初始化一次 // */ { // i_const_3 = 10;// Error:表示式必須是可修改的左值 // i_static_3 = 10; // error LNK2001: 無法解析的外部符號 i_normal_3 = 10; // i_sta_con_3 = 10; //ERROR:因為是靜態資料成員,所以不能在建構函式內初始化靜態常量,因為建構函式可能會被多次呼叫的,而靜態資料只能被初始化一次 } }; //int example::i_const_4 = 10;// Error:非靜態的類資料成員不能在其類的外部定義 int Example::i_static_4 = 10; //int example::i_normal_4 = 10;// Error:非靜態的類資料成員不能在其類的外部定義 //int Example::i_sta_con_4 = 10; //Error:(不知道為什麼不行???) const int Example::i_sta_con_4 = 10; int main() { Example e(10); cout << "i_const_1: " << e.i_const_1 << endl; cout << "i_const_2: " << e.i_const_2 << endl; cout << "i_static_4: " << e.i_const_4 << endl; cout << "i_normal_1: " << e.i_normal_1 << endl; cout << "i_normal_2: " << e.i_normal_2 << endl; cout << "i_normal_3: " << e.i_normal_3 << endl; cout << "i_sta_con_1: " << e.i_sta_con_1 << endl; cout << "i_sta_con_4: " << e.i_sta_con_1 << endl; return 0; }
執行結果如圖:
由此可以很清晰的看到,不同的資料成員有著不同的初始化方式。為了更直觀點,做個表對比。
資料成員型別 | normal | const | static | static const |
類內直接初始化(在宣告時就賦值) | √ | √ | × | √ |
先宣告再通過初始化列表賦初值 | √ | √ | × | × |
先宣告再在建構函式體裡賦初值 | √ | × | × | × |
先宣告再在類外賦初值 | × | × | √ | √ |
還有問題:
1.因為在類宣告時,並沒有例項化物件,也就是沒有分配記憶體,所以C++的成員常量的初始化只能在建構函式的初始化列表中進行,但是本文中上述的程式碼“const int i_const_1 = 10;”即直接在類體內初始化資料常量,經過測試也沒報錯答案也正確,這是為什麼呢?
2.
//int Example::i_sta_con_4 = 10; //Error:(不知道為什麼不行???) const int Example::i_sta_con_4 = 10;
最後的靜態常量用第二種方法賦初值時,直接賦值不可行,報錯誤,必須要加上const關鍵字限定,然後就可以順利賦初值,不曉得為什麼?有大佬可以解釋解釋嗎?