current position:Home>C / C + +: static keyword summary

C / C + +: static keyword summary

2022-01-26 22:53:35 xMathematics

1. Global static variables

Add keywords before global variables static, Global variable is defined as a global static variable .

Static storage area , It's been around for the entire duration of the program .

initialization : Uninitialized global static variables are automatically initialized to 0( The value of the automatic object is arbitrary , Unless it's explicitly initialized );

Scope : Global static variables are not visible outside of the declared file , Exactly where it's defined , To the end of the file .

2. Local static variable

Add the keyword before the local variable static, A local variable becomes a local static variable .

Location in memory : Static storage area

initialization : Uninitialized global static variables are automatically initialized to 0( The value of the automatic object is arbitrary , Unless it's explicitly initialized );

Scope : Scope is still local , When the function or block that defines it ends , Scope end . But after the administration static variables leave the scope , Not destroyed , It's still in memory , It's just that we can't access it anymore , Until the function is called again , And the value doesn't change ;

3. Static functions

Add before function return type static, Functions are defined as static functions . Functions are defined and declared by default extern Of , But the static function is only visible in the file that declares it , Can't be used by other documents .

Function implementation use static modification , So this function can only be used in this cpp Internal use , It's not like anything else cpp The function with the same name in causes a conflict ;

warning: Don't declare... In the header file static Global function of , Not in cpp The internal statement is not static Global function of , If you want to be in more than one cpp The function is repeated in , Just put its statement in the header file , otherwise cpp The internal statement needs to add static modification ;

4. Static member of class

In class , Static members can share data among multiple objects , And using static data members doesn't break the principle of hiding , That is to ensure the safety . therefore , Static members are members shared among all objects of a class , Not a member of an object . For multiple objects , Static data members store only one place , For all objects

5. Static function of class

Static member functions are the same as static data members , They are all static members of the class , They are not members of the object . therefore , References to static members do not need to use object names .
In the implementation of static member function, the non static member described in the class cannot be directly referenced , You can refer to the static members described in the class ( This is very important ). If you want to reference a non static member in a static member function , You can reference... Through objects . It can be seen from it that , Call the static member function in the following format :< Class name >::< Static member function name >(< Parameter table >);

copyright notice
author[xMathematics],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/01/202201262253329319.html

guess what you like

Random recommended