Scope summary

Scope

A scope is a valid identifier in the program area of ​​the body. C ++ scope identifiers are <Function prototype scope> <local scope> <class scope> <namespace scope>

1. Function prototype scope

Scope of the formal parameters in the function prototype declaration is the function prototype scope.

例子:double area(double radius);

2. Local Scope

1. function parameter list scope parameter, starting from the statement of the parameter list, the entire function of the body at the end date.

2. The body of the function variables declared in a statement at the beginning of its scope, has been up to brace the end of the block where the statement.

3. Variables with local scope is also referred to as local variables.

3. class scope
Members of the class having m scope of class X

1. If not specified in the member function of the same name in the local scope identifiers X, then the function can directly access the members m.

2. The xm or by the expression X :: m. This is the most basic way to access object members of the program.

3. By ptr-> such expressions m, wherein ptr is a pointer to an object of class X.

4. namespace scope

Example 1:

namespace SomeNS

{

​ class SomeClass{.....};

}

Example 2:

SomeNS::SomeClass obj1;

Example 3:

using namespace name :: identifier name;

using namespace namespace identifier;

Example 4:

namespace OuterNS

{

namespace {InnerNs

class SomeClass{.......};

}

}

Example 5:

namespace{

Anonymous naming various statements in the space (function declarations, type declarations, .......)

}

Program example:

include < iostream>

STD namespace the using;
int i; // global variables in the global namespace
namespace Ns {
int J; Ns // namespace in global variables
};
int main () {
i =. 5; // global variables i assignment
Ns :: j = 6; // global variable assignment j
{// subblock. 1
the using namespace Ns; Ns // makes it possible to directly reference the namespace identifier in the current block
int i; // local variables, partial scope
I =. 7;
COUT << "I =" I << << endl; // output. 7
COUT << "J =" J << << endl; // output. 6
}
COUT << "= I" << i << endl; // output. 5
return 0;
}

operation result:

i=7
j=6
i=5

Variables with namespace scope, also known as global variables.

Guess you like

Origin www.cnblogs.com/ycldbk/p/11605242.html