The term "const" is an abbreviation for "constant". It can be used in several ways:
const variable
This is an alternative to the use of #include. If we want to have a named constant in a program, we can declare it with a name, a type and an initial value:
const type Name = value;
Examples:
const int ARRAYSIZE = 100; const double PI = 3.1415926; const char Dash = '-';
Normally the name of a constant is all in upper-case letters; this is a convention and not a language requirement.
A const variable may be local or global. As its value cannot be changed, it must be initialized at once when it is defined.
A common standard in C++ coding is to encourage the use of const and discourage the use of #define.
const data member of a class
A const data member must also be a static member, as in this example:
Unlike instance variables, a static constant class variable must
be initialized immediately, so we are allowed and required to
initialize it in the class definition itself (which would not
otherwise be permitted).
Like any static data member, a const static data member belongs
to the class as a whole and not to each particular instance. (See the
note on "static".)
const reference argument
When we pass an argument to a function, we have a choice: pass
it by value or by reference, as in this example:
What happens if F tries to change the value of A? F cannot
change the value of the actual argument in the calling code, but F can
change the value of the copy it received.
What happens if G tries to change the value of B? G can change
the value of the actual argument in the calling code.
If we do not want a function to be able to change the value of an
argument, we can make the argument a "constant reference" argument
instead:
Now the compiler will regard any attempt by H to change the value
of C as an error.
The actual argument we provide to H does not have to be a
constant in the calling code; it could be a variable or an expression
or a literal constant. As far as the compiler knows, however, as it
compiles H, this argument is a constant and cannot be changed.
The advantage of using constant reference arguments is that we
have the compiler checking our work for us: we don't want H to change
C, and the compiler verifies this. Of course, we also have the
advantages of reference arguments in general: there is no need to
make a copy of C, which saves time and memory.
A common standard in C++ coding is to require arguments to be
declared as constant references unless the intention is to change
their values. (This especially applies to arguments larger than, say,
an int or a char.)
const method of a class
Some methods have no need to change the data members of a class.
In that case, we may want to have the compiler verify this for us, so
we do not accidentally change something. If this is what we want, we
can declare a method as const, as in this example:
Here the GetID method has no need to change any of the data
members, and because we have used "const", the compiler will check to
ensure this.
A const method may not use any other methods which are not const.
A common standard in C++ coding is to require all methods to be
declared as const if they are not intended to change the values of
data members.
void F(int A); //F has the address of a copy of A's value
void G(int & B); //G has the address of B itself
void H(const int & C); //H has the address of C itself
//but treats C as a constant
class Widget
{
private:
int ID;
...
public:
int GetID() const; //This returns the value of ID.
...
};