C ++ Third job: Scope

Define the scope of

作用域是一个标识符在程序正文中有效的区域。

1. Function prototype scope

Scope of the formal parameters in the function prototype declaration is the function prototype scope.
It is a C ++ program with minimal scope.
example:

double area(double radius);

Wherein the effective radius is in the range of the parameter list area between the left and right brackets, this identifier is not referenced elsewhere in the program.

2. Local Scope

void fun(int a)
{
    int b=a;
    cin>>b;
    if(b>0)
    {
        int c;
        ......
    }
}

Wherein a scope, b, c starting from the scope of the statement to the whole program, to which it is at the end of the block. Block is a pair of hair enclosed in parentheses a program.
Variables declared within a function, its scope from a statement at the beginning, has been up to brace the end of the block where the statement.
Variables with local scope, also known as local variables.

3. class scope

A class is an independent scope, defined outside the class or static variables need to add the class name and the scope symbols. The members of each class is different from any other member of the class.
Outside the class scope, can only be used by members of an object or a pointer member access operator respectively, or using -> to access (not detailed here in this blog).
example:

void Complex::get() {
    ......
}

General data or function members must be accessed through the object.

4. namespace scope

What is the name space? A large program usually consists of different modules, modules of different classes and functions the same name may occur, which would cause an error.
Namespace is to eliminate such errors exist.

(1) namespace Category:

系统命名空间: std
自定义的命名空间:

Such as:

namespace NS
{
    ......
}

A namespace identified by a namespace scope, all declared in the name of the space, all belong to the namespace scope.
Namespaces also allow nesting.
Such as:

namespcace OuterNS{
    namespace InnerNS{
        {
            class SomeClass{...};
        }
}

For reference SomeClass class, you need the following form:

OuterNS::InnerNS::SomeClass

There are also two types of special namespace
global namespace and anonymous namespace
global namespace is the default namespace identifier declared outside a namespace explicitly declared in a global namespace.
Anonymous namespace is a need to explicitly declare a no-name namespace declaration as follows:

namespace {
匿名命名空间内的各种声明(函数声明、类声明......
}

Use (2) of the namespace

使用命名空间:
①通过作用域符  空间名::空间成员
②声明命名空间:  using namespace 命名空间 ;

(3) Examples

# include < iostream>
using namespace std;
int i;        //在全局命名空间中的全局变量
namespace Ns{
    int j;    //在Ns命名空间中的全局变量
};

int main() {
    i=5;    //为全局变量i赋值
    Ns::j=6;    //为全局变量j赋值
    {               //子块1
        using namespace Ns;//使得在当前块中可以直接引用Ns命名空间的标识符
        int i;  //局部变量,局部作用域
        i= 7;   
        cout<<”i="<<i<<endl;//输出7
        cout<<”j="<<j<<endl;//输出6
    }
    cout<<”i="<<i<<endl;//输出5
    return 0; 
}

//运行结果:
//i=7
//j=6
//i=5

Variables with namespace scope, also known as global variables.

Guess you like

Origin www.cnblogs.com/szy211/p/11601025.html