一:程序的存儲類型: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; }