c ++ namespace and the precautions

preamble

"Current domain" appears below the "current scope" shorthand
namepsace in c ++ variant of the keyword is used to avoid conflicts with the same name in different modules, this rough introduction about namespace usage and the need to pay attention local:
1. namespace specified by the display, use or introduced using symbols, or using namepsace used to load the entire namespace manner under a symbolic name space
2. the display case namespace specified symbol belongs, regardless of whether the expression where are namespace in accordance with an absolute path instead of a relative path matching
3.using instruction is present scope namespace allows simultaneous use of multiple, simultaneous use of a plurality namespace same reference numerals and will not be covered, but the compilation package ambigious
. 4. using namespace corresponding to the variables introduced in the current global domain, where local variables will override the global variables; introduced directly using variables corresponding to the current variable domain definition, the definition of the same variables are not allowed, otherwise an error.

text

Using namespace

Hereinafter called simply creates N1 namespace in which placed a variable Number , and used it in the main (),

namespace n1{
    int number = 1;
};

int main(){
    //1.显式指出使用那个namespace下的number
    std::cout<<n1::number<<std::endl; 
    
    //2.引入n1::number后再使用它
    using n1::number;                
    std::cout<<number<<std::endl;
    return 0;
}

One way to get there is another n1 :: number, by using namespace load the entire namespace in, but this approach has its complexity (described later in detail), is generally not recommended

namespace n1{
    int number = 1;
}

int main(){
    using namespace n1;        //使用namespace n1
    std::cout<<number<<std::endl; 

    return 0;
}

namepsace symbol lookup rules

Because namespace support nested, in the case of explicitly pointed namespace symbol belongs, no matter on what this expression, are in accordance with the absolute path to match the namespace from scratch, what does that mean, as follows:

namespace n1{
    void fn(){
        std::cout<<"1"; //这里是去找std::cout, 而不是找 n1::std::cout
    }
}

Global namspace

Because the compiler is a program created with the default , Ltd. Free Join namespace (not shown specify the definition of life variables in the namespace of the rest are in the global namespace), for global namespace, :: used directly in front do not need anything added

int number = 0;

int main(){
    std::cout<<::temp<<std::endl;
return 0;
}

Scope

The following example shows using lexical scoping, and variable scoping rules as general

namespace n1{
    int number = 1;
}

namespace n2{
    int number = 2;
}

int main(){
    {
        using  n1::number;
        std::cout<<number<<std::endl;     // 1 输出n1::number
        {
            using  n2::number;
            std::cout<<number<<std::endl; // 2 输出n2::number
        }
        std::cout<<number<<std::endl;     // 1 输出n1::number
    }
    return 0;
}

cover

When using namespace time, it will use a namespace, the namespace supports a plurality, in the case of simultaneous use of a plurality of the namespace, will mark the presence of the same name conflict, as follows:
At this time, still need to be displayed He pointed out that to solve your namespace

int number = 0;         //global namespace

namespace n1{
    int number = 1;      //n1 namespace
}

int main(){
    using namespace n1;
    std::cout<<number<<std::endl; //由于同时存在两个全局变量number,所以此处会报ambigious的错误
return 0;
}

But here using a variable if there is no problem, which is equivalent to the introduction of a variable number in the current domain

int number = 0;

namespace n1{
    int number = 1;
}

int main(){
    using n1::number;              //声明接下来要使用n1::number了,接下里的number都是它,相当于在当前域引入了一个变量number
    std::cout<<number<<std::endl; //没问题,输出1
return 0;
}

When using a namespace, when the current field is equivalent to the global namespace variables are introduced in a, as usual, local variables override global variables

namespace n1{
    int number = 1;
}

int main(){
    int number = 0;
    using namespace n1;
    std::cout<<number<<std::endl;//输出的结果为0, 局部变量覆盖了n1::number
return 0;
}

Note, however, if it is introduced into the current variable domain, and said before the same ability to define a variable, the compiler strictly controlled, and no longer allowed to define the same symbol

namespace n1{
    int number = 1;
}

int main(){
    using n1::number;    // error: redeclaration of ‘int number’
    int number = 0;
return 0;
}

Guess you like

Origin www.cnblogs.com/ishen/p/12078652.html