lectures.alex.balgavy.eu

Lecture notes from university.
git clone git://git.alex.balgavy.eu/lectures.alex.balgavy.eu.git
Log | Files | Refs | Submodules

Enumerations.md (594B)


      1 +++
      2 title = 'Enumerations'
      3 +++
      4 # Enumerations
      5 enum (enumeration) — simple user-defined type, specifying its set of values as symbolic constants
      6 
      7 by default, values of constants start with 0 and increment by 1 with each constant
      8 
      9 you can give a constant a value from which the rest is incremented
     10 
     11 e.g.:
     12 
     13 ```cpp
     14 enum class Month {
     15   jan=1, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec
     16 };
     17 ```
     18 
     19 conversions:
     20 
     21 ```cpp
     22 Mont int_to_month(int x) {
     23   if (x < int(Month::jan) || int(Month::dec) < x)
     24   error(“bad month”);
     25   return Month(x);
     26 }
     27 
     28 int m = 12;
     29 Month mm = int_to_month(m);
     30 ```