12345678910
#include <iostream> using namespace std; enum {A = 'a', B, C, D, E}; int main() { cout << B << endl; cout << static_cast<char>(B) << endl; return 0; }
98 b
123456789
#include <iostream> int main() { enum myEnum {SOMETHING, SOMETHING_ELSE, BLAH}; myEnum m = BLAH; std::cout << /* something involving 'm' */; // Prints "BLAH" }
123456789101112
#include <iostream> #include <string> #include <vector> int main() { enum myEnum {SOMETHING, SOMETHING_ELSE, BLAH}; std::vector<std::string> enumStrings = {"SOMETHING", "SOMETHING_ELSE", "BLAH"}; myEnum m = BLAH; std::cout << enumStrings[m]; // Prints "BLAH" }
1234567891011121314151617181920212223242526
#include <iostream> #include <string> #include <stdexcept> enum class colour { WHITE, GREY, BLACK } ; std::string to_string( colour clr ) { switch(clr) { case colour::WHITE: return "colour::WHITE" ; case colour::GREY: return "colour::GREY" ; case colour::BLACK: return "colour::BLACK" ; } throw std::domain_error( "uninitialized colour" ) ; } std::ostream& operator<< ( std::ostream& stm, colour clr ) { return stm << to_string(clr) ; } int main() { const auto clr_cloud = colour::GREY ; std::cout << clr_cloud << '\n' ; }