Storage classes are used to manage the lifetime and scope of the variables. A variable can have only one storage class. There are 4 types of the storage classes:
The keyword “mutable” can be considered as storage class too, but it’s related to the Object Oriented Programming and it will be discussed later in the “C++ Object Oriented” Section.
By default C++ uses automatic storage. The scope of an automatic variable is within the block where it is declared. The Automatic variable is stored in the program’s stack. In the earlier versions of C++ you could write automatic storage type explicitly as shown below:
auto int i;
Now in newer versions of C++, it will generate an error, because
autokeyword is mostly used for other purposes such as ‘Type Deduction’ of a variable. The auto keyword directs the compiler to use the initialization expression of a declared variable to deduce its type. The auto keyword is a simple way to declare a variable that has a complicated type. For example, you can use auto to declare a variable where the initialization expression involves templates, pointers to functions, or pointers to members.
auto i = 30; // compiler will automatically detect the type of i as int
static storage can be used when you want to keep the value of local variable during the whole life of the program. The
static variable once created will retain the value even if you exit the code block and enter it again. The
static variable can be declared in the following way:
static double width;
register storage is used if you want faster access to the variable. The variable will get stored in the CPU register instead of RAM (memory). The other properties of the register storage are same as of automatic storage, except the location, where the variable is stored. Here we are just requesting the compiler to store register variable in CPU’s register but the final decision is upto compiler where it wants to store. Register variable can be declared in the following way:
register int index;
The external storage is used when you want to access a global variable/object which is defined in another file. We use
extern to specify the compiler that the current object is defined somewhere else in other file and not in current file. You can’t initialize a variable when you use extern specifier. But we can modify this variable. Let’s look for an example: you have two files in the same directory: first.cpp and second.cpp. In second.cpp there is a global variable:
//in second.cpp file int _count = 0;
Now no need to include second.cpp in your first.cpp to use this variable
In first.cpp you have to declare the
extern variable, called _count:
//in first.cpp file extern int _count;
Now you can use _count variable in the scope where it is declared as extern:
//in first.cpp file cout << _count;
The use of the extern can be represented by the following image:
The value of _count from second.cpp will be displayed from first.cpp
We are sorry that this post was not useful for you!
Let us improve this post!
Tell us how we can improve this post?