Why is this c++ working? (variables with the same name)

前端 未结 5 1246
野性不改
野性不改 2020-12-10 08:03

Alright, I wanna know why this code is working, I just realized that I have two variables with the same name within the same scope.

I\'m using g++ (gcc 4.4).

相关标签:
5条回答
  • 2020-12-10 08:38

    Because you are allowed to have two variables of the same name within the same scope, but not within the same declaration space. The compiler takes the most-local variable of the appropriate name, similar to how you can 'hide' global variables of name X with a member variable of name X. You should be getting a warning though.

    0 讨论(0)
  • 2020-12-10 08:49

    In C/C++ the variable scope is limited by the braces so the following code is valid for the compiler:

    int k()
    { 
        int k;
        {  
            int k;
            {
               int k;               
            }    
        } 
    } 
    
    0 讨论(0)
  • 2020-12-10 08:54

    Alright, I wanna know why this code is working, I just realized that I have two variables with the same name within the same scope.

    You seem confused about scopes. They're not "within the same" scope... the for loop's k has it's own nested/inner scope. More importantly, to see why the language allows it, consider:

    #define DO_SOMETHING \
        do { for (int i = 1; i <= 2; ++i) std::cout << i << '\n'; } while (false)
    
    void f()
    {
        for (int i = 1; i <= 10; ++i)
            DO_SOMETHING();
    }
    

    Here, the text substituted by the macro "DO_SOMETHING" gets evaluated in the same scope as i. If you're writing DO_SOMETHING, you may need its expansion to store something in a variable, and settle on the identifier i - obviously you have no way of knowing if it'll already exist in the calling context. You could try to pick something more obscure, but you'd have people using such convoluted variable names that their code maintainability suffered, and regardless sooner or later there would be a clash. So, the language just lets the inner scopes introduce variables with the same name: the innermost match is used until its scope exits.

    Even when you're not dealing with macros, it's a pain to have to stop and think about whether some outer scope is already using the same name. If you know you just want a quick operation you can pop it an indepedent (nested) scope without considering that larger context (as long as you don't have code in there that actually wants to use the outer-scope variable: if you do then you can sometimes specify it explicitly (if it's scoped by namespaces and classes, but if it's in a function body you do end up needing to change your own loop variable's name (or create a reference or something to it before introducing your same-named variable)).

    0 讨论(0)
  • 2020-12-10 08:59

    The k in the inner for loop shadows (or hides) the k in the outer for loop.

    You can declare multiple variables with the same name at different scopes. A very simple example would be the following:

    int main()
    {
        int a;       // 'a' refers to the int until it is shadowed or its block ends
        { 
            float a; // 'a' refers to the float until the end of this block
        }            // 'a' now refers to the int again
    }
    
    0 讨论(0)
  • 2020-12-10 09:03

    From the standard docs, Sec 3.3.1

    Every name is introduced in some portion of program text called a declarative region, which is the largest part of the program in which that name is valid, that is, in which that name may be used as an unqualified name to refer to the same entity. In general, each particular name is valid only within some possibly discontiguous portion of program text called its scope. To determine the scope of a declaration, it is sometimes convenient to refer to the potential scope of a declaration. The scope of a declaration is the same as its potential scope unless the potential scope contains another declaration of the same name. In that case, the potential scope of the declaration in the inner (contained) declarative region is excluded from the scope of the declaration in the outer (containing) declarative region.

    It might sound confusing in your first read, but it does answer your question.

    The potential scope is same as the scope of the declaration unless another (inner) declaration occurs. If occurred, the potential scope of the outer declaration is removed and just the inner declaration's holds.

    Hope am clear and it helps..

    0 讨论(0)
提交回复
热议问题