c++ 作用域

作用域

任何一种语言最基本的部分就是变量,而变量有两个非常重要的特性,作用域和生存期。

定义

作用域是变量的一个属性,某个变量在代码中有效的区域为该变量的作用域。

Block Scope

从变量定义处开始,到Block结束。(如果,有嵌套的Block要视情况定义)函数参数的作用域类似。但是,这里有一个新奇的函数,第一次遇到function-try-block

int f(int n = 2)  // scope of 'n' begins
try // function try block
{         // the body of the function begins
    ++n;   // 'n' is in scope and refers to the function parameter
    {
        int n = 2; // scope of the local variable 'n' begins
             // scope of function parameter 'n' interrupted 
        ++n; // 'n' refers to the local variable in this block
    }            // scope of the local variable 'n' ends
             // scope of function parameter 'n' resumes
} catch(...) {
    ++n; // n is in scope and refers to the function parameter
    throw;
} // last exception handler ends, scope of function parameter 'n' ends

初次看,总会觉得一定是代码写错了。实际上,代码是完全正确的。那么,这样的函数有什么用处呢??不知道。

函数原型作用域

函数声明参数从参数声明开始到函数声明结束。

const int n=10;
int f(int n,int m = n);   //error,n referrence to (int n) not (const int n)

函数作用域

函数中的变量属于Block Scope ,函数中的label的作用域与变量的情况不太一样。

void f(){
    goto aaa;     // 从函数开始,即是作用域
aaa:              
    goto aaa;     //直到函数结束
}
void g(){           
    goto aaa;     //error,  只在label声明的函数中,为其作用域,其他函数不是其作用域
}
goto aaa;         //error

label的作用域为该函数中的所有位置,不管是声明前,还是声明后。

命名空间作用域

在源程序中,其实,全局作用域也是一个命名空间,全局命名空间中变量的作用域从声明开始,到编译单元中断,在连接的时候,又将多个编译单元的全局命名空间连接在一起。

命名空间的变量的作用域:

  • 从声明开始,到该命名空间结束中断,在新的一段同名命名空间开始继续开始。
  • 在使用using的地方,将该部分加入到作用域中。
  • 命名空间的子命名空间也包含其中。

匿名命名空间和inline命名空间变量的作用域包含其父亲命名空间。

namespace A{      //Scope of A begins
    int a1=3;       //Scope of a1 begins
    inline namespace B{   //Scope of B  begins
        int a2;           //Scope of a2 begins
    }
    namespace C{          //Scope of C begins
        int a3=3;           //scope of a3 begins
        int f(){
            cout<<a1<<endl;
        }
    }                     //scope of a3 ends
    namespace {
        int a4;           //scope of a4 begins
    }   
    //int a2;    //error: duplicate defination a2
}                         //scope of a1,a2,B,C,a4 interrupted
namespace A{    //scope of a1,a2,B,C,a4 continue

}

类作用域

class中的变量的作用域为:

  • 从声明开始,到class结束
  • 所有的类中成员函数的函数体中,不管是在声明之前,或者是在class之外
  • 函数的默认参数中
  • 所有嵌套在函数中的成员

例:

class X {
    int f(int a = n) { // X::n is in scope inside default parameter
         return a*n;   // X::n is in scope inside function body
    }
    int g();
    int i = n*2;   // X::n is in scope inside initializer

//  int x[n];      // Error: n is not in scope in class body
    static const int n = 1;
    int x[n];      // OK: n is now in scope in class body
};
int X::g() { return n; } // X::n is in scope in out-of-class member function body

访问类中成员的方法有以下四种:

  • 在class的作用域中,或子类的作用域中
  • class或者子类的对象使用’.‘
  • class或者子类对象的指针,使用'->'
  • class 或者子类使用'::'

枚举作用域

枚举有两种类型: scoped enumeration and unscoped enumeration

这两种类型的作用域是不同的。

scoped enumeration: 作用域从变量声明开始,到enumeration结束为止。

unscoped enumeration: 作用域从变量声明开始,到enumeration结束,继续存在,直到全局作用域结束。

enum e1{
    A,
    B
};
enum class e2{
    A2,
    B2
};
e1 o1 = B;
//e2 o2 = B2;    //error : B2 not in scope
e2 o2 = e2::B2;

声明在什么地方发生

在上面我们提到的多种作用域中,很多变量作用域开始是从变量声明点开始,那么,变量声明点是在什么地方。

普通变量

声明点在标识符出现之后,初始化之前:

int a=10;
{
    int a = a;   // a is not definite
}

const int x = 2; // scope of the first 'x' begins
{
    int x[x] = {}; // scope of the second x begins before the initializer (= {})
                   // but after the declarator (x[x]). Within the declarator, the outer
                   // 'x' is still in scope. This declares an array of 2 int.
}

class ,enumerate, template

声明在标识符出现之后,马上发生。

type alias or alias template

声明在alias发生之后。

using T = int; // point of declaration of T is at the semicolon
using T = T;   // same as T = int

enumerator

声明在enumerator定义之后。

const int x = 12;
{
    enum { x = x + 1, // point of declaration is at the comma, x is initialized to 13
           y = x + 1  // the enumerator x is now in scope, y is initialized to 14
         };
}
c++生存期: 生存期
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值