Archived

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

Armedies

simple #define question

Recommended Posts

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...

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
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;
}

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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;


Share this post


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

Share this post


Link to post
Share on other sites
"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]

Share this post


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

Share this post


Link to post
Share on other sites
I was only using an integer as an example, ther real define i''m using is a bit more complex. Sorry for any confusion that may have caused.
The define actuallly is used to hold RGB values in different formats in 16 bit res.

Share this post


Link to post
Share on other sites