读书人

对于一个枚举的有关问题

发布时间: 2012-10-10 13:58:11 作者: rapoo

对于一个枚举的问题
enum {two}a;
a=two;
int main()
{

}
为什么错误,但是如果把a=two放函数里面就能通过!

[解决办法]
An enumerated type is a user-defined type consisting of a set of named constants called enumerators. By default, the first enumerator has a value of 0, and each successive enumerator is one larger than the value of the previous one, unless you explicitly specify a value for a particular enumerator. Enumerators needn’t have unique values. The name of each enumerator is treated as a constant and must be unique within the scope where the enum is defined. An enumerator can be promoted to an integer value. However, converting an integer to an enumerator requires an explicit cast, and the results are not defined.
-------------------------------
把a=two放函数里面----这是显式的指定值

C/C++ code
// Example of the enum keywordenum Days               // Declare enum type Days{   saturday,            // saturday = 0 by default   sunday = 0,          // sunday = 0 as well   monday,              // monday = 1   tuesday,             // tuesday = 2   wednesday,           // etc.   thursday,   friday} today;                // Variable today has type Daysint  tuesday;           // Error, redefinition of tuesdayenum Days yesterday;    // Legal in C and C++Days tomorrow;          // Legal in C++ onlyyesterday = monday;int i = tuesday;        // Legal; i = 2yesterday = 0;          // Error; no conversionyesterday = (Days)0;    // Legal, but results undefined 

读书人网 >C++

热点推荐