Storage Class Specifiers in C++
Specifiers in C++ - Part 2
Forward: In this part of the series, I explain Storage Class Specifiers in C++.
By: Chrysanthus Date Published: 25 Aug 2012
Introduction
The auto and register Specifiers
Consider the following program:
#include <iostream>
using namespace std;
int main()
{
int myInt = 25;
cout << myInt;
return 0;
}
The block of the main function (including any of its parameters) is the function scope, which is just like the local scope. An ordinary object declared in a local scope dies at the end of the local scope. Such an object is said to have automatic storage duration. Such an object, in the local scope can optionally be preceded by the specifier, auto, or register. Here, auto and register are synonyms. So the following code is the same as the above:
#include <iostream>
using namespace std;
int main()
{
auto int myInt = 25;
cout << myInt;
return 0;
}
Note the use and position of, auto. So, the auto or register is like the default specifier, which you can omit.
Remember, some local scopes have nested local scopes.
The static specifier is used to give an object static storage duration, as illustrated below:
Normal Object
Consider the function, fn() in the following code:
#include <iostream>
using namespace std;
void fn()
{
int n = 0;
cout << n << '\n';
n = n + 2;
}
int main()
{
fn();
fn();
fn();
fn();
return 0;
}
In the function, fn(), n is an ordinary integer object. In main, the function fn() is called 4 times. Each time the function, fn() in main is called, n is reinitialized to zero. So the value displayed for n from fn() is always zero. After the display (cout statement), n is increased by 2, but this has no effect in the display, the next time fn() is called. This increase of n as a whole, dies (is annulled) at the very end of the function block.
If in the fn() function above, n where made static, then n will be initialized only when the function is called for the first time. That is one of the characteristics of a static object. Another characteristic of a static object is that it does not die when it goes out of scope. So, for a static object, if the scope is re-executed, the previous value of the static object will be maintained. To define a static object, precede the normal definition with the reserved word, static. Read and try the following code:
#include <iostream>
using namespace std;
void fn()
{
static int n = 0;
cout << n << '\n';
n = n + 2;
}
int main()
{
fn();
fn();
fn();
fn();
return 0;
}
In the fn() function, n is now a static object. The first time the fn() function is called from main, n is initialized to zero. Then n is displayed. Then n is increased by 2. and the function block goes out of scope. Since the value of a static object does not die when it goes out of scope, the last value of n is retained by n.
The next time, the function, fn(), is called, n is not initialized again; that is, the initialization statement is skipped. After the skipping, n is displayed, and then n is increased by 2 on its previous value. So, the output of the above code is, 0, 2, 4, 6, in new lines.
So, a static object is an object that is initialized only once if its scope block is executed many times and it is an object that does not die when it goes out of scope.
The initialization of a static object should not be broken down into definition and assignment.
Note: when using the auto, register, static or extern specifier, the simple type specifiers can also be used. Any of these new specifiers is just typed in front of the simple type specifier.
That is it for this part of the series. We stop here and continue in the next part.
Chrys
Related Courses
C++ CourseRelational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem
NEXT