标签:
任何一种语言最基本的部分就是变量,而变量有两个非常重要的特性,作用域和生存期。
作用域是变量的一个属性,某个变量在代码中有效的区域为该变量的作用域。
从变量定义处开始,到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的作用域为该函数中的所有位置,不管是声明前,还是声明后。
在源程序中,其实,全局作用域也是一个命名空间,全局命名空间中变量的作用域从声明开始,到编译单元中断,在连接的时候,又将多个编译单元的全局命名空间连接在一起。
命名空间的变量的作用域:
匿名命名空间和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 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
访问类中成员的方法有以下四种:
枚举有两种类型: 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++生存期:生存期标签:
原文地址:http://blog.csdn.net/nilbooy/article/details/51361971