The visibility of C + + variables

Source: Internet
Author: User

As we have described earlier, there should be no two variables with the same name in a function. However, we take the following program code (program 11.1.3) to test, found incredibly in the same function can have two names of the same variable. What's going on here? How does the compiler identify the two variables with the same name?
#include "iostream.h"
int main ()
{
int a=3,b=4;
{
int a=5,b=6;
{
Char a= ' A ', b= ' B ';
cout <<a <<b <<endl;
}
cout <<a <<b <<endl;
}
cout <<a <<b <<endl;
return 0;
}

Operation Result:
Ab
56
34

We have already explained that the scope of a variable can be used by the variable's scope. The scope of variables at different levels is like a piece of paper of varying size. Stacking them up will cause some pieces of paper to be covered. We refer to this variable scope as a masking condition Visibility of variables (Visibility)。 As shown in Figure 11.1 below:
The compiler depends on the visibility of the variable to determine which variable we are referencing. Specifically in the program is:
#include "iostream.h"
int main ()
{
int a=3,b=4;//integer variable a=3, b=4 scope start
{
int a=5,b=6;//integer variable a=5, b=6 scope start, integer variable a=3, b=4 not visible
{
Char a= ' A ', b= ' B ';//character variable a= ' a ', b= ' B ' scope start, integer variable A, b not visible
cout <<a <<b <<endl;//output character variable, integer variable A, b not visible
}//character-type variable a= ' A ', b= ' B ' scope end
cout <<a <<b <<endl;//output integer variable a=5, b=6, Integer variable a=3, b=4 not visible
}//integer variable a=5, b=6 end of scope
cout <<a <<b <<endl; Output integer variable a=3, b=4
return 0;
}//integer variable a=3, b=4 end of scope

However, when two sheets of paper are at the same level, it is clear that they cannot be covered. So, if we declare two variables of the same name in the same hierarchy, then their scope is not a cover, but a conflict.

Therefore, multiple variables with the same name cannot be declared within the same syntax level of a function.

The visibility of C + + variables (RPM)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.