⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 enumtype.html

📁 关于 C++ 的历史无须我来介绍了
💻 HTML
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

  
  <link rel="stylesheet" href="css/stdlayout.css" type="text/css">

  
  <link rel="stylesheet" href="css/print.css" type="text/css">

  
  <meta content="text/html; charset=gb2312" http-equiv="content-type">

  
  <title>列举(Enumeration)</title>
</head>


<body>

<h3><a href="http://caterpillar.onlyfun.net/GossipCN/index.html">From
Gossip@caterpillar</a></h3>

<h1><a href="CppGossip.html">C++
Gossip:&nbsp;列举(Enumeration)</a></h1>

在C++中所谓列举(Enumeration)型别,就是以关键字enum开始加上一个列举名称,并以大括号括住要群组管理的常数,例如:<br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">enum Actions{stop, sit,
stand, walk, run};</span><br>

</div>

<br>

上例中宣告一个列举型态Actions,大括号中每一个元素称为列举元(enumerator),预设上列举元从第一个开始的实际数值是0,然后依次递
增,以上例而言,stop为0、sit为1、stand为2、walk为3、run为4,您也可以自行为列举元设定数值,例如:<br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">enum Actions{stop = 1,
sit, stand, walk, run};</span><br>

</div>

<br>

上例来说,stop为1,下一个列举元如果没有设定数值的话,则自动递增1,所以sit为2、stand为3、walk为4、run为5,列举的常数值不
需独一无二,例如下例:<br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">enum Actions{stop = 1,
sit, stand = 2, walk, run};</span><br>

</div>

<br>

下一个列举元如果没有设定数值的话,则自动递增1,所以上例中stop为1、sit为2、stand为2、walk为3、run为4。<br>

<br>

宣告列举之后,您可以用它来宣告列举变数,例如:<br>

<div style="margin-left: 40px; font-family: Courier New,Courier,monospace;"><span style="font-weight: bold;">Actions action = stop;</span><br>

</div>

<br>

上例中action只接受来自Actions中规定的列举元,虽然实际上列举元对应一个数值,但您不可以这么指定数值给列举:<br>

<div style="margin-left: 40px; font-family: Courier New,Courier,monospace;"><span style="font-weight: bold;">Actions action = 1; //
error, invalid conversion from `int' to `Actions' </span><br>

</div>

<br>

在必要的时候,列举元的数值可用来与其它数值作运算,例如:<br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">Actions action = stand;</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">cout &lt;&lt;
stand + 1 &lt;&lt; endl;</span><br>

</div>

<br>

列举值的数值被取出再进行加1,所以上例中会在文字模式下显示数值3,但要知道列举不能进行++或--的动作,例如:<br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">Actions action = stand;</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">stand++; // error,
no `operator++(int)' declared for postfix `++', trying prefix operator
instead </span><br>

</div>

<br>

列举常用在函式的引数传递,与纯綷使用常数来作为操作相比,列举可以限制传入函式的引数范围。<br>

<br>

</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -