The scope identifier - Notes

The scope identifier

Scope: active identifier in the program area of ​​the body

There are the following categories:

  • Function prototype scope
  • Local scope (Scope block)
  • Class scope
  • Namespace scope

1. Function prototype scope

When function prototype declaration scope parameter is the function prototype scope.

Another: C ++ function prototype program scope is the smallest scope

example:

    double area(double radius);//函数声明

Picture error:

He explained: radius of Scope in between the left and right parentheses. (Note: Function Prototype function parameter list is the parameter type identifier may be omitted, but strong for readability, it is necessary to write the Identifier.)

Role: to enhance code readability

2. The local scope (local variables)

The scope parameter function list: parameter declarations from the end of the entire function →

Variables declared inside the body functions: declare at → braces

Examples:
#include
using namespace std;

        void fun(int para) {    //定义一个测试函数,para为形参,作用域到该函数大括号为止
            cout << "para = " << para<<endl;//可以打印出para = 0
            int test1 = 1;      //定义test1,作用域从此到所在块大括号为止
            if (test1) {     
                cout << "--------大括号开始" << endl;
                int test2 = 2;      //定义test2,作用域是从此到所在块大括号为止
                cout << "test1 = " << test1 << endl;   //在test1作用域范围内,所以可以访问
                cout << "test2 = " << test2 << endl;
                cout << "--------大括号结束" << endl;
            }
            cout << "test1 = " << test1 << endl;    
            cout << "para = " << para << endl;  //分别可以打印出test1 和 para,理解作用域
        
        }
        //测试函数
        int main() {
            fun(0);
            return 0;
        }
            

Screenshots results:

If the access identifier-scope error. Such as:

If you added if there int test1 = 3;will be blocked on the level test1

result:

So when using a clear purpose, the definition of local or global identifier.

effect:

  • Prevent data from being contaminated. If you do not want to be used if the data in the external, can be defined if inside, so if accessed outside of error.
  • Save memory space. Variables have local scope also into local variables, local variables will be destroyed after use, to make room.

3. class scope

Class can be seen as a collection of well-known members.

Access Class X members in three ways:

  • Ruoguo X no local member function scope identifier declared with the same name, you can directly access member m. Ruoguo same name, by this-> m access.
  • xm or X :: m access, X :: m used to access static members.
  • ptr-> m Access, where ptr is a pointer to a class.

example:

        #include<iostream>
        using namespace std;
        
        class X {  //定义一个类X
        public:
            void fun1();
            void fun2(int m);
        private:
            int m = 3;
        };
        void X::fun1() {
            cout << "m =" << m << endl;//没有同名标识符,直接访问
        }
        void X::fun2(int m) {  //通过X::fun2访问
            cout << "传入的m = " << m << endl;  //有同名标识符,打印传入的参数
            cout << "访问类的m = " << this->m << endl;
        }
        int main() {
            X x;
            x.fun1();  //!!没有参数也要加括号
            x.fun2(6);
            cout << "指针访问-----------------" << endl;
            X* ptr = &x; //给指针赋初值
            ptr->fun1();
            ptr->fun2(5);
            return 0;
        }

Output:

4. namespace scope (global variable)

Role: the role of the equivalent of a folder.

Usually consists of different modules in the development, the different modules developed by different personnel, there may be an error caused by the same name. Namespace can solve this problem, such as "Nanjing Road", Shanghai Nanjing Road Nanjing Road Wuhan there, which in the end is a Nanjing Road it? Wuhan or Shanghai if coupled with such a "namespace" before the Nanjing Road can be distinguished.

grammar:

    namespace 命名空间名{     //定义
    
        命名空间的声明(函数声明,类申明,全局的数据......)
    

    }
    命名空间名::标识符号      /使用

    //在另一个作用域使用,c++提供了using语句
    using 命名空间名::标识符号;     //暴露指定标识符
    using namespace 命名空间名;    //暴露该命名空间所有标识符

example:

Create a header file .h, enter the global declaration

namespace.h

    #pragma once  /*表示该文件只运行一次,,因为文件中如果加载了很多东西,每次需要的时候都要加载的话会浪费运行速度。*/
    #include<iostream>
    using namespace std;
    #ifndef MYHEAD_H    /*编译预处理,防止命名空间嵌套时重复命名*/
    #define MYHEAD_H  
    namespace mynamespace {
        class Tangle {
            private:
                double lon, wid; //只是外部不能访问,可以进行修改
            public:
                void caltangle(int lon = 1, int wid = 1); //声明caltangle函数,并给lon wid 默认参数
        };
    }
    #endif 

Then create a new file .cpp achieve caltangle function, calculate the area of ​​a rectangle

namespace.cpp

    #include"./namespace.h"//引入头文件
    #include<iostream>
    
    using namespace std;
    using namespace mynamespace;//暴露mynamespace所有标识符
    //实现caltangle函数,计算长方形的面积
    void Tangle::caltangle(int lon=1, int wid=1) {
        long area = lon * wid;
        cout << "arae = " << area;
    }

test

main.cpp

    #include<iostream>
    #include<cmath>
    using namespace std;
    
    #include"./namespace.h"  //引入头文件
    using namespace mynamespace;  //暴露mynamespace所有标识符
    //测试
    int main() {
        Tangle tag;
        int lon, wid;
        cout << "enter two number:";
        cin >> lon >> wid;
        tag.caltangle(lon, wid);
        return 0;
    }

result:

Such a long period code into the statement, implemented, tested three modules. Benefits are: repeatability better management and better, more readable, more reliable code.

Guess you like

Origin www.cnblogs.com/SailorMoon-z/p/11593489.html