C++Storage Class
The storage class defines the scope (visibility) and life cycle of variables/functions in C + + programs. These specifiers are placed before the type they are decorated in. The storage classes available in C + + programs are listed below:
- Auto
- Register
- Static
- extern
- mutable
- Thread_local (C++11)
Starting with C + + 11, the Auto keyword is no longer a C + + storage class descriptor, and the Register keyword is deprecated.
Auto Storage class
Since C + + 11, theauto keyword has been used in two cases: when declaring a variable, the type of the variable is automatically inferred from the initialization expression, and a placeholder for the function's return value when the function is declared.
The Auto keyword in the c++98 standard is used for the declaration of automatic variables, but this usage has been removed in c++11 because of the minimal use and redundancy.
The type of the declared variable is automatically inferred based on the initialization expression, such as:
1 auto f=3.14; // Double 2 Auto S ("Hello"); // Const char* 3 New // int* 4 Auto x1 = 5, x2 = 5.0, x3= ' R '; // error, must be initialized to the same type
Register Storage Class
The register storage class is used to define local variables that are stored in registers rather than in RAM. This means that the maximum size of a variable is equal to the size of the register (usually a word), and it cannot be applied to a unary ' & ' operator (because it has no memory location).
{ Register int miles;}
Registers are used only for variables that require quick access, such as counters. It should also be noted that defining ' register ' does not imply that the variable will be stored in the register, which means that the variable may be stored in the register, depending on the hardware and implementation constraints.
Static Storage class
The static storage class instructs the compiler to maintain the existence of local variables for the lifetime of the program without having to create and destroy each time it enters and leaves the scope. Therefore, using the static modifier local variable can maintain the value of a local variable between function calls.
The static modifier can also be applied to global variables. When static modifies a global variable, the scope of the variable is limited to the file in which it is declared.
In C + +, when static is used on a class data member, it causes only one copy of that member to be shared by all objects of the class.
Instance
1 #include <iostream>2 3 //Function declaration4 void func (void);5 6 StaticIntCount= 10;/*Global Variables*/7 8 int main ()9 {Ten while(Count--) One { A func (); - } - return0; the } - //function Definition - void func (void) - { + Staticint i = 5;//Local static variables -i++; +Std::cout << "Variable i =" <<i; AStd::cout << ", variable count is" <<Count<< std::Endl; at}
Execution Result:
The variable i is 6, the variable count is 9 variable i is 7, the variable count is 8 variable i is 8, the variable count is 7 variable i is 9, the variable count is 6 variable i is 10, the variable count is 5 variable i is 11, the variable count is 4 variable i is 12, the variable count is 3 variable i is 13, the variable count is 2 variable i is 14, the variable count is 1 variable i is 15, and the variable count is 0
extern storage class
The extern storage class is used to provide a reference to a global variable, and global variables are visible to all program files. When you use ' extern ', the variable name is pointed to a previously defined storage location for variables that cannot be initialized.
When you have multiple files and you define a global variable or function that you can use in other files, you can use extern in other files to get a reference to a defined variable or function. As you can understand,extern is used to declare a global variable or function in another file.
The extern modifier is typically used when two or more files share the same global variable or function, as follows:
Main.cpp
1 # include <iostream> 2 3 Count ; 4 extern void Write_extern (); 5 6 int main () 7 { 8 count = 5; 9 Write_extern (); Ten }
Support.cpp
1 # include <iostream> 2 3 Count ; 4 5 void Write_extern (void) 6 {7 Count << std::Endl; 8 }
Here, the extern keyword in the second file is used to declare the count that has been defined in the first file main.cpp. Now, compile these two files as follows:
$ g++ main.cpp Support.cpp-o Write
This produces the write executable, which attempts to execute write, which produces the following results:
1 $./Write2Count is 5
Mutable Storage Class
The mutable specifier applies only to objects of the class, which is explained at the end of this tutorial. It allows members of an object to override constants. That is, mutable members can be modified through the const member function.
Thread_local Storage Class
A variable declared with the thread_local specifier can be accessed only on the thread on which it was created. Variables are created when the thread is created and destroyed when the thread is destroyed. Each thread has its own copy of the variable.
The thread_local specifier can be merged with Static or extern.
Thread_local can be applied only to data declarations and definitions, and thread_local cannot be used for function declarations or definitions.
The following shows the variables that can be declared as thread_local:
1thread_local int x;//Global variables under namespaces2 classX3 {4 StaticThread_local std::stringS//static member variable of class5 };6 StaticThread_local std::stringX::s;//x::s needs to be defined.7 8 void foo ()9 {TenThread_local Std::vector<int> v;//Local Variables One}
24. C + + storage class