Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

The storage class for C+


May 11, 2021 C++


Table of contents


The storage class for C+

The storage class defines the range (visibility) and lifecycle of variables/functions in the C++ program. T hese descriptors are placed before the type they decorate. The storage classes available in the C++ program are listed below:

  • auto
  • register
  • static
  • extern
  • mutable

auto storage class

The auto 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

The register storage class is used to define local variables stored in registers instead of RAM. This means that the maximum size of the variable is equal to the size of the register (usually a word) and cannot be applied to it by the one-dollar 'and'operator' (because it has no memory location).

{
   register int  miles;
}

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

static storage class

The static storage class instructs the compiler to maintain the existence of local variables throughout the life cycle of the program without having to create and destroy them each time it enters and leaves the scope. Therefore, using static to decorate local variables keeps the value of local variables between function calls.

Static modifiers can also be applied to global variables. When static decorates a global variable, the scope of the variable is limited to the file that declares it.

When static is used on a class data member in C++, 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; /* 全局变量 */
 
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

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 you 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 a defined variable or function. Understandably, extern is used to declare a global variable or function in another file.

Extern modifiers are typically used when two or more files share the same global variable or function, as follows:

First file: main .cpp

#include <iostream>
 
int count ;
extern void write_extern();
 
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 count that has been defined .cpp the first file main. Now, compile the two files as follows:

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

This results in a write executable, an attempt to execute a write, and it produces the following results:

$ ./write
Count is 5

Mutable storage class

The mutable descriptor applies only to objects of the class, which will be covered at the end of this tutorial. I t allows members of an object to override constants. That is, the mutable member can be modified by the const member function.