enum [class|struct] identifier [: type] { enumerator[=constexpr], ...}
enum class BodyPartT {BODY, HEAD, LEG, EYE, ANTENNA, TAIL, NONE};
EYE
BodyPartT currentPart{BodyPartT::HEAD}; currentPart = BodyPartT::EYE;
currentPart = static_cast<BodyPartT>(3); // this is ok, but be careful. int i = static_cast<int>(currentPart); // this is ok.
const BodyPartT FIRST_BODY_PART{BodyPartT::BODY};
const size_t BODY_PART_COUNT {static_cast<size_t>(BodyPartT::NONE)};
NONE
at the end so loops work.
BodyPartT i{FIRST_BODY_PART}; for( i = FIRST_BODY_PART; i != BodyPartT::NONE; ++i) { }
return_type operator operator_symbol( params) {
}
// this is code for ++i // the parameter needs to be pass by reference, why? BodyPartT operator ++(BodyPartT & b) { if (b < BodyPartT::NONE) { b = static_cast(static_cast (b)+1); } return b; } // this is code for i++ BodyPartT operator ++(BodyPartT & b, int) { BodyPartT oldValue{b}; if (b < BodyPartT::NONE) { b = static_cast (static_cast (b)+1); } return oldValue; }
const string BODY_PART_NAMES[]{"body", "head", "leg", "eye", "antenna", "tail", "none"};
string BodyPartTToString(BodyPartT b) { string name{"none"}; if (b >= BodyPartT::BODY and b <= BodyPartT::NONE) { name = BODY_PART_NAME[static_cast(b)]; } return name; }
ostream & operator << (ostream & s; const BodyPartT & b) { s << BodyPartTToString(b); return s }