谁教我枚举类型enum的用法?

kzh 2003-01-06 01:34:02
请教,在VC6中,如何使用enum类型?

具体的、抽象的;
考据式、印象式;
详细的、零星的
-----解答都十分欢迎! 谢谢!
...全文
1854 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
lingrain 2010-06-09
  • 打赏
  • 举报
回复
公元前的?我还想回复呢
kzh 2003-01-07
  • 打赏
  • 举报
回复
Up
wangweicai 2003-01-06
  • 打赏
  • 举报
回复
随便拿本关于C/C++的书,都有描述的。
xiaoniu111 2003-01-06
  • 打赏
  • 举报
回复
我不说只看
snollow 2003-01-06
  • 打赏
  • 举报
回复
中英都说拉
我也没有什么好说的拉,其实枚举就是用户自定义的数据类型,和int一样只不过数据是自己规定的而已。
Firstbyte 2003-01-06
  • 打赏
  • 举报
回复
定义数组维数,引用的初始化???
kzh 2003-01-06
  • 打赏
  • 举报
回复
补充问题:
具体在VC6中,又有哪些特别的规定和扩展?
qing_li73 2003-01-06
  • 打赏
  • 举报
回复
enum
enum [tag] {enum-list} [declarator]; // for definition of enumerated type

enum tag declarator; // for declaration of variable of type tag

The enum keyword specifies an enumerated type.

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.

In C, you can use the enum keyword and the tag to declare variables of the enumerated type. In C++, you can use the tag alone.

In C++, enumerators defined within a class are accessible only to member functions of that class unless qualified with the class name (for example, class_name::enumerator). You can use the same syntax for explicit access to the type name (class_name::tag).


Example

// Example of the enum keyword
enum 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 Days

int tuesday; // Error, redefinition of tuesday

enum Days yesterday; // Legal in C and C++
Days tomorrow; // Legal in C++ only

yesterday = monday;

int i = tuesday; // Legal; i = 2
yesterday = 0; // Error; no conversion
yesterday = (Days)0; // Legal, but results undefined
setu1 2003-01-06
  • 打赏
  • 举报
回复
枚举常量是枚举类型中的值,即枚举值。枚举类型是一种用户定义的类型,只有用户在程序中定义它后才能被使用。用户通常利用枚举类型定义程序中需要使用的一组相关的符号常量。枚举类型的定义格式为:

enum <枚举类型名> {<枚举表>};

它是一条枚举类型定义语句,该语句以enum保留字开始,接着为枚举类型名,它是用户命名的一个标识符,以后就直接使用它表示该类型,枚举类型名后为该类型的定义体,它是由一对花括号和其中的枚举表所组成,枚举表为一组用逗号分开的由用户命名的符号常量,每个符号常量又称为枚举常量或枚举值。如:

(1) enum color{red, yellow, blue};

(2) enum day{Sun, Mon, Tues, Wed, Thur, Fri, Sat};

第一条语句定义了一个枚举类型color,用来表示颜色,它包含三个枚举值red,yellow和blue,分别代表红色、黄色和兰色。

第二条语句定义了一个枚举类型day,用来表示日期,它包含7个枚举值,分别表示星期日、星期一至星期六。

一种枚举类型被定义后,可以象整型等预定义类型一样使用在允许出现数据类型的任何地方。如可以利用它定义变量。

(1) enum color c1, c2,c3;

(2) enum day today, workday;

(3) c1=red;

(4) workday=Wed;

第一条语句开始的保留字enum和类型标识符colou表示上述定义的枚举类型color,其中enum可以省略不写,后面的三个标识符c1,c2和c3表示该类型的三个变量,每一个变量用来表示该枚举表中列出的任一个值。

第二条语句开始的两个成分(成分之间的空格除外)表示上述定义的枚举类型day,同样enum可以省略不写,后面的两个标识符today和workday表示该类型的两个变量,每一个变量用来表示该枚举表中列出的七个值中的任一个值。

第三条语句把枚举值red赋给变量c1,第四条语句把枚举值Wed赋给变量workday。

在一个枚举类型的枚举表中列出的每一个枚举常量都对应着一个整数值,该整数值可以由系统自动确认,也可以由用户指定。若用户在枚举表中一个枚举常量后加上赋值号和一个整型常量,则就表示枚举常量被赋予了这个整型常量的值。如:

enum day{Sun=7, Mon=0, Tues, Wed, Thur, Fri, Sat};

用户指定了Sun的值为7,Mon的值为0。

若用户没有给一个枚举常量赋初值,则系统给它赋予的值是它前一项枚举常量的值加1,若它本身就是首项,则被自动赋予整数0。如对于上述定义的color类型,red,yellow和blue的值分别为0,1和2;对于刚被修改定义的day类型,各枚举常量的值依次为7,0,1,2,3,4,5,6。

由于各枚举常量的值是一个整数,所以可把它同一般整数一样看待,参与整数的各种运算。又由于它本身是一个符号常量,所以当作为输出数据项时,输出的是它的整数值,而不是它的标识符,这一点同输出其他类型的符号常量是一致的。

16,550

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧