Benefits of enum

Pages: 12
Named values. Names for values.

1
2
3
4
5
class car {
  static const int foo = 5;
};

5 == car::foo

The foo is a variable, but it is almost global variable. Foo exists even when you have no cars. No instance of car uses memory for foo. The class scope limits who can access foo.

1
2
3
class car {
  enum bar { foo = 5 };
};

No variable, just a literal constant value. However, now there is a type too: bar.

std::cout << "Hello";
"Hello" is not a variable. It is a literal constant value.
Topic archived. No new replies allowed.
Pages: 12