diff options
| author | Per Lilja <perlilja@gmail.com> | 2015-10-21 13:28:36 +0200 | 
|---|---|---|
| committer | Per Lilja <perlilja@gmail.com> | 2015-10-21 13:28:36 +0200 | 
| commit | 3f8b067a0cace44bb43bdd08561b0efc747fb26c (patch) | |
| tree | 1e17c3f1968f7b2f97ea9f84ca0098224ff60786 /c++.html.markdown | |
| parent | d1a822f96c88855b2cbb649a4ea7b452e4104164 (diff) | |
| parent | ef6973b13f50063462d28a96ac57e93aed40844c (diff) | |
Merge pull request #1 from adambard/master
Update fork
Diffstat (limited to 'c++.html.markdown')
| -rw-r--r-- | c++.html.markdown | 68 | 
1 files changed, 67 insertions, 1 deletions
| diff --git a/c++.html.markdown b/c++.html.markdown index 8ee964ca..d03092e5 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -264,7 +264,7 @@ string retVal = tempObjectFun();  // What happens in the second line is actually:  //   - a string object is returned from tempObjectFun -//   - a new string is constructed with the returned object as arugment to the +//   - a new string is constructed with the returned object as argument to the  //     constructor  //   - the returned object is destroyed  // The returned object is called a temporary object. Temporary objects are @@ -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  ////////////////////////////////////////// @@ -404,6 +468,8 @@ int main() {  // Inheritance:  // This class inherits everything public and protected from the Dog class +// as well as private but may not directly access private members/methods  +// without a public or protected method for doing so  class OwnedDog : public Dog {      void setOwner(const std::string& dogsOwner); | 
