On the scope of the identifier

Scope

Definition: A scope is a valid identifier in the program area of ​​the body.

Category: 1; Function prototype scope

2: local scope

3: class scope

4: namespace scope

1; function prototype scope; in the function prototype declaration parameter range of action is the function prototype scope.

eg:

double area(double radius);

Identifier radius range of action between the left and right brackets in the parameter list of the function area, but can not be referenced in other places.

2; the local scope: the function parameter list scope parameter, the parameter declarations from the beginning of the list, to the far end of the entire body of the function. Board has

eg;

#include<iostream>
using namespace std;
int main()
{
    int a,b,t,j;
    cin>>a>>b;
    t=a+b;
    if(a>0)
    {
        int c;
        cin>>c;
        j=a+c;
    }
    cout<<"t的值为"<<t<<" "<<"j的值为"<<j<<endl;

 } 

We can see from the above a, b, t, j scope entire main function, however, if c is inside the scope.

3; class scope: class can be seen as a collection of well-known member of the class having m members of class X scope.

There are three ways to access the m:

1; if no local scope identifier declared with the same name in the member functions X, then the function can directly access the members m.

eg;

Date date;

date.show();

2: The xm or expression X :: m.

eg:

void Date::show()

{
cout<<Date::day<<endl;
}

3: Such expression by ptr-> m, wherein X ptr to point to an object pointer to the class.

eg;

Date*date = new date();
date->show();

4; namespace scope: a namespace identifies a namespace scope, all the statements within the namespace does not belong to the scope of the respective identifier of the front, are in namespace scoping.

Namespace effect; eliminate errors due to the same name can occur between the various modules and functions caused by type.

The following is a namespace scope of several forms of usage;

1; can refer directly to the current namespace identifier declared inside a namespace, if you need to reference other namespace identifier,

:: namespace name identifier name

eg:

namespace SomeNs{

    class SomeClass{....};
}

2; If you feel namespace qualified is too long, you can use the using statement

using命名空间名::标识符名;
using namespace::标识符名;

3; allows nested namespace

eg:

namespace OuterNs{
    namespace InnerNs{
    class SomeClass{....};
        }
}

Examples of verification; the following global variables declared to have global scope of space, they are valid throughout the document.

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


}
Summary: In fact, the scope of discovery scope refers to the variables defined in different places of the above description, and there will be different results in different situations.

Guess you like

Origin www.cnblogs.com/byp-520/p/11593563.html