3.3.6 - [basic.scope.class] - 【基本.作用域.类】

请不要转载本文;请不要以任何形式重新出版,发布本文;请在下载本文 24 小时内将其删除;禁止将本文用于商业目的。

3 Basic concepts [basic]

3.3 Declarative regions and scopes [basic.scope]
3.3.6 Class scope [basic.scope.class]

 

3 基本概念 【基本】

3.3 定义区域和作用域 【基本.作用域】
3.3.6 类作用域 【基本.作用域.类】

 

The following rules describe the scope of names declared in classes.
  1. The potential scope of a name declared in a class consists not only of the declarative region following the name's declarator, but also of all function bodies, default arguments, and constructor ctor-initializers in that class (including such things in nested classes).
  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. No diagnostic is required for a violation of this rule.
  3. If reordering member declarations in a class yields an alternate valid program under 1 and 2, the program is ill-formed, no diagnostic is required.
  4. A name declared within a member function hides a declaration of the same whose scope extends to or past the end of the member function's class.
  5. 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:

        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 };
        };

        typedef char*  T;
        struct Y {
            T  a;                           //
    error: T refers to ::T
                                            //
    but when reevaluated is Y::T
            typedef long T;
            T  b;
        };

        typedef int I;
        class D {
            typedef I I;                    //
    error, even though no reordering involved
        };

    --end example]

 
如下规则描述了在类中声明的名字的作用域。
  1. 在类中声明的名字的潜在作用域不仅包括其声明符后面的声明区域,还包括该类的所有函数体,默认参数,以及构造函数的 ctor-初始化符,(包括嵌套类的相应东西)。
  2. 在类 S 中应用的名字 N 在其语境中所指代的声明,以及当 S 的作用域完成时所重新评估得到的声明应当相同。违反此条规则不需要诊断。
  3. 如果在规则 1 和 2 下,对某个类的成员声明重新安排会产生不同的合法程序,则程序是病态形式的,不需要诊断。
  4. 在成员函数中声明的名字,将隐藏作用域为此成员函数所在类之外,或结尾之后的相同名字的声明。
  5. 在类定义外部或之后的声明的潜在作用域同样在该类的成员定义之外,而不管这些成员在词法上是否定义在类(这些成员包括静态数据成员定义,嵌套类定义,成员函数定义(包括成员函数体和构造函数(12.1)的 ctor-初始化符(12.6.2)) ,或任何在该标识符后面的声明符部分,包括参数-声明-子句以及任何默认自变量(8.3.6)的外面。【例:

        typedef int  c;
        enum { i = 1 };

        class X {

            char  v[i];                     // 错误:i 指代 ::i
                                            // 但当进行重新评估后将为 X::i
            int  f() { return sizeof(c); }  // 正确: X::c
            char  c;
            enum { i = 2 };
        };

        typedef char*  T;
        struct Y {
            T  a;                           //
    错误:T 指代 ::T
                                            //
    但当进行重新评估后将为 Y::T
            typedef long T;
            T  b;
        };

        typedef int I;
        class D {
            typedef I I;                    //
    错误,即使没有对其重新安排
        };

    --例完

 

The name of a class member shall only be used as follows:
  • in the scope of its class (as described above) or a class derived (clause 10) from its class,
  • after the . operator applied to an expression of the type of its class (5.2.5) or class derived from its class,
  • after the -> operator applied to a pointer to an object of its class (5.2.5) or a class derived from its class,
  • after the :: scope resolution operator (5.1) applied to the name of its class or a class derived from its class.

 

类成员的名称仅能被按照下列情况使用:
  • 在其类的作用域(如上所述)或某派生(章节 10)自该类的类的作用域,
  • 在应用到该类,或派生自该类的类型的表达式(5.2.5)上的 . 运算符后面,
  • 在应用到指向该类,或派生自该类的类对象的指针(5.2.5)上的 -> 运算符后面,
  • 在应用到该类,或派生自该类的类名称上的 :: 作用域解析运算符(5.1)后面。

 

PREV [basic.scope.namespace] | NEXT [basic.scope.hiding]上一页 【基本.作用域.名字空间】 | 下一页 【基本.作用域.隐藏】
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值