枚举的奇淫技巧!
枚举特性中: 枚举必须在编译时建立其所有值,并且其值可用于常量表达式。
1 在旧的编译器中以下代码不能编译通过
static const int length = 100;
int i[length];
2 利用枚举间接实现
struct Body {
enum { length = 100 };
int i[length];
};
3 枚举骇客的由来
static const
不被编译器作常量支持,这一点令人费解。- 在编译器中不能被支持,利用枚举的特性来支持;
- 枚举特性中:
枚举必须在编译时建立其所有值,并且其值可用于常量表达式。
4 例子
#include <iostream>
using namespace std;
struct Body {
enum { length = 100 };
int i[length];
};
int main()
{
cout << "sizeof(Body) = " << sizeof(Body)
<< "sizeof(int[100]) = " << sizeof(int[100]) << endl;
return 0;
}
5 最后
- 猜猜结果输出是什么?