E. Robert Tisdale said:
A const variable is an oxymoron.
An object is either a constant or a variable -- not both.
The phrase "const variable" sounds like an oxymoron if you think of
const as meaning "cannot change" and variable as meaning "can change";
but these are insufficiently precise definitions borrowed from casual,
every-day English. If we're talking about C++ we should use these
terms as they are defined in the C++ standard.
In C++, the term variable does not mean non-const. A variable is simply
a name that denotes an object. (From 3. Basic Concepts, "A variable is
introduced by the declaration of an object. The variable's name denotes
the object.") Thus, in the following example, k is a variable which
also happens to be const:
const int k = 0;
Perhaps you would argue that k is not really an object. If so, you'd
be wrong. The compiler may replace occurrences of k in expressions
with 0, and might even optimize away the memory allocated to k, but
these are optimizations; k is still an object according to the rules
of the language; if it weren't you wouldn't be able to take its
address (perhaps this is the point JKop was trying to make).
So QED. But arguments by reference to the standard are not always
very satisfying (convincing yes, but not always satisfying) so
I'll try to give a more intuitive explanation.
Let's make a little detour into mathematics. Consider the equation:
f(x) = x^2 + x + 1
Here we've defined f as a function of x, where x is a variable.
By variable, we mean only that we don't know the value of x in
advance; but once we "plug in" a value for x, we don't expect it
to change from one occurrence to the next.
Now let's consider a similar C++ function:
double f(const double x)
{
return (x * x) + x + 1;
}
Here x is a variable in the C++ sense (a name bound to an object)
and also in the mathematical sense (an unknown value). It is also
const, which means we can expect every occurrence of x to have the
same value -- just as in the mathematical expression.
Now it's true (AFAIK - I'm no mathematician) that in math a symbol
is either a variable or a constant, not both. But "const" in C++
does not mean something is constant in the sense of a fixed, known
value like e or pi; it means something closer to "read-only".
A nearer equivalent to a mathematical constant would be k from the
first example above. In C++, k is a variable which also happens to
be an "integral constant expression" (per 5.19 because it is a const
int variable initialized using an integral constant expression; note
here that "constant" and "const" are NOT synonyms). This is a bit of
a departure from math. But really, it's an arbitrary choice whether
to think of constants and variables as mutually exclusive categories;
or of constants as a sort of special, degenerate case of variables.
C++ takes the latter approach. An advantage is that you can use k
anywhere you would use a variable of the same type, and you don't
need a whole separate set of language rules to do so.