LiveWire Network Peer Answers Peer Support Teen Forums Tech Forums College Forums 397 users online 225521 members 289 active today Advertise Here Sign In
TeenCollegeTechPhotos | Quizzes | LiveSecret | Memberlist | Dictionary | News | FAQ
Member Spotlight
harry555
Music: Music- Anything sweet.. rap, techno, re...
Mood: Flirty
You have 1 new message.
Emergency Help
Until you sign up you can't do much. Yes, it's free.

Sign Up Now
Membername:
Password:
Already have an account?
Invite Friends
Active Members
Groups
Contests
Moderators
3 online / 27 MPM
Fresh Topics
  LiveWire / Technical Forums / Programming & Application Development / Adding Reply

Quoting Post
Archived Topic: It will not be bumped to the top of the forum.
Topic A short bit on 'const correctness'
Membername   Not a member? Sign Up Free (takes 20 seconds)
Password   Forgotten your password?
Post

Font:   Size:   Color:

FAQ Keyword Search:
Post Options
Favorites Manager
Notify me of new replies to this topic by email
Notify me of new replies to this topic by private message
Original Post
sakurag Posted at 9:55 pm on July 13, 2004
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.
Code:

int x = 5;
g_int(x);

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.

Replies
Duekiller Posted at 6:01 pm on Aug. 12, 2004
easier way is to declare the veriable on top

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

BBallAsh23 Posted at 8:52 pm on July 29, 2004
i think sakurag might have to help me this school year, lol... maybe. :p
melissa05 Posted at 7:20 pm on July 28, 2004
Where the hell were you when I was taking C++ last year?  
sakurag Posted at 9:46 am on July 19, 2004
Quote: from squirellplayingtag at 8:26 pm on July 15, 2004

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.

squirellplayingtag Posted at 8:26 pm on July 15, 2004
I don't mind programming assignments. I prefer a challenge. The stuff I got this year was sinchy.
sakurag Posted at 4:47 pm on July 15, 2004
No you don't, you wouldn't like my assignments..
squirellplayingtag Posted at 7:36 am on July 15, 2004
I wish sakurag was my IT teacher.
BBallAsh23 Posted at 9:59 pm on July 13, 2004
ahh i think i get it... ;)  :D
All 8 previous replies displayed.