-OR-Refresh your memory.. (or correct me if I'm wrong) ;]
So what is 'const' ?===='const' is an important keyword in C/C++. It's short for constant, and that basically describes what it is used for. A constant is a variable type which, under most circumstances, cannot be changed. These come in handy on frequently used variable which never change.
Ex.
....for(int i = 0; i < MAX; i++){ // C = 2 * PI * r circum[i] = radius[i] * TWO_PI;}
Notice that we use TWO_PI a lot. In fact, a nine million times a lot. There is absolutely no reason why we should calculated 2 * PI 9,000,000 times, right? Just once should be fine.
Now let's talk about what const does to a variable. When a variable is declared const it becomes, for our purposes, read only. This means you cannot change them. (note, there is an explicit cast which can change a constant variable..)
After that fun, let's talk about const correctnes. We'll make up a class we all know about. I'm going to call it a g_int. It represents an integer number, and we'll store the data in a int type. Here is a quick idea of the class.
Now lets look at whats going on here. If I want a g_int, I create a g_int by doing this.
Now we're golden.Let's add another simple function.Assume we have written a function for '='.
The first one is a little more interesting. We are returning a constant reference to the sum. Why are we doing this? Well, first, we don't really want to do a full copy, like before. And secondly, we DO NOT want people to change this reference part way through. Let's pretend we wrote an overloaded = which does some altering.. (bad bad)Here is the body..
g_int z(2);g_int x = z;
This is why we return a constant version of this reference, so no other function decides it's going to be messing with the value inbetween.
Whew.. that's was quite a read. In the end, I hope you have more of an idea about const correctness.
As always, comments are welcome. Corrections are welcome even more.
for example
#define PI 3.14
then you cant really overirde PI
NOTE: you can override, but the compiler will give you a big ass worning, cause that could be a bug in your program
I don't mind programming assignments. I prefer a challenge. The stuff I got this year was sinchy.
;] Maybe you don't know what I would assign. ;]I would make all attempts to assign homework that would take 8-10hours for smaller stuff. Large projects would definately be 100+ hours.