下列例子用到了C++的關鍵詞 and, not,雖然這種作法比較少用,但它可讀性比||
和!
會好不少。
bool isOk = false; int i = 1; if (i < 2 and i > 0) isOk = true; if (not isOk) { printf("OK"); }
能夠在iso646.h
頭文件中找到:微信
#define and && #define and_eq &= #define bitand & #define bitor | #define compl ~ #define not ! #define not_eq != #define or || #define or_eq |= #define xor ^ #define xor_eq ^=
&
或|
致使邏輯錯誤。if (x && y) { ... } /* 遺留了&可能致使程序意想不到的缺陷 */ if (x & y) { ... } /* 而遺留了and則直接致使編譯錯誤,從而發現錯誤 */ if (x and y) { ... }
& | ^
等字符,須要使用關鍵詞來標識。/Za
以支持替代關鍵字。更多精彩文章請關注微信公衆號:「Qt君」code