diff options
author | Jacob Ward <jacobward1898@gmail.com> | 2015-10-26 23:03:37 -0600 |
---|---|---|
committer | Jacob Ward <jacobward1898@gmail.com> | 2015-10-26 23:03:37 -0600 |
commit | a0eb996415cc86cb72c44e793ebfacc3ec2d7b17 (patch) | |
tree | 516c83684b752f374758b9abd3b89be723478fd3 /c++.html.markdown | |
parent | 66bc42e31bf62a1592f9b763e12c0b963b3e7d3d (diff) | |
parent | 44ca091c73afe13ec8760021cfed1d77afc5e4a5 (diff) |
Merge remote-tracking branch 'adambard/master'
Diffstat (limited to 'c++.html.markdown')
-rw-r--r-- | c++.html.markdown | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/c++.html.markdown b/c++.html.markdown index 2bee51dc..d03092e5 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -310,6 +310,70 @@ basic_string(basic_string&& other); // constructor that "salvages" parts of that temporary string. You will see this // concept referred to as "move semantics". +///////////////////// +// Enums +///////////////////// + +// Enums are a way to assign a value to a constant most commonly used for +// easier visualization and reading of code +enum ECarTypes +{ + Sedan, + Hatchback, + SUV, + Wagon +}; + +ECarTypes GetPreferredCarType() +{ + return ECarTypes::Hatchback; +} + +// As of C++11 there is an easy way to assign a type to the enum which can be +// useful in serialization of data and converting enums back-and-forth between +// the desired type and their respective constants +enum ECarTypes : uint8_t +{ + Sedan, // 0 + Hatchback, // 1 + SUV = 254, // 254 + Hybrid // 255 +}; + +void WriteByteToFile(uint8_t InputValue) +{ + // Serialize the InputValue to a file +} + +void WritePreferredCarTypeToFile(ECarTypes InputCarType) +{ + // The enum is implicitly converted to a uint8_t due to its declared enum type + WriteByteToFile(InputCarType); +} + +// On the other hand you may not want enums to be accidentally cast to an integer +// type or to other enums so it is instead possible to create an enum class which +// won't be implicitly converted +enum class ECarTypes : uint8_t +{ + Sedan, // 0 + Hatchback, // 1 + SUV = 254, // 254 + Hybrid // 255 +}; + +void WriteByteToFile(uint8_t InputValue) +{ + // Serialize the InputValue to a file +} + +void WritePreferredCarTypeToFile(ECarTypes InputCarType) +{ + // Won't compile even though ECarTypes is a uint8_t due to the enum + // being declared as an "enum class"! + WriteByteToFile(InputCarType); +} + ////////////////////////////////////////// // Classes and object-oriented programming ////////////////////////////////////////// |