枚举的奇淫技巧! 枚举特性中: 枚举必须在编译时建立其所有值,并且其值可用于常量表达式。
static const int length = 100;int i[length];
struct Body { enum { length = 100 }; int i[length];};
staticconst
不被编译器作常量支持,这一点令人费解。#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;
}