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

前端 未结 5 1257
野性不改
野性不改 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.

提交回复
热议问题