|
In programming, it is common for the values of some of the objects, after they are initialized, to remain unchanged throughout the execution of the program. In C++, it is best to add const to the declaration of such objects. A const object is a read-only object. Thus you eliminate any possibility that the value of such an object might get inadvertently modified through a bug in the program. In Java, the effect is achieved by the type modifier final. Here are some examples for C++:[12]
const int N = 100; const double PI = 3.141592653587932385; const int a[] = {0, 1, 2, 3};
and some examples for Java:
final int N = 100; final double PI = 3.141592653587932385; final int[] a = new int[] {0, 1, 2, 3};
None of these objects can be modified within the scope of their declarations. It should be obvious that since a const object cannot be assigned to, it must be initialized when it is first declared.
Unlike C++, Java does not insist that a variable declared to be final be also initialized at the same time. In other words, Java lets you declare a variable final in one statement and to then initialize it separately in a later statement. You are therefore allowed to do the following in Java:
final int N; // ... N = 1000; // N cannot be changed hereafter
In C++, the modifier const can also be used for pointer types, but its meaning has to be treated with care. For example, if you say
const T* p; //(A)
this says that p is a pointer to an object of type const T. Therefore, the object that p points to cannot be changed, although p itself could be. If we wanted the pointer itself to be const, we would declare
T* const p; p = new T( ....constructor args.... ); //(B)
To infer the correct meaning of such declarations, it can be helpful to read them from right to left. In this manner, you'd read the declaration at(A) as p is a pointer to T that must remain const and the one at (B) as p is a const pointer to T.
To illustrate further the distinction between the declarations at (A) and(B) above, the following syntax would not be acceptable to a C++ compiler:
T* const p; p = new T( ...constructor arguments ....); // ERROR
because a const pointer cannot be assigned to; it can only be initialized. The following syntax is also illegal, but for a different reason
const T* p; p = new T( ...constructor args.... ); *p = T( .... constructor args .... ); // ERROR
Since p is now a pointer to a const object, by assigning to *p, you are trying to change the contents of the memory where that object resides. But that memory was previously declared to be read-only memory.
However, the following piece of code is legal:
const T* p; const T* q; p = new T( ...constructor args.... ); q = p; p = new T( .... constructor args .... );
These statements are legal because both p and q are modifiable pointers (although the objects they point to are not modifiable).
[12]Regarding the third example shown, note that the elements of a C++ arraythat is declared to be const can only be accessed by a const pointer. By the same token, the elements of a const container can only be accessed by a const iterator.
|