The const qualifier explicitly declares a data object as something
that cannot be changed. Its value is set at initialization. You
cannot use const data objects in expressions requiring a modifiable
lvalue. For example, a const data object cannot appear on the
lefthand side of an assignment statement.
An object that is declared const is guaranteed to remain constant for its lifetime, not throughout the entire execution of the program. For this reason, a const object cannot be used in constant expressions. In the following example, the const object k is declared within foo, is initialized to the value of foo's argument, and remains constant until the function returns. In C, k cannot be used to specify the length of an array because that value will not be known until foo is called.
void foo(int j) { const int k = j; int ary[k]; /* Violates rule that the length of each array must be known to the compiler */ }
In C, a const object that is declared outside a block has external linkage and can be shared among files. In the following example, you cannot use k to specify the length of the array because it is probably defined in another file.
extern const int k; int ary[k]; /* Another violation of the rule that the length of each array must be known to the compiler */
A top-level declaration of a const object without an explicit storage class is considered to be extern in C, but is considered static in C++.
const int k = 12; /* Different meanings in C and C++ */ static const int k2 = 120; /* Same meaning in C and C++ */ extern const int k3 = 121; /* Same meaning in C and C++ */
In C++, all const declarations must have initializers, except those referencing externally defined constants.
The remainder of this section pertains to C++ only.
A const object can appear in a constant expression if it is an integer and it is initialized to a constant. The following example demonstrates this.
const int k = 10; int ary[k]; /* allowed in C++, not legal in C */
In C++, a const object can be defined in header files because a const object has internal linkage by default.
The keyword const for pointers can appear before the type, after the type, or in both places. The following are legal declarations:
const int * ptr1; /* A pointer to a constant integer: the value pointed to cannot be changed */ int * const ptr2; /* A constant pointer to integer: the integer can be changed, but ptr2 cannot point to anything else */ const int * const ptr3; /* A constant pointer to a constant integer: neither the value pointed to nor the pointer itself can be changed */
Declaring an object to be const means that the this pointer is a pointer to a const object. A const this pointer can by used only with const member functions.
const Member Functions
Declaring a member function const means that the this pointer is a pointer to a const object. Data members of the class will be const within that function. The function is still able to change the value, but requires a const_cast to do so:
void foo::p() const{ member = 1; // illegal const_cast <int&> (member) = 1; // a bad practice but legal }
A better technique would be to declare member mutable.
Related References