🔒 Locked
const or #define?
Advertisement
Original Post
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.
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.
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.
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
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
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?
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]
[Edited by - Dead Eye on March 12, 2005 1:30:15 AM]
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?
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?
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.
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.
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
const variables with internal linkage can be defined multiple times in a program, but not multiple times in a .cpp.
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 errorconst variables with internal linkage can be defined multiple times in a program, but not multiple times in a .cpp.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement