Skip to main content
gamedev.net
🔒 Locked

const or #define?

Started by warp X 37 replies 5k views
Original Post
warp X
warp X
Which one is better to be used in game development, a const definition or a #define definition in c++? What about c-macros versus inline functions?
"I got stuck between levels 4 and 5 and got into this strange place: warp X..."
_the_phantom_
_the_phantom_
Always prefer built in language features to pre-processor trickery.
With built in language features you get type safety and proper checking, with pre-processor trickery is alot easier to end up in a mess [wink]

dont get me wrong, #defines and macros have their place, but often built in features are much better to use.
nilkn
nilkn
Quote:
Original post by _the_phantom_
Always prefer built in language features to pre-processor trickery.
With built in language features you get type safety and proper checking, with pre-processor trickery is alot easier to end up in a mess [wink]

dont get me wrong, #defines and macros have their place, but often built in features are much better to use.


Also, when in a situation like this:

#define FOO (1.0f / 987.0f)

vs.

static const float foo = 1.0f / 987.0f;

always use const as the math will be precomputed and stored in a memory location. If you used the define, it would be recomputed everytime you used the identifier.
CJM
CJM
Regardless of if it's game design or general programming,

Hands down, const.

Only use #defines if you need to perform a literal replacement everywhere in your source code, use const if all of the stuff that you're replacing is of one data type.

Const is type-safe, can be a member of a class, can take on any type [only 1 at a time though], and you can template them AFAIK, though I haven't tried.

#define is a literal token replacement, changes whereever that is in your code literally at compile, is not type safe, but can be more easily used for many types of data which need to represent the one thing.

#define() function macros however, are very handy. If you can't do it using consts or inlined functions [a little slower generally, but stuff that you'd #define functions for would be so quick anyways] then and only then use #defines.

[edit] beaten to the punch, by several people... [/edit]
Just my opinion,
CJM
chollida1
chollida1
const parameters can be verified for type safety, #defines can't.

Cheers
Chris
CheersChris
JClayton
JClayton
Also, #defines are not added to the v-table, so if the compiler or the debugger detects an error it will display the value of the #define rather than the name.
cwhite
cwhite
Use const
smart_idiot
smart_idiot
Quote:
Original post by JClayton
Also, #defines are not added to the v-table, so if the compiler or the debugger detects an error it will display the value of the #define rather than the name.


Since when do variables and non-virtual functions get added to vtables?
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other
Dead Eye
Dead Eye
use both. some guy above though said it best when he said "they both have their place." though i really only use cpp when i have to (defining WIN32_VER and WINNT_VER and so on). basically MFC/Win32/Linux related stuff. i rarely declare global consts too. for exact constants i usually plug the value right in instead of using a variable or a macro.

[Edited by - Dead Eye on March 12, 2005 1:30:15 AM]
dave
dave
Quote:
Original post by nilkn
Quote:
Original post by _the_phantom_
Always prefer built in language features to pre-processor trickery.
With built in language features you get type safety and proper checking, with pre-processor trickery is alot easier to end up in a mess [wink]

dont get me wrong, #defines and macros have their place, but often built in features are much better to use.


Also, when in a situation like this:

#define FOO (1.0f / 987.0f)

vs.

static const float foo = 1.0f / 987.0f;

always use const as the math will be precomputed and stored in a memory location. If you used the define, it would be recomputed everytime you used the identifier.


But surely only used at compile time?
Jingo
Jingo
Quote:
Original post by nilkn
Quote:
Original post by _the_phantom_
Always prefer built in language features to pre-processor trickery.
With built in language features you get type safety and proper checking, with pre-processor trickery is alot easier to end up in a mess [wink]

dont get me wrong, #defines and macros have their place, but often built in features are much better to use.


Also, when in a situation like this:

#define FOO (1.0f / 987.0f)

vs.

static const float foo = 1.0f / 987.0f;

always use const as the math will be precomputed and stored in a memory location. If you used the define, it would be recomputed everytime you used the identifier.


static const?

Whats the point in that?
Polymorphic OOP
Polymorphic OOP
Use const when you need compile-time integral constants or any constants used at run-time. Use inline functions when you want to give a hint to the compiler that a function should be inlined. Use template metafunctions when you need to do calculations whose results are useable at compile-time. Use macros when you need to generate code that cannot be expressed with other language features or if it gets rid of code redundancy. Use #define constants when you need constants at pre-compile-time, such as when you need to use constants that have to be passed and used by macros prior to compilation.
Jingo
Jingo
Quote:
Original post by Dead Eye
well in windows it allows you to make multiple foo declarations
in other source modules.

// in module1.cpp
const float foo = 1.0f / 987.0f;

// in module2.cpp
const float foo = 2.0f / 987.0f; // error! foo multiple declaration

there's no error if you declare them as static.


Thats not true. :(

Well, it would be true in that case, as you have given the variable different values, if you gave it the same value in each .cpp, there would be no undefined behaviour, and no error.
valla2
valla2
For me the answer is simple: Use const because most people prefer it :)
Dead Eye
Dead Eye
correct. oops. thought i coulda sworn i got that error once :S
Jingo
Jingo
In addition, static variables at namespace scope are deprecated in c++.
Jingo
Jingo
Its a matter of linkage Dead Eye. const variables at namespace scope (which includes globals) have internal linkage by default, they are a special case. If you tried your code without the const, it would not compile, as non-const variables have external linkage by default.

If you gave one of the const variables in a .cpp file external linkae, you would get an error. For example

1.cppextern const int i = 0;2.cppconst int i = 0; // multiple defintions, linker error


const variables with internal linkage can be defined multiple times in a program, but not multiple times in a .cpp.
Dead Eye
Dead Eye
ah i see cool. thanks for clearing that up Jingo :).
valla2
valla2
Quote:
Original post by Anonymous Poster
IIRC #define is good when you need to concatenate constant strings, and when the constant is shared across translation units.

can you post a very simple example showing that, because i don't see how to do that?
Jingo
Jingo
#define hello "Hello"#define world "World"#include<iostream>#include<ostream>int main(){std::cout << hello world << std::endl;}


Though why you would want to do that is anyones guess. :)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.