设为首页 加入收藏

TOP

2.2 Variables (8)
2013-10-07 16:16:50 来源: 作者: 【 】 浏览:80
Tags:2.2 Variables

2.2.4 Scope of a Name

At any particular point in a program, each name that is in use refers to a specific entity—a variable, function, type, and so on. However, a given name can be reused to refer to different entities at different points in the program.

A scope is a part of the program in which a name has a particular meaning. Most scopes in C++(www.cppentry.com) are delimited by curly braces.

The same name can refer to different entities in different scopes. Names are visible from the point where they are declared until the end of the scope in which the declaration appears.

As an example, consider the program from § 1.4.2 (p. 13):

  1. #include <iostream>   
  2. int main()   
  3. {   
  4. int sum = 0;   
  5. // sum values from 1 through 10 inclusive   
  6. for (int val = 1; val <= 10; ++val)   
  7. sum += val; // equivalent to sum = sum + val   
  8. std::cout << "Sum of 1 to 10 inclusive is "   
  9. << sum << std::endl;   
  10. return 0;   
  11. }  

This programdefines three names—main, sum, andval—and uses the namespace name std, along with two names from that namespace—cout and endl.

The name main is defined outside any curly braces. The name main—like most names defined outside a function—has global scope. Once declared, names at the global scope are accessible throughout the program. The name sum is defined within the scope of the block that is the body of the main function. It is accessible from its point of declaration throughout the rest of the main function but not outside of it. The variable sum has block scope. The name val is defined in the scope of the for statement. It can be used in that statement but not elsewhere in main.

ADVICE: DEFINE VARIABLES WHERE YOU FIRST USE THEM

It is usually a good idea to define an object near the point at which the object is first used. Doing so improves readability by making it easy to find the definition of the variable. More importantly, it is often easier to give the variable a useful initial value when the variable is defined close to where it is first used.

Nested Scopes

Scopes can contain other scopes. The contained (or nested) scope is referred to as an inner scope, the containing scope is the outer scope.

Once a name has been declared in a scope, that name can be used by scopes nested inside that scope. Names declared in the outer scope can also be redefined in an inner scope:
 

  1. #include <iostream>   
  2. // Program for illustration purposes only: It is bad style for a function   
  3. // to use a global variable and also define a local variable with the same name   
  4. int reused = 42; // reused has global scope   
  5. int main()   
  6. {   
  7. int unique = 0; // unique has block scope   
  8. // output #1: uses global reused; prints 42 0   
  9. std::cout << reused << " " << unique << std::endl;   
  10. int reused = 0; // new, local object named reused hides global reused   
  11. // output #2: uses local reused; prints0 0   
  12. std::cout << reused << " " << unique << std::endl;   
  13. // output #3: explicitly requests the global reused; prints 42 0   
  14. std::cout << ::reused << " " << unique << std::endl;   
  15. return 0;   
  16. }  

Output #1 appears before the local definition of reused. Therefore, this output statement uses the name reused that is defined in the global scope. This statement prints 42 0. Output #2 occurs after the local definition of reused. The local reused is now in scope. Thus, this second output statement uses the local object named reused rather than the global one and prints 0 0. Output #3 uses the scope operator (§ 1.2, p. 8) to override the default scoping rules. The global scope has no name. Hence, when the scope operator has an empty left-hand side, it is a request to fetch the name on the right-hand side from the global scope. Thus, this expression uses the global reused and prints 42 0.

It is almost always a bad idea to define a local variable with the same name as a global variable that the function uses or might use.

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇2.6 Defining Our Own Data Struc.. 下一篇2.2 Variables (6)

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·数据库:推荐几款 Re (2025-12-25 12:17:11)
·如何最简单、通俗地 (2025-12-25 12:17:09)
·什么是Redis?为什么 (2025-12-25 12:17:06)
·对于一个想入坑Linux (2025-12-25 11:49:07)
·Linux 怎么读? (2025-12-25 11:49:04)