[Supplement] C ++ storage modifier-detailed explanation of living space

Source: Internet
Author: User

C ++ names can be used only in certain regions of a program. this area is called the "scope" of the name. scope determines the "lifetime" of a name that does not denote an object of static extent. scope also determines the visibility of a name, when class constructors and Destructors are called, and when variables local to the scope are initialized. there are five kinds of scope:

  • Local scope. A name declared within a block is accessible only within that block and blocks enclosed by it, and only after the point of declaration. the names of formal arguments to a function in the scope of the outermost block of the function have local scope, as if they had been declared inside the block enclosing the function body. consider the following code fragment:
    {    int i;}

    Because the DeclarationiIs in a block enclosed by curly braces,iHas local scope and is never accessible because no code accesses it before the closing curly brace.

  • Function scope. labels are the only names that have function scope. They can be used anywhere within a function but are not accessible outside that function.
  • File Scope. any name declared outside all blocks or classes has File Scope. it is accessible anywhere in the Translation Unit after its declaration. names with file scope that do not declare static objects are often called "Global" names.
  • Class scope. Names of Class Members have class scope. class member functions can be accessed only by using the Member-selection operators (.Or->) Or pointer-to-member operators (.*Or-> *) On an object or pointer to an object of that class; nonstatic class member data is considered local to the object of that class. Consider the following class declaration:
    class Point{    int x;    int y;};

    The class membersxAndyAre considered to be in the scope of classPoint.

  • Prototype scope. Names declared in a function prototype are visible only until the end of the prototype. The following prototype declares two names (szDest,szSource); These names go out of scope at the end of the prototype:
    char *strcpy( char *szDest, const char *szSource );


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.