一:程序的存储类型:ios
(1):extern:外部存储:函数
1:extern int n:他表示变量n不在本文件中分配内存,而在程序的其余文件中分配内存,分配内存(即变量的定义).
2:默认的函数声明或定义老是extern的。
extern void hanshu();
extern void hanshu2();
3:带extern的变量或函数表示的是声明而不是定义.同一个变量不能屡次定义.//源文件.cppoop
#include <iostream> using namespace std; /* run this program using the console pauser or add your own getch, system("pause") or input loop */ void hanshu(); //函数的声明通常在源文件的开始位置。 void hanshu2(); /*等价于: extern void hanshu(); extern void hanshu2(); */ int n = 0; void hanshu() //定义函数 {
hanshu2(); } int main(int argc, char** argv) { n = 3; cout<<"调用函数前n="<<n<<endl; //输出的是3; hanshu(); cout<<"调用hanshu()函数后n="<<n<<endl; //输出的是8 return 0; }
//extern.cpp
#include <iostream> using namespace std; /* run this program using the console pauser or add your own getch, system("pause") or input loop */ extern int n; //这里不是定义n而是告诉编译器,n这个变量是外部的.他的内存空间在外部。 void hanshu2() //定义函数 { n = 8; } /* extern:外部存储: 1:extern int n:他表示变量n不在本文件中分配内存,而在程序的其余文件中分配内存,分配内存(即变量的定义). 2:默认的函数声明或定义老是extern的。 extern void hanshu(); extern void hanshu2(); 3:带extern的变量或函数表示的是声明而不是定义.同一个变量不能屡次定义. */
(2):静态存储类型:this
1:静态全局变量:在全局变量前加一个static,使该变量只在这个源文件中能够,称之为全局静态变量。spa
//源文件.cpp #include <iostream> using namespace std; int n = 0; //全局变量. void fn(); //声明函数 int main() { n = 20; cout<<n<<endl; //20 fn(); cout<<"调用函数fn()后n="<<endl; //20 return 0; }
//静态类型 #include <iostream> using namespace std; static int n; //默认初始化为0,这里指明了n不是外部的变量,他的空间在本文件中分配,不为外部使用。 void fn() { n++; cout<<"在非源文件中定义了函数fn()输出n="<<endl;//输出的是1而不是21说明两个变量是在不一样的存储空间中。 }
2:静态函数:在文件做用域下声明的inline函数默认为static存储类型,在文件做用域下声明的const的常量(常量一旦定义就不能在修改。)也默认为static存储类型.若是加上extern则为外部变量.code
二:C++的做用域范围分为局部做域(块做用域)、函数做用域、函数原型做用域、文件做用域和类做用域。blog
(1):局部做用域:当标识符的声明出如今由一对大括号括起来的一段程序(块)内时,该标识符的做用域从声明点开始,到块结束为止。生命周期
//源文件.cpp #include <iostream> using namespace std; void hanshu(); //函数的声明 void hanshu2(); int f(); void fn(int n); int mian() { hanshu(); hanshu2(); }
//局部做用域.cpp
#include <iostream> using namespace std; int f() { return 1; } void hanshu() { if(int i = 10) //i的做用域今后if()括号内定义开始 i = 2 * i; else i = 100; //i的做用域到此结束. //cout<<i<<endl; //报错 i 无定义 } void hanshu2() { switch(int C = f()) //C做用域的开始处. { case 1: cout<<C<<endl; } //i的做用域到此结束. // cout<<i<<endl; //报错 i 无定义 } void fn(int n) { if(n>5) int i = n; //整型i的做用域今后开始,到此结束 else double i = n; //double型i 的做用域今后开始,到此结束 //cout<<i<<endl; //报错i 无定义 }
三:函数做用域:内存
1:文件做用域:静态全局变量,静态函数都是文件做用域的,因此,文件做用域即为静态全局的.例如cout和cin都是在头文件iostream.h的头文件做用域中声明的标识符,这两个标识符的做用域延伸到嵌入iostream.h的源文件中。ci
main.cpp
#include <iostream> using namespace std; /*文件做用域是在全部函数定义以外说明的,其做用域从说明点开始,一直延伸到源文件结束*/ /* run this program using the console pauser or add your own getch, system("pause") or input loop */ void fn(int a,int b); //函数原型声明 int number; static int value; //静态全局变量;是文件做用域 int main(int argc, char** argv) { int a = 1; int b = 0; switch(a) //【重点注意】 { // int b = 0; 在switch中定义的变量b没法到达case1中. case 1: b++; } fn(2,3); return 0; }
函数原型做用域.cpp
#include <iostream> using namespace std; /* */ void fn(int a,int b) //函数原型定义. { a = 20; cout<<a<<endl; }
四:生命期:也叫生存期,生命期与存储区域密切相关,存储区域主要有代码区,数据区,栈区,堆区。对应的生命周期为静态生命期,局部生命期和动态生命期。
main.cpp
#include <iostream> using namespace std; void fn(); /*文件做用域是在全部函数定义以外说明的,其做用域从说明点开始,一直延伸到源文件结束*/ /* run this program using the console pauser or add your own getch, system("pause") or input loop */ int n = 20; int main(int argc, char** argv) { double m = 3.8;//局部变量,不能在fn函数做用域块中使用 cout<<"n = "<<n<<endl; fn(); return 0; }
fn.cpp
#include <iostream> using namespace std; /* */ void fn() //函数原型定义. { //被调用函数不能享有使用调用函数中数据的权力。 extern int n; //cout<<"m = "<<m<<endl; cout<<"n = "<<n<<endl; }