C++ storage classes storage classes define the scope (visibility) and life cycle of variables/functions in C++ programs. These specifiers precede the type they modify. The following lists the storage classes available in C++ programs:

  • auto
  • register
  • static
  • extern
  • mutable

Thread_local (C++11) starting with C++ 17, the auto keyword is no longer the C++ storage class specifier, and the register keyword is deprecated.

Auto storage class since C++ 11, the auto keyword is used in two cases: when declaring a variable, the type of the variable is automatically inferred from the initialization expression, and when declaring a function, a placeholder for the value returned by the function.

The C++98 standard uses the auto keyword for automatic variable declarations, but it has been removed from C++11 due to its rare and redundant use.

Automatically infer the type of the declared variable from the initialization expression, for example:

Auto f = 3.14; //double auto s("hello"); //const char* auto z = new auto(9); // int* auto x1 = 5, x2 = 5, x3='r'; // Error, must be initialized to the same typeCopy the code

The register storage class

The Register storage class is used to define local variables stored in registers rather than RAM. This means that the maximum size of the variable is equal to the size of the register (usually a word), and the unary ‘&’ operator cannot be applied to it (because it has no memory location).

{
   register int  miles;
}
Copy the code

Registers are used only for variables that need quick access, such as counters. It should also be noted that defining ‘register’ does not mean that variables will be stored in registers, it means that variables may be stored in registers, depending on hardware and implementation limitations.

The static storage class

The static storage class instructs the compiler to keep a local variable alive for the life of the program, without needing to create and destroy it every time it enters and leaves scope. Therefore, using static to modify local variables preserves the value of the local variable between function calls.

The static modifier can also be applied to global variables. When static modifies a global variable, it limits the scope of the variable to the file in which it was declared.

In C++, when static is used on a class data member, it results in only one copy of that member being shared by all objects of the class.

#include <iostream> // void func(void); static int count = 10; /* global */ int main() {while(count--) {func(); } return 0; Void func(void) {static int I = 5; // local static variable i++; STD ::cout << "variable I is" << I; STD ::cout << ", variable count is "<< count << STD ::endl; }Copy the code

When the above code is compiled and executed, it produces the following results:

If I was 6 and count was 9 and I was 7 and count was 8 and I was 8 and I was 9 and count was 6 and I was 10, So if we have variable count of 5 we have variable I of 11, variable count of 4 variable I of 12, variable count of 3 variable I of 13, variable count of 2 variable I of 14, The variable count is 1 and the variable I is 15 and the variable count is 0Copy the code

Extern storage class

The extern storage class is used to provide a reference to a global variable that is visible to all program files. When you use ‘extern’, for variables that cannot be initialized, the variable name points to a previously defined storage location.

When you have multiple files and define a global variable or function that can be used in other files, you can use extern in other files to get a reference to the defined variable or function. Extern is used to declare a global variable or function in a separate file.

The extern modifier is usually used when two or more files share the same global variable or function, as follows:

The first file: main.cpp

#include <iostream>
 
int count ;
extern void write_extern();
 
int main()
{
   count = 5;
   write_extern();
}
Copy the code

The second file: support-cpp

#include <iostream>
 
extern int count;
 
void write_extern(void)
{
   std::cout << "Count is " << count << std::endl;
}
Copy the code

In this case, the extern keyword in the second file is used to declare the count already defined in the first file main.cpp. Now compile the two files as follows:

$ g++ main.cpp support.cpp -o write

This produces the write executable, which attempts to execute write, and produces the following results:

$ ./write Count is 5

Mutable storage class

The mutable modifier applies only to class objects, as we’ll explain at the end of this tutorial. It allows members of an object to replace constants. That is, a mutable member can be modified by const member functions.

Thread_local storage class

A variable declared using the thread_local specifier is only accessible on the thread on which it is 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 combined with static or extern.

You can apply thread_local only to data declarations and definitions; thread_local cannot be used for function declarations or definitions.

The following shows variables that can be declared as thread_local:

thread_local int x; // global variables in the namespace

thread_local int x; Class X {static thread_local STD ::string s; // Class static member variable}; static thread_local std::string X::s; Void foo() {thread_local STD ::vector<int> v; // local variable}Copy the code

Recently sorted out a number of suitable for small white people entry learning materials, including e-books, tools, installation packages and so on click to join my official group [free] access to a full set of C language proficient learning materials. (“▔□▔)/ If you are new to C, you can share a full set of C language mastery learning materials to you. If you are learning C++, you can help you to learn the DIRECTION of C++ game development, so that you can think clearly and learn more effectively