Expanding the Image API

Enum Class vs Regular Enum

Why do we use enum class for ScalingMode instead of a regular enum?

Abstract art representing computer programming

Using enum class for ScalingMode instead of a regular enum is a deliberate choice that offers several advantages. Let's explore why we might prefer enum class in this context:

Type Safety

The primary benefit of enum class is improved type safety. Unlike regular enums, enum classes are strongly typed and don't implicitly convert to integers. This prevents accidental comparisons or assignments with other types.

Consider this example with a regular enum:

enum ScalingMode { None, Fill, Contain };

void SetScaling(ScalingMode Mode) {
  // This compiles, but it's probably not
  // what we meant
  if (Mode == 1) {
    // ...
  }
}

int main() {
  // This also compiles, but it's not type-safe
  SetScaling(2);
}

Now, let's see how enum class improves this:

enum class ScalingMode { None, Fill, Contain };

void SetScaling(ScalingMode Mode) {
  if (Mode == 1) {  
    // ...
  }
}

int main() {
  SetScaling(2);  
}
error: no viable conversion from 'int' to 'ScalingMode'
error: invalid operands to binary expression ('ScalingMode' and 'int')

Scope

Enum classes introduce a new scope, which helps prevent name clashes. With regular enums, the enum values are in the same scope as the enum, which can lead to naming conflicts:

enum Color { Red, Green, Blue };
enum TrafficLight { Red, Yellow, Green };
error: redefinition of enumerator 'Red'
error: redefinition of enumerator 'Green'

With enum classes, this isn't an issue:

enum class Color { Red, Green, Blue };
// No problem!
enum class TrafficLight { Red, Yellow, Green };

void SetColor(Color C) {
  // We must use Color::Red, not just Red
  if (C == Color::Red) {
    // ...
  }
}

Explicit Conversions

While enum classes don't implicitly convert to integers, you can still perform explicit conversions when needed:

enum class ScalingMode { None, Fill, Contain };

void PrintScalingMode(ScalingMode Mode) {
  switch (Mode) {
    case ScalingMode::None:
      std::cout << "None ("
        << static_cast<int>(Mode) << ")\n";
      break;
    case ScalingMode::Fill:
      std::cout << "Fill ("
        << static_cast<int>(Mode) << ")\n";
      break;
    case ScalingMode::Contain:
      std::cout << "Contain ("
        << static_cast<int>(Mode) << ")\n";
      break;
  }
}

int main() {
  PrintScalingMode(ScalingMode::Fill);
}
Fill (1)

By using enum class for ScalingMode, we're creating a more robust and self-documenting API for our Image class. It helps prevent errors and makes the code's intent clearer, which is particularly valuable in larger codebases or when working in teams.

Answers to questions are automatically generated and may not have been reviewed.

sdl2-promo.jpg
Part of the course:

Game Dev with SDL2

Learn C++ and SDL development by creating hands on, practical projects inspired by classic retro games

Free, unlimited access

This course includes:

  • 51 Lessons
  • 100+ Code Samples
  • 91% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved