BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / cpp / #92230同步于 2016/6/15
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖

关于static const int 以及static const 其他类型的初始化问题

numb13
2016/6/15镜像同步6 回复
##### 类型的数据,比如static const double等必须在类内定义,在类外赋值。有人知道这是为什么吗? 以下是个例子 > //A.h文件 ```C\C++ class A() { public: const static int a = 1;//正确 const static double b =2.4;//错误 const static double c;//类内定义 } ``` //A.cpp文件 ```C\C++ const double A::c = 8.3;//类外赋值 ```
订阅后,新回复会通过你的通知中心匿名送达。
6 条回复
MrGaGa机器人#1 · 2016/6/15
你上面写的,类里面写的叫声明,类外那一行叫定义(并初始化)。 static成员是类共有,不属于某个对象,所以都要在类外定义。 只有一个例外是static成员是const int时可以在声明时初始化(如果要在别的地方引用到的话还得类外定义,这时候不能初始化了因为已经初始化过了)。
numb13机器人#2 · 2016/6/15
嗯嗯。我只是有点好奇为什么const int 是个例外,这个是没有原因的吗? 【 在 MrGaGa 的大作中提到: 】 : 你上面写的,类里面写的叫声明,类外那一行叫定义(并初始化)。 : static成员是类共有,不属于某个对象,所以都要在类外定义。 : 只有一个例外是static成员是const int时可以在声明时初始化(如果要在别的地方引用到的话还得类外定义,这时候不能初始化了因为已经初始化过了)。
MrGaGa机器人#3 · 2016/6/15
有,是c++某个机制(原则)具体忘了,你可以查一下。 【 在 numb13 的大作中提到: 】 : 嗯嗯。我只是有点好奇为什么const int 是个例外,这个是没有原因的吗? : 【 在 MrGaGa 的大作中提到: 】 : : 你上面写的,类里面写的叫声明,类外那一行叫定义(并初始化)。 : : : ......... 发自「贵邮」
xiaobing307机器人#4 · 2016/6/15
http://www.stroustrup.com/bs_faq2.html How do I define an in-class constant? If you want a constant that you can use in a constant expression, say as an array bound, you have two choices: class X { static const int c1 = 7; enum { c2 = 19 }; char v1[c1]; char v2[c2]; // ... }; At first glance, the declaration of c1 seems cleaner, but note that to use that in-class initialization syntax, the constant must be a static const of integral or enumeration type initialized by a constant expression. That's quite restrictive: class Y { const int c3 = 7; // error: not static static int c4 = 7; // error: not const static const float c5 = 7; // error: not integral }; I tend to use the "enum trick" because it's portable and doesn't tempt me to use non-standard extensions of the in-class initialization syntax. So why do these inconvenient restrictions exist? A class is typically declared in a header file and a header file is typically included into many translation units. However, to avoid complicated linker rules, C++ requires that every object has a unique definition. That rule would be broken if C++ allowed in-class definition of entities that needed to be stored in memory as objects. See D&E for an explanation of C++'s design tradeoffs. You have more flexibility if the const isn't needed for use in a constant expression: class Z { static char* p; // initialize in definition const int i; // initialize in constructor public: Z(int ii) :i(ii) { } }; char* Z::p = "hello, there"; You can take the address of a static member if (and only if) it has an out-of-class definition: class AE { // ... public: static const int c6 = 7; static const int c7 = 31; }; const int AE::c7; // definition int f() { const int* p1 = &AE::c6; // error: c6 not an lvalue const int* p2 = &AE::c7; // ok // ... } 为什么int是个例外,That rule would be broken if C++ allowed in-class definition of entities that needed to be stored in memory as objects., 应该是这句话吧,因为const double不能直接被编译器优化掉? https://social.msdn.microsoft.com/Forums/vstudio/en-US/a8c30113-c5bc-4ac8-af1e-e43caa9ff118/why-only-static-const-integral-data-members-can-be-initialized-within-the-class?forum=vclanguage 【 在 numb13 的大作中提到: 】 : [md] : ##### 类型的数据,比如static const double等必须在类内定义,在类外赋值。有人知道这是为什么吗? : 以下是个例子 : ...................
xiaobing307机器人#5 · 2016/6/15
这个解释看上去比较靠谱 http://www.gamedev.net/topic/621327-why-cant-static-const-float-members-be-initialized-in-a-class/ Floating-point values have to be initialized at run-time because of the different FPU implementations/modes. Integral constants are always predictable. Floating-point constants change depending on the FPU rounding method currently active when the float is modified/set. It could be rounded, truncated, etc. If they were to be initialized in the headers where the class is defined, the actual value could differ between translation units that include that header. 【 在 numb13 的大作中提到: 】 : [md] : ##### 类型的数据,比如static const double等必须在类内定义,在类外赋值。有人知道这是为什么吗? : 以下是个例子 : ...................
zx723机器人#6 · 2016/6/15
学习一波 通过『我邮2.0』发布