這段時間在複習C++基礎知識,會不定期寫一些重要的總結,算是這段時間學習過程。
1、關於const與pointer A、指向const的pointer(
指標常量—是指對於指標來說,指向的是常量,實際是不是常量,並不一定) eg:int age = 23; int num = 100; const int * pAge = &age; *pAge = 50; // 非法的,不能使用 age = 50; // 正確的 pAge = # // 正確的。
注意:
pAge
的聲明並不意味著它指向的值實際上就是一個常量,只是意味著對
pAge
而言,這個值是一個常量,並且
pAge
自己不是一個常量。
B、將const變數的地址賦給指向const的pointer(
指標常量) eg: const double PI = 3.141592; double money = 20.5; const double *p_PI = Π p_PI = &money;// OK C、int age = 21;(
常量指標—指標本身是常量) int sloth = 3; int * const pointer = &age; *pointer = 100; // 正確 Pointer = &sloth;// 錯誤 D、double trouble = 2.65; (
指標常量指標) const double * const stick = &trouble; *stick = 3.62; // 錯誤 Stick = &money; // 錯誤 int num = 15; const int age = 22; int * pointer =# *pointer = 20; //pointer = &age; //無法從“const int *__w64 ”轉換為“int *” //不能將常量賦給一個變數 *pointer = 25; const int * conPointer = # //*conPointer = 62; //指標常量 conPointer = &age; //*conPointer = 26; int * const pointerCon = # *pointerCon = 45; //pointerCon = &age;//常量指標 //int * const pointerCon1 = &age;//無法從“const int *__w64”轉換為“int *const” *pointerCon = 52; cout<<"argc:"<<argc<<endl; cout<<"argv[]:"<<*argv<<endl;