C、C++变量auto,static,register,extern类型

  推导类型变量:/Zc:auto[-] compiler option tells the compiler how to use the auto keyword to declare variables.‘ data-guid="0be6182f1ca27bd1ea991f3f06584d3b">编译器选项指示编译器如何使用 auto 关键字来声明变量。 如果指定默认选项 /Zc:auto,编译器从其初始化表达式中推导声明的变量的类型。 如果指定 /Zc:auto-,编译器将该变量分配给自动存储类。
  
  使用示例:
  
  auto root = new TreeNode(*pre_first); 这里root为 TreeNode *
  
  auto inRootPos = find(in_first,in_last,*pre_first); 这里inRootPos 为 int
  
  auto leftSize = distance(in_first,inRootPos); 这里leftsize 为 int
  
  static:
  
  它是C,C++中都存在的关键字,它主要有三种使用方式,其中前两种只指在C语言中使用,第三种在C++中使用。
  
  (1)局部静态变量
  
  (2)外部静态变量/函数
  
  (3)静态数据成员/成员函数
  
  局部静态变量
  
  与auto类型(普通)局部变量相比, static局部变量有三点不同:
  
  1. 存储空间分配不同,auto类型分配在栈上,属于动态存储类别,占动态存储区空间,函数调用结束后自动释放;而static分配在静态存储区,在程序整个运行期间都不释放。两者之间的作用域相同,但生存期不同。
  
  2. static局部变量在所处模块的初次运行时进行初始化工作,且只初始化一次。
  
  3. 对于局部静态变量,如果不赋初值,编译期会自动赋初值0或空字符;而auto类型的初值是不确定的。(对于C++中的class对象例外,class的对象实例如果不初始化,则会自动调用默认构造函数,不管是否是static类型)
  
  二、外部静态变量/函数
  
  在C中static有了第二种含义:用来表示不能被其它文件访问的全局变量和函数。为了限制全局变量/函数的作用域,函数或变量前加static使得函数成为静态函数。但此处“static”的含义不是指存储方式,而是指对函数的作用域仅局限于本文件(所以又称内部函 数)。注意此时,对于外部(全局)变量,不论是否有static限制,它的存储区域都是在静态存储区,生存期都是全局的。此时的static只是起作用域限制作用,限定作用域在本模块(文件)内部。使用内部函数的好处是:不同的人编写不同的函数时,不用担心自己定义的函数,是否会与其它文件中的函数同名。
  
  三、静态数据成员/成员函数(C++特有)
  
  C++重用了这个关键字,并赋予它与前面不同的第三种含义:表示属于一个类而不是属于此类的任何特定对象的变量和函数。这是与普通成员函数的最大区别,也是其应用所在。比如在对某一个类的对象进行计数时,计数生成多少个类的实例,就可以用到静态数据成员。在这里面,static既不是限定作用域的,也不是扩展生存期的作用,而是指示变量/函数在此类中的唯一性。这也是“属于一个类而不是属于此类的任何特定对象的变量和函数”的含义。因为它是对整个类来说是唯一的,因此不可能属于某一个实例对象的。(针对静态数据成员而言,成员函数不管是否是static,在内存中只有一个副本,普通成员函数调用时,需要传入this指针,static成员函数调用时,没有this指针)
  
  注意:
  
  不能将union成员声明为static的,然而,对于全局的匿名union,必须显式的声明为static。
  
  register关键字:
  
  只有函数参数和局部变量可被声明为register。意思是,在可能的情况下,该变量被存储在CPU寄存器中。register变量和auto变量一样,它的生命周期只维持在它所声明的块中。编译器并不赞成程序员指定register变量。实际的情况是,编译器会根据全局优化的需要自动决定是否采用register类型。
  
  extern关键字:
  
  extern用在声明语句中表示该对象或变量是在其它编译单元中(不能说是其它文件,因为有些文件,如头文件不是编译单元)定义的;如果用在定义语句中,表示该变量对外部可见。
  
  注意extern与#include作用的区别:
  
  例1,使用extern:
  
  //out.h
  
  int a = 10;
  
  //out.cpp
  
  #include "out.h"
  
  //example.cpp
  
  #include <iostream>
  
  using namespace std;
  
  extern int a;
  
  int _tmain(int argc, _TCHAR* argv[])
  
  {
  
  cout<<"a="<<a<<endl;
  
  system("pause");
  
  return 0;
  
  }
  
  编译链接流程:
  
  由于out.cpp文件引用了out.h文件,所以out.cpp文件的内容变成了int a = 10;,编译时生成编译单元out.obj(out.o),其中定义了变量a。example.cpp文件中出现了对a的引用cout<<"a="<<a<<endl;,但该文件中没有a的定义(extern int a;只是一个声明),而且包含的头文件中也没有,但是由于有声明extern int a;,表明a是在其它编译单元中定义的,编译不出错。最后链接器在编译单元out.obj(out.o)中找到a的定义,建立连接关系,生成最后的exe文件。该工程中有两个编译单元,最后链接成一个exe文件。
  
  例2,使用#include:
  
  //out.h
  
  int a = 20;
  
  //example.cpp
  
  #include "out.h"
  
  #include <iostream>
  
  using namespace std;
  
  int _tmain(int argc, _TCHAR* argv[])
  
  {
  
  cout<<"a="<<a<<endl;
  
  system("pause");
  
  return 0;
  
  }
  
  编译链接流程:
  
  example.cpp文件中出现了对a的引用cout<<"a="<<a<<endl;,但该文件中没有a的定义,但是包含的头文件out.h中有,这样在编译之前,头文件out.h展开到example.cpp文件中,相当于example.cpp文件中定义了a(int a = 20;),编译成功。最后链接器链接编译单元生成最后的exe文件。该工程中只有一个编译单元,最后链接成一个exe文件。

https://www.cnblogs.com/hanmlp52/category/1727703.html
https://www.cnblogs.com/hanmlp52/category/1727705.html
https://www.cnblogs.com/hanmlp52/category/1727708.html
https://www.cnblogs.com/hanmlp52/category/1727709.html
https://www.cnblogs.com/hanmlp52/category/1727712.html
https://www.cnblogs.com/hanmlp52/category/1727714.html
https://www.cnblogs.com/hanmlp52/category/1727716.html
https://www.cnblogs.com/hanmlp52/category/1727718.html
https://www.cnblogs.com/hanmlp52/category/1727720.html
https://www.cnblogs.com/hanmlp52/category/1727722.html
https://www.cnblogs.com/hanmlp52/category/1727724.html
https://www.cnblogs.com/hanmlp52/category/1727727.html
https://www.cnblogs.com/hanmlp52/category/1727729.html
https://www.cnblogs.com/hanmlp52/category/1727730.html
https://www.cnblogs.com/hanmlp52/category/1727733.html
https://www.cnblogs.com/hanmlp52/category/1727736.html
https://www.cnblogs.com/hanmlp52/category/1727737.html
https://www.cnblogs.com/hanmlp52/category/1727740.html
https://www.cnblogs.com/hanmlp52/category/1727743.html
https://www.cnblogs.com/hanmlp52/category/1727745.html
https://www.cnblogs.com/hanmlp52/category/1727748.html
https://www.cnblogs.com/hanmlp52/category/1727749.html
https://www.cnblogs.com/hanmlp52/category/1727751.html
https://www.cnblogs.com/hanmlp52/category/1727754.html
https://www.cnblogs.com/hanmlp52/category/1727756.html
https://www.cnblogs.com/hanmlp52/category/1727757.html
https://www.cnblogs.com/hanmlp52/category/1727761.html
https://www.cnblogs.com/hanmlp52/category/1727763.html
https://www.cnblogs.com/hanmlp52/category/1727765.html
https://www.cnblogs.com/hanmlp52/category/1727767.html
https://www.cnblogs.com/hanmlp52/category/1727769.html
https://www.cnblogs.com/hanmlp52/category/1727771.html
https://www.cnblogs.com/hanmlp52/category/1727774.html
https://www.cnblogs.com/hanmlp52/category/1727776.html
https://www.cnblogs.com/hanmlp52/category/1727778.html
https://www.cnblogs.com/hanmlp52/category/1727781.html
https://www.cnblogs.com/hanmlp52/category/1727783.html
https://www.cnblogs.com/hanmlp52/category/1727786.html
https://www.cnblogs.com/hanmlp52/category/1727787.html
https://www.cnblogs.com/hanmlp52/category/1727790.html
https://www.cnblogs.com/hanmlp52/category/1727793.html
https://www.cnblogs.com/hanmlp52/category/1727795.html
https://www.cnblogs.com/hanmlp52/category/1727797.html
https://www.cnblogs.com/hanmlp52/category/1727799.html
https://www.cnblogs.com/hanmlp52/category/1727801.html
https://www.cnblogs.com/hanmlp52/category/1727802.html
https://www.cnblogs.com/hanmlp52/category/1727806.html
https://www.cnblogs.com/hanmlp52/category/1727807.html
https://www.cnblogs.com/hanmlp52/category/1727808.html
https://www.cnblogs.com/hanmlp52/category/1727811.html
https://www.cnblogs.com/hanmlp52/category/1727813.html
https://www.cnblogs.com/hanmlp52/category/1727815.html
https://www.cnblogs.com/hanmlp52/category/1727817.html
https://www.cnblogs.com/hanmlp52/category/1727820.html
https://www.cnblogs.com/hanmlp52/category/1727821.html
https://www.cnblogs.com/hanmlp52/category/1727823.html
https://www.cnblogs.com/hanmlp52/category/1727825.html
https://www.cnblogs.com/hanmlp52/category/1727829.html
https://www.cnblogs.com/hanmlp52/category/1727830.html
https://www.cnblogs.com/hanmlp52/category/1727832.html
https://www.cnblogs.com/hanmlp52/category/1727834.html
https://www.cnblogs.com/hanmlp52/category/1727837.html
https://www.cnblogs.com/hanmlp52/category/1727838.html
https://www.cnblogs.com/hanmlp52/category/1727840.html
https://www.cnblogs.com/hanmlp52/category/1727842.html
https://www.cnblogs.com/hanmlp52/category/1727843.html
https://www.cnblogs.com/hanmlp52/category/1727845.html
https://www.cnblogs.com/hanmlp52/category/1727846.html
https://www.cnblogs.com/hanmlp52/category/1727848.html
https://www.cnblogs.com/hanmlp52/category/1727849.html
https://www.cnblogs.com/hanmlp52/category/1727850.html
https://www.cnblogs.com/hanmlp52/category/1727852.html
https://www.cnblogs.com/hanmlp52/category/1727853.html
https://www.cnblogs.com/hanmlp52/category/1727854.html
https://www.cnblogs.com/hanmlp52/category/1727856.html
https://www.cnblogs.com/hanmlp52/category/1727857.html
https://www.cnblogs.com/hanmlp52/category/1727858.html
https://www.cnblogs.com/hanmlp52/category/1727860.html
https://www.cnblogs.com/hanmlp52/category/1727861.html
https://www.cnblogs.com/hanmlp52/category/1727862.html
https://www.cnblogs.com/hanmlp52/category/1727864.html
https://www.cnblogs.com/hanmlp52/category/1727865.html
https://www.cnblogs.com/hanmlp52/category/1727866.html
https://www.cnblogs.com/hanmlp52/category/1727868.html
https://www.cnblogs.com/hanmlp52/category/1727869.html
https://www.cnblogs.com/hanmlp52/category/1727870.html
https://www.cnblogs.com/hanmlp52/category/1727872.html
https://www.cnblogs.com/hanmlp52/category/1727873.html
https://www.cnblogs.com/hanmlp52/category/1727875.html
https://www.cnblogs.com/hanmlp52/category/1727876.html
https://www.cnblogs.com/hanmlp52/category/1727877.html
https://www.cnblogs.com/hanmlp52/category/1727879.html
https://www.cnblogs.com/hanmlp52/category/1727880.html
https://www.cnblogs.com/hanmlp52/category/1727881.html
https://www.cnblogs.com/hanmlp52/category/1727883.html
https://www.cnblogs.com/hanmlp52/category/1727884.html
https://www.cnblogs.com/hanmlp52/category/1727885.html
https://www.cnblogs.com/hanmlp52/category/1727887.html
https://www.cnblogs.com/hanmlp52/category/1727888.html
https://www.cnblogs.com/hanmlp52/category/1727890.html
https://www.cnblogs.com/hanmlp52/category/1727891.html
https://www.cnblogs.com/hanmlp52/category/1727892.html
https://www.cnblogs.com/hanmlp52/category/1727894.html
https://www.cnblogs.com/hanmlp52/category/1727895.html
https://www.cnblogs.com/hanmlp52/category/1727897.html
https://www.cnblogs.com/hanmlp52/category/1727898.html
https://www.cnblogs.com/hanmlp52/category/1727899.html
https://www.cnblogs.com/hanmlp52/category/1727901.html
https://www.cnblogs.com/hanmlp52/category/1727902.html
https://www.cnblogs.com/hanmlp52/category/1727904.html
https://www.cnblogs.com/hanmlp52/category/1727905.html
https://www.cnblogs.com/hanmlp52/category/1727907.html
https://www.cnblogs.com/hanmlp52/category/1727908.html
https://www.cnblogs.com/hanmlp52/category/1727909.html
https://www.cnblogs.com/hanmlp52/category/1727911.html
https://www.cnblogs.com/hanmlp52/category/1727912.html
https://www.cnblogs.com/hanmlp52/category/1727913.html
https://www.cnblogs.com/hanmlp52/category/1727915.html
https://www.cnblogs.com/hanmlp52/category/1727916.html
https://www.cnblogs.com/hanmlp52/category/1727918.html
https://www.cnblogs.com/hanmlp52/category/1727919.html
https://www.cnblogs.com/hanmlp52/category/1727920.html
https://www.cnblogs.com/hanmlp52/category/1727922.html
https://www.cnblogs.com/hanmlp52/category/1727923.html
https://www.cnblogs.com/hanmlp52/category/1727925.html
https://www.cnblogs.com/hanmlp52/category/1727926.html
https://www.cnblogs.com/hanmlp52/category/1727927.html
https://www.cnblogs.com/hanmlp52/category/1727929.html
https://www.cnblogs.com/hanmlp52/category/1727930.html
https://www.cnblogs.com/hanmlp52/category/1727932.html
https://www.cnblogs.com/hanmlp52/category/1727933.html
https://www.cnblogs.com/hanmlp52/category/1727934.html
https://www.cnblogs.com/hanmlp52/category/1727936.html
https://www.cnblogs.com/hanmlp52/category/1727937.html
https://www.cnblogs.com/hanmlp52/category/1727940.html
https://www.cnblogs.com/hanmlp52/category/1727941.html
https://www.cnblogs.com/hanmlp52/category/1727942.html
https://www.cnblogs.com/hanmlp52/category/1727943.html
https://www.cnblogs.com/hanmlp52/category/1727945.html
https://www.cnblogs.com/hanmlp52/category/1727946.html
https://www.cnblogs.com/hanmlp52/category/1727948.html
https://www.cnblogs.com/hanmlp52/category/1727949.html
https://www.cnblogs.com/hanmlp52/category/1727951.html
https://www.cnblogs.com/hanmlp52/category/1727952.html
https://www.cnblogs.com/hanmlp52/category/1727953.html
https://www.cnblogs.com/hanmlp52/category/1727955.html
https://www.cnblogs.com/hanmlp52/category/1727956.html
https://www.cnblogs.com/hanmlp52/category/1727959.html
https://www.cnblogs.com/hanmlp52/category/1727960.html
https://www.cnblogs.com/hanmlp52/category/1727962.html
https://www.cnblogs.com/hanmlp52/category/1727963.html
https://www.cnblogs.com/hanmlp52/category/1727965.html
https://www.cnblogs.com/hanmlp52/category/1727966.html
https://www.cnblogs.com/hanmlp52/category/1727967.html
https://www.cnblogs.com/hanmlp52/category/1727969.html
https://www.cnblogs.com/hanmlp52/category/1727970.html
https://www.cnblogs.com/hanmlp52/category/1727971.html
https://www.cnblogs.com/hanmlp52/category/1727973.html
https://www.cnblogs.com/hanmlp52/category/1727974.html
https://www.cnblogs.com/hanmlp52/category/1727976.html
https://www.cnblogs.com/hanmlp52/category/1727977.html
https://www.cnblogs.com/hanmlp52/category/1727978.html
https://www.cnblogs.com/hanmlp52/category/1727980.html
https://www.cnblogs.com/hanmlp52/category/1727981.html
https://www.cnblogs.com/hanmlp52/category/1727982.html
https://www.cnblogs.com/hanmlp52/category/1727984.html
https://www.cnblogs.com/hanmlp52/category/1727985.html
https://www.cnblogs.com/hanmlp52/category/1727986.html
https://www.cnblogs.com/hanmlp52/category/1727988.html
https://www.cnblogs.com/hanmlp52/category/1727989.html
https://www.cnblogs.com/hanmlp52/category/1727991.html
https://www.cnblogs.com/hanmlp52/category/1727992.html
https://www.cnblogs.com/hanmlp52/category/1727993.html
https://www.cnblogs.com/hanmlp52/category/1727995.html
https://www.cnblogs.com/hanmlp52/category/1727996.html
https://www.cnblogs.com/hanmlp52/category/1727998.html
https://www.cnblogs.com/hanmlp52/category/1727999.html
https://www.cnblogs.com/hanmlp52/category/1728000.html
https://www.cnblogs.com/hanmlp52/category/1728002.html
https://www.cnblogs.com/hanmlp52/category/1728003.html
https://www.cnblogs.com/hanmlp52/category/1728005.html
https://www.cnblogs.com/hanmlp52/category/1728006.html
https://www.cnblogs.com/hanmlp52/category/1728007.html
https://www.cnblogs.com/hanmlp52/category/1728009.html
https://www.cnblogs.com/hanmlp52/category/1728010.html
https://www.cnblogs.com/hanmlp52/category/1728012.html
https://www.cnblogs.com/hanmlp52/category/1728013.html
https://www.cnblogs.com/hanmlp52/category/1728015.html
https://www.cnblogs.com/hanmlp52/category/1728016.html
https://www.cnblogs.com/hanmlp52/category/1728018.html
https://www.cnblogs.com/hanmlp52/category/1728019.html
https://www.cnblogs.com/hanmlp52/category/1728020.html
https://www.cnblogs.com/hanmlp52/category/1728022.html
https://www.cnblogs.com/hanmlp52/category/1728023.html
https://www.cnblogs.com/hanmlp52/category/1728025.html
https://www.cnblogs.com/hanmlp52/category/1728026.html
https://www.cnblogs.com/hanmlp52/category/1728027.html
https://www.cnblogs.com/hanmlp52/category/1728029.html
https://www.cnblogs.com/hanmlp52/category/1728030.html
https://www.cnblogs.com/hanmlp52/category/1728032.html

猜你喜欢

转载自www.cnblogs.com/hanmlp52/p/12723700.html