Skip to content

Latest commit

 

History

History
139 lines (125 loc) · 3.49 KB

enums.md

File metadata and controls

139 lines (125 loc) · 3.49 KB
marp math theme footer
true
katex
custom-theme

Enumeration types

Today:

  • What are enumeration types
  • How to use them
  • How not to use them

📺 Watch the related YouTube video!


Special symbols used in slides

  • 🎨 - Style recommendation
  • 🎓 - Software design recommendation
  • 😱 - Not a good practice! Avoid in real life!
  • ✅ - Good practice!
  • ❌ - Whatever is marked with this is wrong
  • 🚨 - Alert! Important information!
  • 💡 - Hint or a useful exercise
  • 🔼1️⃣7️⃣ - Holds for this version of C++(here, 17) and above
  • 🔽1️⃣1️⃣ - Holds for versions until this one C++(here, 11)

Style (🎨) and software design (🎓) recommendations mostly come from Google Style Sheet and the CppCoreGuidelines


🔼1️⃣1️⃣ Enumeration classes

  • Store an enumeration of options
    enum class FoodOptions : BaseType { kPizza, kPasta, kSushi };
    enum class ChoicesEnum { kYay, kNay };
  • By default BaseType is int, can be any other integral type
  • Enums can have any number of options
  • The options are fixed upon enum definition
  • Options are assigned consequent numbers
  • Mostly used to pick a path in a switch statement
  • Use values as: FoodOptions::kPizza, ChoicesEnum::kYay, ...
  • 🎨 Name enum types in CamelCase
  • 🎨 Name enum values as constants: kSomeConstant

Example

#include <iostream>
#include <string>

enum class OutputChannel { kStdOut, kStdErr };

// Note that output_channel is passed by value!
void Print(OutputChannel output_channel, const std::string& msg) {
  switch (output_channel) {
    case OutputChannel::kStdOut:
      std::cout << msg << std::endl;
      break;
    case OutputChannel::kStdErr:
      std::cerr << msg << std::endl;
      break;
  }
}
int main() {
  Print(OutputChannel::kStdOut, "hello");
  Print(OutputChannel::kStdErr, "world");
  return 0;
}

Explicit values

  • By default enum values start from 0
  • We can specify custom values if needed
    enum class EnumType {
      kOption1 = 10,      // Decimal
      kOption2 = 0x2,     // Hexadecimal
      kOption3 = 0b101010 // Binary
    };
  • 💡 Most of the times enums are used with default values

😱 The olden days enum

  • Before C++11 you would define an enum without class:
    enum MyEnum{kValue1, kValue2};  // 😱 Don't do this in C++11
  • Old enums can be implicitly converted to int:
  • These enums are not scoped like the class ones
    enum OldEnum{kValue1, kValue2};  // Really, don't use it.
    enum class NewEnum{kValue1, kValue2};
    int main() {
      const OldEnum old_value = kValue1; // Note no OldEnum:: prefix
      const int some_value = kValue1;
      const NewEnum new_value = NewEnum::kValue1;
      return 0;
    }
  • ❓😱 Do you see an issue with this?
    enum EnumDecision{kRight, kWrong};
    enum EnumSide{kLeft, kRight};

    Try to compile this!

bg