summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAdam Bard <github@adambard.com>2015-10-18 00:02:12 +0800
committerAdam Bard <github@adambard.com>2015-10-18 00:02:12 +0800
commit0ffceab0656e3c36b79b487399660f157c6a2b90 (patch)
tree6837b00f00be3cefb1120e67f2a5f55ff137d387
parente854849a305babf3aa45f2bbbe014699685c51df (diff)
parentca435fbb0dd09cdc9c70fe945a891ae3e6c19ab2 (diff)
Merge pull request #1589 from Alters/master
[C++/en] Added in Enum information
-rw-r--r--c++.html.markdown64
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
//////////////////////////////////////////