From 597886ae5f4cdf8e3be60e942f4b3bbe7845fd42 Mon Sep 17 00:00:00 2001 From: light-city <455954986@qq.com> Date: Wed, 7 Aug 2019 18:13:57 +0800 Subject: [PATCH] update dir --- ::/:: | Bin 0 -> 9232 bytes ::/::.cpp | 20 ++++++++ ::/README.md | 14 +++++ README.md | 2 + enum/README.md | 105 ++++++++++++++++++++++++++++++++++++++ enum/classic_practice | Bin 0 -> 9328 bytes enum/classic_practice.cpp | 87 +++++++++++++++++++++++++++++++ enum/tradition_color.cpp | 10 ++++ 8 files changed, 238 insertions(+) create mode 100755 ::/:: create mode 100644 ::/::.cpp create mode 100644 ::/README.md create mode 100644 enum/README.md create mode 100755 enum/classic_practice create mode 100644 enum/classic_practice.cpp create mode 100644 enum/tradition_color.cpp diff --git a/::/:: b/::/:: new file mode 100755 index 0000000000000000000000000000000000000000..da06a11988e192b63587043c31ee1257939e500a GIT binary patch literal 9232 zcmeHNeQaCR6~DHdkAC1drDY{W)MM>dKrwckWT9waCw6OBX__St>sZql$957k$Byjh zl2mjn3ajv-G-FK4q;1*+n>q<;Oq(XONexw}rbwFv(%_%*M~4PVpo}6|l})tX@7#Bf z{ruuEw2%Kd(R=5f-#zEt@B8jO&No9nd+j!xV6uyQ1xfA(zl4mp5Pi2uQDL28t?-L& z;ucX4qz;c?<`6eh%$JpG=8a0v0lo%Lr`-TL?S7eH&c!k38WN}SD&$v5gFm;<5SgPT z5+FOO>+%g!1Rs_O=Hsd!k5R=Wx#gQ+vfJX6BKWXOFu$ZK#+=(nZKEfu{6_75NF@7- ziJ*K}IO!7gy{RWC-L_VW;C`84zM9>aVMq1+q~ubH{i?jx`lV6TpSfzTxGS9+>1ewv zooG#`GKHzusm_kpjy6v|>)9pSP5w!DU+)Q1ZA5;Sx5RQES52M`{>UojKE=3Nrw}Ej@TU4)s`pEoUvDDOELy~{Ygj@q z)K3z|F|Q}$ciu^3*C8}*Y$BV{@_H<%YnssZAJzJkW2wBJ%yp+@`Ft`jv`F-TmPqC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 zO79e-wRr#7@b5tkR3eqp3i%{Dh*WDwgSA9Vj~QWBv>P#G9vO=MQmEEasFuiQwQ&qL z1(}T)E%lo*L9~&4o_(pV=5Vsb&3_zQw)ja>^@WTN3r~J}LXVAr>p6pub1IcV+L!-CTf#-WB{ViT!2 z>X6mRJrL>w54v7>uro|xwacC8zkAVn2wNx46bA7*pMNLhd!ovgx|}BdIXrYXV|%_2 z@b7SLpyP)qsqQZIpq8L zMP+|R;Utm`m(TW30;7JV`VsNHYf=H+ek0eYE2oJU@tE!Tem1M@Pmq#{I3B|MY2=yh zT@8Mz?V?Eu;`$oWO8JM#Fx&Ha(XH(5+_B05m&^X1vDkAwXS)h$a9o4+)wE(!{9M`d z*_O|BUS(+2XPNmgl|5IJfA{9!!-M6Lj0D>;Ja4h*`{pu|0-;n@UT7KZyU)e$f|FZ{{kJVBHQyl()9`L--0boHMZyY4*&ncCDnd4 z9n1CMcFkGJ=X>=sH40>{FU!o|gO0iW<@>iPpig?Kyx5NUvc;b7Q+>*w>&YJ2j^*n} z5XSB2bMvq=bgTBWzMB(@)9?1A;zZI=&T|yYO)PdH +using namespace std; + +int count=0; // 全局(::)的count + +class A { +public: + static int count; // 类A的count (A::count) + +}; +// 静态变量必须在此处定义 +int A::count; +int main() { + ::count=1; // 设置全局的count为1 + A::count=5; // 设置类A的count为2 + cout< +using namespace std; + +enum Color {RED,BLUE}; +enum Feeling {EXCITED,BLUE}; + +int main() +{ + return 0; +} +``` +- 会隐式转换为int +- 用来表征枚举变量的实际类型不能明确指定,从而无法支持枚举类型的前向声明。 + +具体实现见:[tradition_color.cpp](tradition_color.cpp) + +## 经典做法 + +解决作用域不受限带来的命名冲突问题的一个简单方法是,给枚举变量命名时加前缀,如上面例子改成 COLOR_BLUE 以及 FEELING_BLUE。 + +一般说来,为了一致性我们会把所有常量统一加上前缀。但是这样定义枚举变量的代码就显得累赘。C 程序中可能不得不这样做。不过 C++ 程序员恐怕都不喜欢这种方法。替代方案是命名空间: +```c++ +namespace Color +{ + enum Type + { + RED=15, + YELLOW, + BLUE + }; +}; +``` + +这样之后就可以用 `Color::Type c = Color::RED;` 来定义新的枚举变量了。如果 `using namespace Color` 后,前缀还可以省去,使得代码简化。不过,因为命名空间是可以随后被扩充内容的,所以它提供的作用域封闭性不高。在大项目中,还是有可能不同人给不同的东西起同样的枚举类型名。 + +更“有效”的办法是用一个类或结构体来限定其作用域,例如:定义新变量的方法和上面命名空间的相同。不过这样就不用担心类在别处被修改内容。这里用结构体而非类,一是因为本身希望这些常量可以公开访问,二是因为它只包含数据没有成员函数。 + +```c++ +struct Color1 +{ + enum Type + { + RED=102, + YELLOW, + BLUE + }; +}; +``` + +具体实现见:[tradition_color.cpp](tradition_color.cpp) + +## C++11 的枚举类 + +上面的做法解决了第一个问题,但对于后两个仍无能为力。庆幸的是,C++11 标准中引入了“枚举类”(enum class),可以较好地解决上述问题。 + +- 新的enum的作用域不在是全局的 +- 不能隐式转换成其他类型 + +```c++ +/** + * @brief C++11的枚举类 + * 下面等价于enum class Color2:int + */ +enum class Color2 +{ + RED=2, + YELLOW, + BLUE +}; +r2 c2 = Color2::RED; +cout << static_cast(c2) << endl; //必须转! +``` + +- 可以指定用特定的类型来存储enum + +```c++ +enum class Color3:char; // 前向声明 + +// 定义 +enum class Color3:char +{ + RED='r', + BLUE +}; +char c3 = static_cast(Color3::RED); +``` + +具体实现见:[tradition_color.cpp](tradition_color.cpp) \ No newline at end of file diff --git a/enum/classic_practice b/enum/classic_practice new file mode 100755 index 0000000000000000000000000000000000000000..f9f67b7aad21345bab72ac320341410e3d763f7e GIT binary patch literal 9328 zcmeHNeQaCR6~DHVCTWY~LJMgN)Eg}eWid&cW+`a!oW!Zk+&0Sx%Ybm>*iPc%*p~fV z(rRp}QYx%c$Y7c>O_MT}Nlcms(}cwMqiGe?!lX^G38~tqsY5U&e2lVoD5^@cbM8CG zetxz$_Vph}@;mqZ?m6e)ckai#_axu;1-tDwn_#kwy9G*fHJXBqrw}jPs6@irMV-*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|~C-7#N>J}f#Yo}kpkT2>?-i>TV>wPSWONB!@i db$l~a?t~w|Kkr|-KFhU#ffA&@uvA*x{|B`$?6m*@ literal 0 HcmV?d00001 diff --git a/enum/classic_practice.cpp b/enum/classic_practice.cpp new file mode 100644 index 0000000..f831521 --- /dev/null +++ b/enum/classic_practice.cpp @@ -0,0 +1,87 @@ +/** + * @file classic_practice.cpp + * @brief g++ -o classic_practice classic_practice.cpp -std=c++11 + * @author 光城 + * @version v1 + * @date 2019-08-07 + */ + +#include +using namespace std; +/** + * @brief namespace解决作用域不受限 + */ +namespace Color +{ + enum Type + { + RED=15, + YELLOW, + BLUE + }; +}; + +/** + * @brief 上述如果 using namespace Color 后,前缀还可以省去,使得代码简化。 + * 不过,因为命名空间是可以随后被扩充内容的,所以它提供的作用域封闭性不高。 + * 在大项目中,还是有可能不同人给不同的东西起同样的枚举类型名。 + * 更“有效”的办法是用一个类或结构体来限定其作用域。 + * + * 定义新变量的方法和上面命名空间的相同。 + * 不过这样就不用担心类在别处被修改内容。 + * 这里用结构体而非类,一是因为本身希望这些常量可以公开访问, + * 二是因为它只包含数据没有成员函数。 + */ +struct Color1 +{ + enum Type + { + RED=102, + YELLOW, + BLUE + }; +}; + +/** + * @brief C++11的枚举类 + * 下面等价于enum class Color2:int + */ +enum class Color2 +{ + RED=2, + YELLOW, + BLUE +}; + +enum class Color3:char; // 前向声明 + +// 定义 +enum class Color3:char +{ + RED='r', + BLUE +}; + +int main() +{ + // 定义新的枚举变量 + Color::Type c = Color::RED; + cout<(c2) << endl; + + char c3 = static_cast(Color3::RED); + cout< +using namespace std; + +enum Color {RED,BLUE}; +enum Feeling {EXCITED,BLUE}; + +int main() +{ + return 0; +}