Introduction :
A storage classes in c++ is generally used to define the scope (visibility) and life-time of the particular variables and/or particular functions that are within a C++ Program. These specifiers often precede the type that they are supposed to modify. Below are the storage classes, which are present there to be used in a C++ Program
1.auto
2.register
3.static
4.extern
5.mutable
The auto Storage Class:
The auto storage class is often times considered to be the default storage class available for all of the local variables present in the program.
The register Storage Class:
The register storage class is generally used for defining the local variables that are supposed to be stored in a register instead of RAM. This however means that the variable generally has a maximum size which is equal to the register size (this is usually one word) and they can’t have the unary ‘&’ operator which could get applied to it (as it cannot be located using a memory location).
The static Storage Class:
The static storage class gives instructions to the compiler for it to keep a local variable in existence while the life-time of the program in the place of creating and destroying it every time when it will come into and go out of the scope. That’s why, making those local variables static will allow them for maintaining their values in between the function calls.
The extern Storage Class:
The extern storage class generally comes into use for giving a reference of a global variable that would be visible to ALL the present program files. When someone uses ‘extern’ the variable will not be to get initialized as all it could to is point the variable name at any storage location that has already been defined prior. The extern modifier is considered to be the most general in use when there comes the existence of two or more files that are sharing the same global variables or functions as explained in the following program.
storage classes in c++
Here, extern is the keyword that is being used for declaring the count in any other file. Here, compiling these two files below−
$g++ rimain.cpp risupport.cpp -o write
This is going to produce write executable program, you can try to execute write and you are allowed to check the result as given below −
$./write
21
The mutable Storage Class:
The mutable specifier generally applies only to the objects of the class. It generallly allows a member of a given object so that they can override const member function. This ultimately means, any mutable member is allowed to be modified by a const member function.