Public Group

#### Archived

This topic is now archived and is closed to further replies.

# const and #define (C++ Programming)

This topic is 5241 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello, I have a question. My tutorial is telling me about #define and "const int variable=5;" etc. etc. But why should you use them?? I mean isn''t "const int test=5" the same as "int test=5;"? Can someone please explain me the difference?

##### Share on other sites
I mean isn''t "const int test=5" the same as "int test=5;"?

Using ''const'' variables means that you can''t change them, and because you can''t change them the compiler can just pass the value directly instead of an address...

I''m a C programmer though, so I use #defines.

##### Share on other sites
so...

why should I use const? (and not just always normal variables)
Will it save memory or something?

##### Share on other sites
The const is so you dont accidentally change the variables value.

So say you had a variable named Gravity and somewhere in your code you accidentally changed the value of Gravity, that would not be good. So by saying const float Gravity, it wont change.

[edited by - envy3d on April 14, 2004 3:44:59 PM]

##### Share on other sites
The reason you should use const type name = whatever instead of #define name whatever is the const version is type-safe. This means that if name is a float you can only use it as a float, and will get an error if you use it otherwise, but with a define it may still work if you use it as something other than a float but cause a run-time error. The const bit is just so you can''t accidentally alter it.

##### Share on other sites
yes.. I know you can''t change it.. but what I mean is why should I use it instead of just the normal declaration of variables? I mean if you declare a variable its not nessacary to change it later!

##### Share on other sites
quote:
yes.. I know you can't change it.. but what I mean is why should I use it instead of just the normal declaration of variables? I mean if you declare a variable its not nessacary to change it later!

Wrong...
You might define a variable:

char cont = 'y';

and then enter a loop:

while(cont=='y'){ //do stuff and ask to continue, updating cont as is necessary}

Or you might have a constant as was mentioned above.
Basically you can declare a value that will never change and is also type safe when in use later. The point is, no one can modify that variable in any way, which is especially fortunate in group efforts when someone is always doing something peculiar.

[edited by - woodsman on April 14, 2004 4:02:18 PM]

##### Share on other sites
still don''t get it :/

:''(
Damn.. I also don''t realy know why using it.. the tutorial I have don''t give good examples.. and I can''t buy a book here in the Netherlands, I haven''t found one here :''(

##### Share on other sites
The fact that it can''t be changed is only half of the story. The other reason to use const with built-in integral types is not just because it can''t be changed and is typesafe and scope-safe, but also because you can use const integral built-in instantiations in expressions that would have normally expected literal constants back in C89. For instance, in C++ you can use a const std::size_t initialized to compile-time constant integer value as the size of an array established at compile time. Back in C89, this wasn''t possible, which is why people used #define instead of const. In C99, that SOMEWHAT changed with the advent of variable length arrays (arrays declared normally on the stack but with a variable as a size). Even still, that case does not allow you to work with the array as if it had a size established at compile time, whereas const in C++ does. Example (in C++):

#include<cstddef>const std::size_t max_size = 256;std::size_t max_size_2 = 256;int array[max_size]; // worksint array2[max_size_2]; // will not work

As well, you can use consts as template arguments, whereas you can''t with non-const variables:

#include<cstddef>const std::size_t max_size = 256;std::size_t max_size_2 = 256;template< std::size_t Value >class example {};example< max_size > object; // worksexample< max_size_2 > object2; //will not work

##### Share on other sites
So basically the idea was to replace defines. It's a teeny bit faster than a normal declaration, and it prevents you from doing something stupid with it, but it also allows error checking, besides what Polymorphic OOP said.

[edited by - Drakkcon on April 14, 2004 5:28:45 PM]

1. 1
2. 2
JoeJ
20
3. 3
4. 4
frob
12
5. 5

• 13
• 19
• 13
• 20
• 13
• ### Forum Statistics

• Total Topics
632194
• Total Posts
3004692

×