simple #define question

Started by
9 comments, last by Armedies 20 years, 8 months ago
Can you use a #define in an if statement? Here''s an example of what I''m trying: int a = 0; //manipulate a if(a == 1) { #define example 4 } else if(a == 2) { #define example 2 } //etc...
Advertisement
#define are compile-time statements, they are not evaluated at run-time.

You can use #if and compare with compile-time constants

#if defined(__BLAH)
#define EXAMPLE 5
#else
#define EXAMPLE 4
#endif
so what would be the best alternative to use if i needed a Run-Time #define-like variable?

[edited by - Armedies on August 7, 2003 11:59:31 AM]
Um, if it''s a value that can change at run-time, then use a plain-old variable!

int example = 0;
if(a == 1)
{
example=4;
}
else if(a == 2)
{
example=2;
}
A normal variable would do nicely for run-time data

int example;if(a == 1){  example = 4;}else{  example = 2;} 


I don''t understand what you mean by a run-time #define-like variable?

#defines are NOT variables, they''re replacements. If you do:

#define WABADOO (x+5)int something(int x){  return WABADOO;} 


the compiler will see this code (after the pre-processing, which is where it does the #include and #define work (and anything else starting with ''#'') as:

int something(int x){ return (x+5);} 


It''s as simple as that. #define is like a find-and-replace

Anything that needs to change its value during runtime has to be a variable of some form.
well, I don''t want to change it''s value. I just want to run a switch statement at the beginning of my program, and depending on it''s value, use on or another #define. nothing will be changed after that.
just use:

const int EXAMPLE_1 = 1;
const int EXAMPLE_2 = 2;
const int* EXAMPLE = NULL;

if(a == 1)
EXAMPLE = &EXAMPLE_1;
else
EXAMPLE = &EXAMPLE_2;


quote:Original post by Armedies
well, I don''t want to change it''s value. I just want to run a switch statement at the beginning of my program, and depending on it''s value, use on or another #define. nothing will be changed after that.


What''s the difference between using define and using a variable for you?

If you''re concerned about someone accidentally changing the value, then I would say make a class that can wrap up your settings and only provide read-only access:

class Settings {  friend void SetSettings(Settings& settings);  const int m_example;public:  Settings() : m_example(0);  Settings(_example) : m_example(_example) {}  int example() const { return m_example; }};Settings g_Settings;void SetSettings(Settings& settings) {  if(a == 1) {    settings.m_example = 4;  }  else {    settings.m_example = 5;  }}


Call SetSettings(g_Settings) at the beginning of your app.

Regards,
Jeff
"What's the difference between using define and using a variable for you?"

-well, i started the program with a define, and changing it to an actual variable means a lot of chasing. however, the class idea, isn't a bad one, perhaps I'll try that, and see how well it works

Thank you for the help.

[edited by - Armedies on August 7, 2003 12:31:53 PM]
What kind of chasing do you need to do? You''ve changed a literal integer value (the #define) into an integer variable but how does that screw up your code?

This topic is closed to new replies.

Advertisement