Jump to content
  • Advertisement

Archived

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

Toadhead

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
quote:
Original post by Toadhead
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 this post


Link to post
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 this post


Link to post
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]; // works

int 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; // works

example< max_size_2 > object2; //will not work

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!