How to Get enum item name from its value

后端 未结 10 979
天涯浪人
天涯浪人 2020-12-08 09:56

I declared a enum type as this,

enum WeekEnum
{
Mon = 0;
Tue = 1;
Wed = 2;
Thu = 3;
Fri = 4;
Sat = 5;
Sun = 6;
};

How can I get the item na

10条回答
  •  庸人自扰
    2020-12-08 10:29

    You can define an operator that performs the output.

    std::ostream& operator<<(std::ostream& lhs, WeekEnum e) {
        switch(e) {
        case Monday: lhs << "Monday"; break;
        .. etc
        }
        return lhs;
    }
    

提交回复
热议问题