设为首页 加入收藏

TOP

2.3.6 Scope of a Name
2013-10-07 15:23:27 来源: 作者: 【 】 浏览:81
Tags:2.3.6 Scope Name

Every name in a C++(www.cppentry.com) program must refer to a unique entity (such as a variable, function, type, etc.). Despite this requirement, names can be used more than once in a program: A name can be reused as long as it is used in different contexts,
from which the different meanings of the name can be distinguished. The context used to distinguish the meanings of names is a scope. A scope is a region of the program. A name can refer to different entities in different scopes.

Most scopes in C++(www.cppentry.com) are delimited by curly braces. Generally, names are visible from their point of declaration until the end the scope in which the declaration appears. As an example, consider this program, which we first encountered in Section 1.4.2 (p. 14):

  1. #include <iostream>  
  2. int main()  
  3. {  
  4. int sum = 0;  
  5. // sum values from 1 up to 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;  

This programdefines three names and uses two names from the standard library. It defines a function named main and two variables named sum and val. The name main is defined outside any curly braces and is visible throughout the program. Names defined outside any function have global scope; they are accessible from anywhere in the program. The name sum is defined within the scope of the main function. It is accessible throughout the main function but not outside of it. The variable sum has local scope. The name val is more interesting. It is defined in the scope of the for statement (Section 1.4.2, p. 14). It can be used in that statement but not elsewhere in main. It has statement scope.

C++(www.cppentry.com) 中的作用域除了能决定名字的能见度,还能直接控制对象的生命期(lifetime),不可不察。

Scopes in C++(www.cppentry.com) Nest

Names defined in the global scope can be used in a local scope; global names and those defined local to a function can be used inside a statement scope, and so on. Names can also be redefined in an inner scope. Understanding what entity a name
refers to requires unwinding the scopes in which the names are defined:

  1. #include <iostream>  
  2. #include <string>  
  3. /* Program for illustration purposes only:  
  4. * It is bad style for a function to use a global variable and then  
  5. * define a local variable with the same name  
  6. */ 
  7. std::string s1 = "hello"// s1 has global scope  
  8. int main()  
  9. {  
  10. std::string s2 = "world"// s2 has local scope  
  11. // uses global s1; prints ‘‘hello world’’  
  12. std::cout << s1 << " " << s2 << std::endl;  
  13. int s1 = 42; // s1 is local and hides global s1  
  14. // uses local s1; prints ‘‘42 world’’  
  15. std::cout << s1 << " " << s2 << std::endl;  
  16. return 0;  

This program defines three variables: a global string named s1, a local string named s2, and a local int named s1. The definition of the local s1 hides the global s1.

Variables are visible from their point of declaration. Thus, the local definition of s1 is not visible when the first output is performed. The name s1 in that output expression refers to the global s1. The output printed is hello world. The second statement that does output follows the local definition of s1. The local s1 is now in scope. The second output uses the local rather than the global s1. It writes 42 world.

Programs such as the preceeding are likely to be confusing. 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. It is much better to use a distinct name for the local.

应该避免内层变量名遮盖外围作用域的变量名(函数名、类型名同理),对此有的编译器提供了警告选项,如g++ 的-Wshadow。

We’ll have more to say about local and global scope in Chapter 7 and about statement scope in Chapter 6. C++(www.cppentry.com) has two other levels of scope: class scope,which we’ll cover in Chapter 12 and namespace scope, which we’ll see in Section 17.2.

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇5.2 Relational and Logical Oper.. 下一篇2.3.1 What Is a Variable?

评论

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

·Sphinx : 高性能SQL (2025-12-24 10:18:11)
·Pandas 性能优化 - (2025-12-24 10:18:08)
·MySQL 索引 - 菜鸟教 (2025-12-24 10:18:06)
·Shell 基本运算符 - (2025-12-24 09:52:56)
·Shell 函数 | 菜鸟教 (2025-12-24 09:52:54)