我注意到 C + + 不会编译以下代码:
class No_Good {
static double const d = 1.0;
};
然而,它将很高兴地允许一种变体,其中 double 被更改为 int、 unsigned 或任何整数类型:
class Happy_Times {
static unsigned const u = 1;
};
我的解决办法是把它改成:
class Now_Good {
static double d() { return 1.0; }
};
并认为编译器会聪明到在必要的地方内联,但这让我很好奇。
为什么 C + + 设计器允许我使用 int 或 unsigned 来表示静态常量,而不允许使用 double 呢?
编辑: 我在 Windows XP 上使用 Visual Studio 7.1(. net 2003)。
编辑2:
问题已经得到解答,但为了完成,我看到的错误是:
error C2864: 'd' : only const static integral data members can be initialized inside a class or struct