1. 程式人生 > >特別沒前途的C++ - explicit和volatile/const的內容

特別沒前途的C++ - explicit和volatile/const的內容

color .exe 編程 tar 通過 code 程序 col func

第一眼見到explicit和volatile可能會一楞一楞的覺得可能是c11或者c14新加的標識符。

其實不是這樣,volatile和const兩個關鍵字在C語言的第二個版本KR C的時候就被加入了C標準,他們是兩個相對的關鍵字

const 修飾符表示這是一個常量類型,這個變量的值不會被程序改變

volatile 修飾符表示這個變量可能被編譯器以外的行為(譬如內聯匯編程序)改變。

修飾常量變量只要和類型緊挨著就可以

int const a = 1;
const int a = 1;

修飾指針時以*號為分界符號

#include <iostream>
#include 
<iostream> int main() { int a = 10; int b = 20; const int *p1 = &a; int const *p2 = &a; int *const p3 = &a; const int *const p4 = &a; int const *const p5 = &a; printf("&a = %X\n", &a); printf("&b = %X\n", &b); printf(
"p1 = 0x%X , *p1 = %d\n", p1, *p1); printf("const int *p1 = &a; \n (*p1) = b;通過p修改a的值\n"); // (*p1) = b; p1 = &b; printf("p1 = 0x%X , *p1 = %d\n", p1, *p1); printf("p2 = 0x%X , *p2 = %d\n", p2, *p2); printf("int const *p2 = &a; \n (*p2) = b;通過p修改a的值\n"); // (*p2) = b;
p2 = &b; printf("p2 = 0x%X , *p2 = %d\n", p2, *p2); printf("p3 = 0x%X , *p3 = %d\n", p3, *p3); printf("int *const p3 = &a; \n p3 = &b;修改p的指向\n"); // p3 = &b; (*p3) = b; printf("p3 = 0x%X , *p3 = %d\n", p3, *p3); printf("p4 = 0x%X , *p4 = %d\n", p4, *p4); printf("const int *const p4 = &a; \n 二者都不能修改\n"); // (*p4) = &b; // p4 = &b; printf("p4 = 0x%X , *p4 = %d\n", p4, *p4); printf("p5 = 0x%X , *p5 = %d\n", p5, *p5); printf("int const *const p5 = &a; \n 二者都不能修改\n"); // (*p5) = &b; // p5 = &b; printf("p5 = 0x%X , *p5 = %d\n", p5, *p5); return 0; }

gcc報錯結果

J:\SITP\alg\main.cpp: In function int main():
J:\SITP\alg\main.cpp:14:11: error: assignment of read-only location * p1
     (*p1) = b;
           ^
J:\SITP\alg\main.cpp:17:11: error: assignment of read-only location * p2
     (*p2) = b;
           ^
J:\SITP\alg\main.cpp:20:8: error: assignment of read-only variable p3
     p3 = &b;
        ^
J:\SITP\alg\main.cpp:23:11: error: assignment of read-only location *(const int*)p4
     (*p4) = &b;
           ^
J:\SITP\alg\main.cpp:23:11: error: invalid conversion from int* to int [-fpermissive]
J:\SITP\alg\main.cpp:24:8: error: assignment of read-only variable p4
     p4 = &b;
        ^
J:\SITP\alg\main.cpp:26:11: error: assignment of read-only location *(const int*)p5
     (*p5) = &b;
           ^
J:\SITP\alg\main.cpp:26:11: error: invalid conversion from int* to int [-fpermissive]
J:\SITP\alg\main.cpp:27:8: error: assignment of read-only variable p5
     p5 = &b;
        ^
mingw32-make.exe[3]: *** [CMakeFiles/alg.dir/main.cpp.obj] Error 1
mingw32-make.exe[2]: *** [CMakeFiles/alg.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles/alg.dir/rule] Error 2
CMakeFiles\alg.dir\build.make:61: recipe for target CMakeFiles/alg.dir/main.cpp.obj failed
CMakeFiles\Makefile2:66: recipe for target CMakeFiles/alg.dir/all failed
CMakeFiles\Makefile2:78: recipe for target CMakeFiles/alg.dir/rule failed
mingw32-make.exe: *** [alg] Error 2
Makefile:117: recipe for target alg failed

將非法的行註釋掉之後可以看到允許修改的內容

J:\SITP\alg\cmake-build-debug\alg.exe
&a = 72FE24
&b = 72FE20
p1 = 0x72FE24 , *p1 = 10
const int *p1 = &a;
 (*p1) = b;通過p修改a的值
p1 = 0x72FE20 , *p1 = 20
p2 = 0x72FE24 , *p2 = 10
int const *p2 = &a;
 (*p2) = b;通過p修改a的值
p2 = 0x72FE20 , *p2 = 20
p3 = 0x72FE24 , *p3 = 10
int *const p3 = &a;
 p3 = &b;修改p的指向
p3 = 0x72FE24 , *p3 = 20
p4 = 0x72FE24 , *p4 = 20
const int *const p4 = &a;
 二者都不能修改
p4 = 0x72FE24 , *p4 = 20
p5 = 0x72FE24 , *p5 = 20
int const *const p5 = &a;
 二者都不能修改
p5 = 0x72FE24 , *p5 = 20

Process finished with exit code 0

特別沒前途的C++ - explicit和volatile/const的內容