
static const int i = 3;class X { char v[i]; static const int i = 2;}; 从标准来看,
3.3.6/2 A name N used in a class S shall refer to the same declaration in its context and when re-evaluated in the completed scope of S
我认为这意味着’我’将是2,重新评估的东西在这里意味着什么?
解决方法 正确的行为是它应该导致错误,因为重新评估会改变含义:3.3.6节中的示例:
The potential scope of a declaration that extends to or past the end of a class deFinition also extends to the regions defined by its member deFinitions,even if the members are defined lexically outsIDe the class (this includes static data member deFinitions,nested class deFinitions,member function deFinitions (including the member function body and,for constructor functions (12.1),the ctor-initializer (12.6.2)) and any portion of the declarator part of such deFinitions which follows the IDentifIEr,including a parameter-declaration-clause and any default arguments (8.3.6). [Example:
该示例与您的示例类似(使用枚举而不是静态const int):
typedef int c;enum { i = 1 };class X { char v[i]; // error: i refers to ::i // but when reevaluated is X::i int f() { return sizeof(c); } // OK X::c char c; enum { i = 2 };}; 在遇到v [i]时,编译器只知道enum {i = 1}; (或静态const int i = 3;,但是当已知完整类声明时,char v [i]会有所不同,因为我将被重新评估为2.
总结以上是内存溢出为你收集整理的c – 课堂成员的范围全部内容,希望文章能够帮你解决c – 课堂成员的范围所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)