one、Process-oriented designstatic 1、Static global variables Before global variables,Add keywordsstatic,该变量就被定义成为one个Static global variables。我们先举one个Static global variables的例子,as follows: //Example 1#include <>void fn();static int n; //Define static global variable void main()void fn() static global variable has the following characteristics:This variable allocates memory in the global data area; 未经初始化的Static global variablesIt will be automatically initialized to0(The value of the automatic variable is random,Unless it is explicitly initialized); Static global variablesexist声明它的整个文件都是可见的,And it is invisible outside the file; All static variables all allocate memory in the global data area,Including the static local variables to be mentioned later。对于one个完整的程序,exist内存middle的分布情况as follows图: Code area Global data area Stack area Stack area one般程序的由new产生的动态数据存放existStack area,函数内部的自动变量存放existStack area。自动变量one般会随着函数的退出而释放空间,Static data(Even the static inside the function Local variables in state)也存放existGlobal data area。Global data area的数据并不会因为函数的退出而释放空间。Careful readers may find,Example 1The code in the code static int n; //Define static global variablesChange to int n; //Define global variablesThe program still runs normally。 indeed,Defining global variables can enable sharing of variables in files,但定义Static global variables还有以下好处: Static global variables不能被其它文件所use; Variables with the same name can be defined in other files,No conflicts; 您可以将上述示例代码Change toas follows: //Example 2//File1#include <>void fn();static int n; //Define static global variable void main()//File2#include <>extern int n;void fn() Compile and run Example 2, and you will find that the above code can be compiled separately, but there is an error during runtime. Try to putstatic int n; //Define static global variablesChange to int n; //Define global variablesCompile and run the program again,细心体会全局变量andStatic global variables的区别。 2、静Local variables in state Before local variables,Add keywordsstatic,该变量就被定义成为one个静Local variables in state。 我们先举one个静Local variables in state的例子,as follows: //Example 3#include <>void fn();void main()void fn() generally,exist函数体内定义了one个变量,Whenever the program runs to the statement, the stack memory will be allocated to the local variable.。But as the program exits the function body,The system will reclaim the stack memory,Local variables also fail accordingly。 But sometimes we need to save the value of the variable between calls。generally的想法是定义one个全局变量来accomplish。但this样one来,Variables no longer belong to the function itself,No longer controlled by functions,Causes inconvenience to program maintenance。 静Local variables in state正好可以解决this个问题。静Local variables in state保存existGlobal data area,Instead of saving it in the stack,每次的value保持到下one次调use,Until the next time you assign new values。 静Local variables in state有以下特点: This variable allocates memory in the global data area; 静Local variables in stateexist程序执行到该对象的声明处时被首次初始化,That is, the function calls will no longer be initialized; 静Local variables in stateone般exist声明处初始化,If not explicitly initialized,It will be automatically initialized to0; 它始终驻留existGlobal data area,Until the program runs。But its scope is local scope,When the function or statement block that defines it ends,Its scope ends; 3、Static functions Add the function's return type beforestaticKeywords,函数Right now被定义为Static functions。Static functions与普通函数不同,It can only be seen in the file that declares it,Cannot be used by other files。 Static functions的例子: //Example 4#include <>static void fn();//Declare static function void main()void fn()//Define static function benefits of defining static functions:Static functions不能被其它文件所use; Functions with the same name can be defined in other files,No conflicts; two、Object-orientedstaticKeywords(In the classstaticKeywords) 1、Static datamember exist类内数据member的声明前Add keywordsstatic,该数据member就是类内的Static datamember。先举one个Static datamember的例子。 //Example 5#include <>class Myclass;int Myclass::Sum=0;//Define and initialize static data member Myclass::Myclass(int a,int b,int c)void Myclass::GetSum()void main() It can be seen that static data members have the following characteristics:对于非Static datamember,Each class object has its own copy。而Static datamember被当作是类Members of。No matter how many objects of this class are defined,Static datamemberexist程序middle也只有one份拷 cowry,Shared access by all objects of this type。That is to say,Static datamember是该类的所有对象所共有的。For multiple objects of this class,Static datamember只分配one次内存,For all objects use。so,Static datamember的value对每个对象都是one样的,Its value can be updated; Static datamember存储existGlobal data area。Static datamember定义时要分配空间,so不能exist类声明middle定义。existExample 5middle,Statementint Myclass::Sum=0;是定义Static datamember; Static datamemberand普通数据memberone样遵从public,protected,privateAccess Rules; 因为Static datamemberexistGlobal data area分配内存,All objects belonging to this class share,so,It does not belong to a specific class object,exist没有产生类对象时其作use域就可见,Right nowexist没有产生类的实例时,We can operate it; Static datamember初始化与one般数据member初始化不同。Static datamember初始化的格式为: <Data Type><Class Name>::<Static datamember名>=<value> 类的Static datamember有两种访问形式: <Class object name>.<Static datamember名> or <Class type name>::<Static datamember名> 如果Static datamember的访问权限允许的话(Right nowpublicMembers of),可exist程序middle,按上述格式来引useStatic datamember ; Static datamember主要useexist各个对象都有相同的某项属性的时候。比如对于one个存款类,The interest rate for each instance is the same。so,应该把利息设为存款类的Static datamember。this There are two benefits,第one,No matter how many deposit class objects are defined,利息数据member都共享分配existGlobal data area的内存,so节省存储空间。第two,one旦利息需要改变时,只要改变one次, Then all the interest rates for all deposit objects have changed; Compared with global variables,使useStatic datamember有两个优势: Static datamember没有进入程序的全局名字空间,因此不存exist与程序middle其它全局名字冲突的可能性; It can realize information hiding。Static datamember可以是privatemember,Global variables cannot; 2、静态member函数 与Static datamemberone样,我们也可以创建one个静态member函数,它为类的全部服务而不是为某one个类的具体对象服务。静态member函数与Static datamemberone样,It's all inside the class accomplish,属于类定义的one部分。 普通Members of函数one般都隐含了one个thispointer,thispointerrefer to向类的对象本身,因为普通member函数总是具体的属于某个类的具体对象的。generally情况下,this It is default。Like functionfn()Actually it isthis->fn()。But compared with normal functions,静态member函数由于不是与任何的对象相联系,So it does not havethisrefer to Needle。从this个意义上讲,它无法访问属于类对象的非Static datamember,也无法访问非静态member函数,它只能调use其余的静态member函数。 下面举个静态member函数的例子。 //Example 6#include <>class Myclass;int Myclass::Sum=0;//Define and initialize static data member Myclass::Myclass(int a,int b,int c)void Myclass::GetSum() //Implementation of static member functions void main() 关于静态member函数,It can be summarized as follows: 出现exist类体外的函数定义不能refer to定Keywordsstatic; 静态member之间可以相互访问,包括静态member函数访问Static datamemberand访问静态member函数; 非静态member函数可以任意地访问静态member函数andStatic datamember; 静态member函数不能访问非静态member函数and非Static datamember; Since there is nothispointer的额外开销,因此静态member函数与类的全局函数相比速度上会有少许的增长; 调use静态member函数,可以usemember访问操作符(.)and(->)为one个类的对象orrefer to向类对象的pointer调use静态member函数,也可以直接使useas follows格式: <Class Name>::<静态member函数名>(<Parameter table>) 调use类的静态member函数。