手机版
你好,游客 登录 注册
背景:
阅读新闻

C++中参数的持续性,作用域和连接性

[日期:2012-08-14] 来源:Linux社区  作者:paul_wuhaha [字体:大 中 小]
如果你运行了程序会发现,tom的地址在两个文件是一样的,但是呢,另两个变量地址不同,所以extern被认为是引用,不给这个变量分配空间。一般的,如果在一个文件定义一个外部链接性变量,另一个文件不使用extern,而是重新定义它,那就会报错。

接下来介绍下无链接性的静态变量。

  1. #include <iostream>   
  2.   
  3. const int ArSize = 10;  
  4.   
  5. void strcount(const char * str);  
  6.   
  7. int main()  
  8. {  
  9.     using namespace std;  
  10.     char input[ArSize];  
  11.     char next;  
  12.   
  13.     cout << "Enter a line: \n";  
  14.     cin.get(input, ArSize);  
  15.     while(cin)  
  16.     {  
  17.         cin.get(next);  
  18.         while(next != '\n')  
  19.             cin.get(next);  
  20.         strcount(input);  
  21.         cout << "Enter next line (empty line to quit): \n";  
  22.         cin.get(input, ArSize);  
  23.     }  
  24.     cout << "Bye\n";  
  25.     return 0;  
  26. }  
  27.   
  28. void strcount(const char * str)  
  29. {  
  30.     using namespace std;  
  31.     static int total = 0;           //静态存储,无链接性,作用域在函数中,但生命周期是整个文件,所以只经过一次初始化   
  32.     int count = 0;                  //每次执行函数都重新初始化这个变量   
  33.       
  34.     cout << "\""  << str << "\" contains ";  
  35.     while(*str++)  
  36.         count++;  
  37.     total += count;  
  38.     cout << count << " characters\n";  
  39.     cout << total << " characters total \n";  
  40. }  
static 声明的变量生命周期在整个文件中,但作用域只在声明的代码块中。

这个例子最好自己运行下,多动手就理解了。

接下来再简单说下const,在全局定义const就类似使用static说明符。如果要让一个常量的链接性为外部的,那么就用extern来覆盖原有的内部链接性。如 extern const int state = 10;

在C++中,针对函数有个单定义规则,对每个非内联函数,程序只能包含一个定义,对于链接性为外部的函数来说,意味着在多文件程序中,只有一个文件包含该函数定义,但使用该函数的每个文件都应包含函数原型。

下面简单说下布局new操作符,它可以指定要使用的位置,例如可以声明一个char型数组 buffer[100]然后使用布局new操作符来初始化,int *p = new (buffer)int[20];则说明了,p指向的数组放在了Buffer[]中,p的首地址就是buffer数组的首地址。

紧接着介绍下命名空间,命名空间不能位于代码块中。

  1. namespace Jill{  
  2.     int x;  
  3. }  
接下来访问命名空间的名称,有两种方式,一种称为using声明,还一种称为using编译指令。

两者的不同是编译指令会使整个命名空间可用,但是using声明不会,他只是该用哪就用在哪,因为使用using编译指令的话,当出现了同名的局部变量则会覆盖命名空间里的变量。像上面的命名空间,using声明表示为 using Jill::x,using namespace Jill则是using编译指令。还有一种是没有命名的名称空间,如

  1. namespace  
  2. {  
  3.     int y;  
  4. }  
像这样的名称空间可以用来替代链接性为内部的静态变量。(最好用名称空间)。

可能听着晕乎乎的,还是上代码。答案我写在注释里了

  1. #include <iostream>   
  2. using namespace std;  
  3. void other();  
  4. namespace n1  
  5. {  
  6.     int x = 1;  
  7. }  
  8.   
  9. namespace n2  
  10. {  
  11.     int x = 2;  
  12. }  
  13.   
  14. int main()  
  15. {  
  16.     using namespace n1;  
  17.     cout << x << endl;          // 1   
  18.     {  
  19.         int x = 4;  
  20.         cout << x << ", " << n1::x << ", " << n2::x << endl;    // 4, 1, 2   
  21.     }  
  22.     using n2::x;  
  23.     cout << x << endl;          // 2   
  24.     other();  
  25.     return 0;  
  26. }  
  27.   
  28. void other()  
  29. {  
  30.     using namespace n2;  
  31.     cout << x << endl;          // 2   
  32.     {  
  33.         int x = 4;  
  34.         cout << x << ", " << n1::x << ", " << n2::x << endl;    // 4, 1, 2   
  35.     }  
  36.     using n2::x;  
  37.     cout << x << endl;          // 2   
  38. }  
好了,先介绍到这。linux
相关资讯       C++ 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款