const int D = a[1] - a[0];
Right there. At that point the value of D
is determined from the current value of a[1]
and a[0]
and stored. It is then disconnected completely from a[1]
and a[0]
. Changes to those two will no longer have an effect on D
.
In the vast majority of programming language this is exactly how it will work. The =
operator is called the assignment operator. It takes the result of expression on the right side and assigns it to the variable on the left side. It works by value, not by reference. So it will assign only the value at the time of execution to the variable. The variable will not change with out a second assignment.
There are cases in C++ and a few other languages where this is not strictly true, they deal with pointers. And will look like this:
int b = 5;
int *d = &b;
The expression on the right side is the address of a (single) variable that gets assigned to a pointer (the &
operator is the address operator, the *
operator on declaration declares that d
is a pointer). The pointer then contains the address of that variable. The value it gives when dereferenced with be the value of the variable. However, pointers only hold the value of a single variable. They cannot be an alias to an expression. But before you dig further into pointers, you should get yourself a better grasp of the language in general. Pointers are pretty complex and advanced topic.