枚举的范围 [英] the range of enum
问题描述
根据<< C ++编程语言>>:
enum flag {x = 1,y = 2,z = 4,e = 8}; //范围0:15
...
标志f4 =标志(99); //未定义:99不在标志范围内
但以下代码的结果为99.
// --------------------------------------------------
#include" iostream"
int main(int argc,char * argv [])
{
enum flag {x = 1,y = 2,z = 4,e = 8};
flag f1 = flag(99);
std :: cout<< f1<< std :: endl;
返回0;
}
// --------------------------------------- -------------
编译器是否将枚举类型视为整数类型?那么enum范围的含义是什么?
Vane写道:
< blockquote class =post_quotes>根据<< C ++编程语言>>:
enum flag {x = 1,y = 2,z = 4,e = 8 }; //范围0:15
...
标志f4 =标志(99); // undefined:99不在flag的范围内
但以下代码的结果为99。
"未定义"意思是结果可以是任何东西。这包括
值99.
这是一个合适的答案,因为有些编译器已经实现。
但是,编译器应该产生错误:
flag f4 = flag(99);
此时,说undefined意味着你做的任何事情都没关系,不是好的。
似乎是合理的。
问候,
博士Z. < br $> b $ b首席科学家
zo****@ZHMicro.com
http://www.zhmicro.com
http://distributed-software.blogspot.com
Zorro写道:
这是一个合适的答案,因为有些编译器已经实现。
然而,编译器应该产生错误:
flag f4 = flag(99);
不,它不应该。这是一个强制转换,并将一个整数转换为枚举是允许的
。如果是这样会有所不同:
flag f4 = 99;
According to <<C++ Programming Language>>:
enum flag { x = 1, y = 2, z = 4, e = 8 }; //range 0:15
...
flag f4 = flag(99); // undefined : 99 is not within the range of flag
But the following codes has result 99.
//--------------------------------------------------
#include "iostream"
int main(int argc, char* argv[])
{
enum flag{ x = 1, y = 2, z = 4, e = 8 };
flag f1 = flag( 99 );
std::cout<<f1<<std::endl;
return 0;
}
//----------------------------------------------------
Whether the compiler treats enum type as integral type? Then what''s the
meaning of the range of enum?
Vane wrote:
According to <<C++ Programming Language>>:enum flag { x = 1, y = 2, z = 4, e = 8 }; //range 0:15
...
flag f4 = flag(99); // undefined : 99 is not within the range of flag
But the following codes has result 99.
"Undefined" means "the result can be anything". And this includes a
value of 99.
This is an appropriate answer, as some compilers are implemented.
However, the compiler should have produced error at:
flag f4 = flag(99);
At this point, saying undefined means anything you do is OK, does not
seem justifiable.
Regards,
Dr. Z.
Chief Scientist
zo****@ZHMicro.com
http://www.zhmicro.com
http://distributed-software.blogspot.com
Zorro wrote:
This is an appropriate answer, as some compilers are implemented.
However, the compiler should have produced error at:
flag f4 = flag(99);
No, it shouldn''t. This is a cast, and casting an integer into an enum is
allowed. It would be different if it were:
flag f4 = 99;
这篇关于枚举的范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!