C++ storage class


Storage classes define the scope (visibility) and life cycle of variables/functions in a C++ program. These specifiers are placed before the type they modify. The storage classes available in C++ programs are listed below:

  • auto

  • register

  • static

  • extern

  • mutable

auto storage class

auto The storage class is the default storage class for all local variables.

{
   int mount;
   auto int month;
}

The above example defines two variables with the same storage class. Auto can only be used within functions, that is, auto can only modify local variables.

register Storage Class

register The storage class is used to define local variables that are stored in registers instead of RAM. This means that the maximum size of a 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;
}

Registers are only used for variables that need to be accessed quickly, such as counters. It should also be noted that defining 'register' does not mean that the variable will be stored in a register, it means that the variable may be stored in a register, depending on hardware and implementation limitations.

static storage class

static The storage class instructs the compiler to maintain the existence of a local variable during the life cycle of the program without requiring it to enter and leave the function each time. Domains are created and destroyed. Therefore, using static to modify a local variable maintains its value between function calls.

static modifier can also be applied to global variables. When static is used to modify 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 will cause only one copy of the member to be shared by all objects of the class.

#include <iostream>
 
// 函数声明 
void func(void);
 
static int count = 10; /* 全局变量 */
 
int main()
{
    while(count--)
    {
       func();
    }
    return 0;
}
// 函数定义
void func( void )
{
    static int i = 5; // 局部静态变量
    i++;
    std::cout << "变量 i 为 " << i ;
    std::cout << " , 变量 count 为 " << count << std::endl;
}

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

变量 i 为 6 , 变量 count 为 9
变量 i 为 7 , 变量 count 为 8
变量 i 为 8 , 变量 count 为 7
变量 i 为 9 , 变量 count 为 6
变量 i 为 10 , 变量 count 为 5
变量 i 为 11 , 变量 count 为 4
变量 i 为 12 , 变量 count 为 3
变量 i 为 13 , 变量 count 为 2
变量 i 为 14 , 变量 count 为 1
变量 i 为 15 , 变量 count 为 0

extern storage class

extern Storage class is used for Provides 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 the defined variable or function citation. It can be understood that extern is used to declare a global variable or function in another file.

The extern modifier is usually used when there are two or more files sharing the same global variable or function, as shown below:

First file: main.cpp

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

Second file: support.cpp

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

Here, the extern keyword in the second file is used to declare that it is already in the first file main.cpp count defined in . Now, compile these two files as follows:

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

This will produce the write executable program. Try executing write and it will produce the following results:

$ ./write
Count is 5

mutable storage class

mutable specifier only applies to objects of the class, which will be explained at the end of this tutorial. It allows members of an object to be substituted for constants. That is, mutable members can be modified through const member functions.