Quote: i think about the only thing i think i know is that you use a const when whats being stored is not expected to change.
Well, that's basically all there is to it, honest. :) It's a safety mechanism, and serves as a statement of intent. By touching a const variable, you are essentially entering a (bendable) contract to not mess with it. Of course, sometimes you need to strip the const annotation. There are two such situations:
(1) for some reason, you want to edit it, even though you're not supposed to. This is very, very unusual, and if it comes up, it might be worth rethinking why you're editing a const variable.
(2) much more frequent: you need to pass something to a function that you know doesn't edit it, but for whatever reason (usually laziness or not knowing any better on the part of the function writer) the function isn't marked as having const variables. Therefore you need to temporarily remove the const keyword with a case.
Of course, in case (2), the compiler (rightfully) complains at you, telling you that you're doing something dangerous. For all you know, that function might modify the data, which is something it really shouldn't do -- and that modification might even cause a crashing bug. (If the data is read-only, and that read-onliness is enforced by the operating system, attempting to write will cause a runtime error.)
I think Nick's explanation of the arg question is very fine so I'll not elaborate on that point. :) I'll just highlight one point which is the conceptual difference between null and zero. Java makes this difference explicit, actually, by marking it a compile-time error to assign null to non-pointers, or zero to pointers. |