|
Until you sign up you can't do much. Yes, it's free.
|
|
|
|
|
|
|
 | / / / Viewing Topic
|  |
|
|
A short bit on 'const correctness' |
| for BBallAsh23 |
|
|
|
Replies: 8 Last Post Aug. 12, 2004 6:01pm by Duekiller
|
|
|
|
|
( sakurag )
Soothsayer
Patron
|
For those of you who know all about the keyword const in C/C++, please disregard this-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.
| Code: | ... const float TWO_PI = 2 * 3.14159; const int MAX = 9000000;.. .. 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..) Ex.
| Code: | .. const int x = 50; x = 4 + 5; // bad, compiler error
|
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. | Code: | class g_int { public: g_int( ); g_int(int value) { _value = value; }; private: int _value; };
|
Now lets look at whats going on here. If I want a g_int, I create a g_int by doing this.
Now let's do some corrections, (which will involve our first occurance of const) yay. The line is question is this one:
| Code: | g_int(int value) { _value = value; };
|
yuck, What was I thinking? Let's change this so we can be more efficient. Instead of passing the 'int value' by Value, let's pass it by reference. This is good because now we don't have to allocate another 'value' on the stack. Look at the usage code above. We have now just made an additional copy of 'x' because we passed it by value to g_int. This doesn't seem so bad, but what if you had a variable which was 2-3meg.. yeah, another copy would really be terrible. Let's change the code.
| Code: | g_int(const int& value) { _value = value; };
|
Now instead of passing it by value, the 'value' passed in is a reference to the original. Notice I put a const in front of it. Why on earth would I do that? Well, this is where part of const correctness comes in.. Say you didn't do this, and left the code the same, except for one part.
| Code: | g_int(int& value) { value = 1000; };
|
Woah. Now you just changed the original you put in. That's terrible! This wasn't the intention.. I know I did it purposely, but pretend that this was a more complicated function.. and deep deep in, it did this. And now you've got no clue why your customer.account_balance keeps going from $500 to $1000000..Now we're golden. Let's add another simple function. Assume we have written a function for '='.
| Code: | const g_int& add(const g_int& rhs) const;
|
Now you're probably like.. 'that's a lot of const.' You understand the inner one. I explained it before in the previous section. But what about the beginning one and the ending one. Well the ending one is rather simple to explain, it just means that this function can be called by a const version of the class. Without it, you couldn't use the add( ) if you make a 'const g_int g(6)' and then do 'g.add(....)'. Which is just stupid. You should be able to add two integers even if they are constants.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..
| Code: | void g_int::operator=(g_int& rhs) const // lookie, no const { rhs = 50000; }
|
NOW, if were were to do this..
| Code: | g_int z(2); g_int x = z;
|
What do you think this did? Well, first.. x is still undefined. who knows what you did to 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.
------- I've got spurs that jingle jangle jingle. As I go riding merrily along. And they sing, "Oh, ain't you glad you're single?" And that song ain't so very far from wrong.
|
9:55 pm on July 13, 2004 | Joined: Oct. 2002 | Days Active: 853 Join to learn more about sakurag Washington, United States | Straight Male | Posts: 2,751 | Points: 12,118
|
|
| |
|
|
|
|
 LiveWire Humor
|
|
|
|
squirellplayingtag
Dairy Product Addict
Patron
|
I wish sakurag was my IT teacher.
------- "The most abundant element in the universe is hydrogen, then second is stupidity."-Harlan Ellison "What good fortune for our governments that the people do not think."-Hitler
|
|
|
( sakurag )
Soothsayer
Patron
|
No you don't, you wouldn't like my assignments..
------- I've got spurs that jingle jangle jingle. As I go riding merrily along. And they sing, "Oh, ain't you glad you're single?" And that song ain't so very far from wrong.
|
4:47 pm on July 15, 2004 | Joined: Oct. 2002 | Days Active: 853 Join to learn more about sakurag Washington, United States | Straight Male | Posts: 2,751 | Points: 12,118
|
|
| |
|
|
squirellplayingtag
Dairy Product Addict
Patron
|
I don't mind programming assignments. I prefer a challenge. The stuff I got this year was sinchy.
------- "The most abundant element in the universe is hydrogen, then second is stupidity."-Harlan Ellison "What good fortune for our governments that the people do not think."-Hitler
|
|
|
|
|
melissa05
Dairy Product Addict
|
Where the hell were you when I was taking C++ last year?
------- If you think everything is going good then you obviously don't know what the hell is going on.
Deja Moo. The feeling that you've heard t
|
|
|
|
|
Duekiller
Dairy Product Addict
|
easier way is to declare the veriable on topfor 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
------- Some days I am the dog, Some days I am the hydrant. In either case the sun shines in my face.
|
|
|
|
| Looking for something else?
|
|
|
|
|
|
 | / / / Viewing Topic |  |
|