在enum中定义的量和#define、const定义的量都是常量。定义后都不能用"++"的形式自增和自减。
一:在定义下面后,Monday就是常量“1”:
enum Days
{
Sunday,Monday,tuesday,Wedneday,Tursday,Friday,Saturday
};
如果执行程序:
cout<<"\n ++Monday is : "<< ++Monday;
则编译器会提示:
no match for 'operator++' in '++Monday'
在定义下面:
#define BDefine 10
如果程序执行下面语句:
cout<<"\n ++BDefine is : "<< ++BDefine;
编译器会提示:
non-lvalue in increment
定义如下const后:
const int CConst = 20;
如果程序执行下面语句:
cout<<"\n ++CConst is : "<< ++CConst;
编译器会提示:
increment of read-only variable `CConst'
附测试程序:(源于书中34页清单3.7)
/*********************************************
*Developer: yayu; *
*My email: xieyayu@163.com *
*Development environment: Dev-C++ 4.9.9.0; *
*********************************************/
#include
#include
#define BDefine 10
const int CConst = 20;
using namespace std;
int main(int argc, char *argv[])
{
enum Days
{
Sunday,Monday,tuesday,Wedneday,Tursday,Friday,Saturday
};
Days today;
today = Monday;
int AEnum;
AEnum = Monday;
if(today == Sunday || today == Saturday)
{
cout<<"\n today is : "<< today;
cout<<"\n Yayu love the weekends! \n";
}
else
{
cout<<"\n today is : "<< today;
cout<<"\n Back to work!. \n";
}
cout<<"\n AEnum is : "<< AEnum;
cout<<"\n Monday is : "<< Monday;
cout<<"\n ++AEnum is : "<< ++AEnum;
//cout<<"\n ++Monday is : "<< ++Monday;
//cout<<"\n ++BDefine is : "<< ++BDefine;
//cout<<"\n ++CConst is : "<< ++CConst;
return 0;
}