Modern C++ › C++11
enum class, override and final
Scoped enums and safer inheritance.
Since C++11 beginner #enum-class#override#final#default#delete#type-safety
Explanation
Scoped enumerations (enum class) fix three problems with C++98 enum:
- the enumerators live in the enum’s own scope (
Color::Red), so they do not leak into the surrounding scope and clash; - they do not convert implicitly to
int(usestatic_cast); - you can choose the underlying type (
enum class Level : std::uint8_t) and forward-declare them.
override after a virtual function declaration tells the compiler “this must override a base function”. If the signature does not match (missing const, different parameter), it is a compile error instead of a silently new function.
final on a virtual function forbids further overriding; on a class it forbids inheriting.
Related C++11 member-control keywords: = default (compiler-generated version), = delete (forbid it).
Example
#include <iostream>
enum class Color : unsigned char { Red, Green, Blue };
enum class Status { Ok, Error }; // Red and Ok don't collide
const char* name(Color c) {
switch (c) {
case Color::Red: return "red";
case Color::Green: return "green";
case Color::Blue: return "blue";
}
return "?";
}
class Shape {
public:
virtual ~Shape() = default;
virtual double area() const = 0;
virtual void draw() const { std::cout << "shape\n"; }
};
class Circle final : public Shape { // no one may derive from Circle
double r_;
public:
explicit Circle(double r) : r_(r) {}
double area() const override { return 3.14159 * r_ * r_; }
// void draw() override; // fine
// double area() override; // error: no matching base function
};
class NonCopyable {
public:
NonCopyable() = default;
NonCopyable(const NonCopyable&) = delete;
NonCopyable& operator=(const NonCopyable&) = delete;
};
int main() {
Color c = Color::Green;
std::cout << name(c) << " " << static_cast<int>(c) << "\n"; // green 1
// int x = c; // error: no implicit conversion
// if (c == Status::Ok) ... // error: different enum types
Circle circle(2.0);
const Shape& s = circle;
std::cout << s.area() << "\n";
NonCopyable a;
// NonCopyable b = a; // error: deleted
(void)a;
return 0;
}
Common pitfalls
- Old and new enum styles mix badly: stick to
enum classfor new code. - Using an enum class as an array index or bit mask needs
static_cast(and operator overloads for bit flags). overrideis not a reserved word: it is only special after a declarator, so older code using it as a name still compiles.- Marking everything
finalblocks useful extension and mocking in tests; use it deliberately. finalon a class does not prevent composition of that class, only inheritance.
Legacy vs modern
// C++98 // C++11
enum Color { Red, Green }; enum class Color { Red, Green };
virtual void f() const; void f() const override;
private: Foo(const Foo&); Foo(const Foo&) = delete;
C++23 adds std::to_underlying and std::is_scoped_enum.
Exercises
1. Why does int x = Color::Red; not compile for an enum class?
Scoped enums do not convert implicitly to integers. Write static_cast<int>(Color::Red).