运算符重载枚举 [英] Operators overloading for enums
本文介绍了运算符重载枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以定义枚举的运算符?例如我有我的班级的枚举月份,我希望能够编写++ my_month。
谢谢
PS
为了避免溢出我做了这样的事情:
Is it possible to define operators for enums? For example I have enum Month in my class and I would like to be able to write ++my_month.
Thanks
P.S.
In order to avoid overflowing I did something like this:
void Date::add_month()
{
switch(my_month_)
{
case Dec:
my_month_ = Jan;
add_year();
break;
default:
++my_month_;
break;
}
}
推荐答案
是它是。操作符重载可以对所有用户定义的类型进行。包括枚举。
Yes it is. Operator overloading can be done for all user defined types. That includes enums.
这篇关于运算符重载枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文