View Single Post
  #3   Spotlight this post!  
Unread 14-02-2004, 15:19
steven114 steven114 is offline
Programming Wizard and Team Captain
AKA: Steven Schlansker
FRC #0114 (Eaglestrike)
Team Role: Programmer
 
Join Date: Feb 2004
Location: Los Altos, CA
Posts: 335
steven114 is a jewel in the roughsteven114 is a jewel in the roughsteven114 is a jewel in the rough
Send a message via AIM to steven114
Re: Conditional #define Statements

Quote:
Originally Posted by Paul
Why not just have a conditional declaration?
if(your condition is meet)
{
int your_var;
}
else
{
int something else;
}

I'm probably misunderstanding you but its still worth a shot.
Won't work. The declaration goes out of scope after the if block. There are two methods -

1) You know at compile time which one you want
Code:
#if IWANTVAR1
#define myVar var1
#else
#define myVar var2
#endif

//myVar now refers to var1 or var2, depending on IWANTVAR1 (which MUST be either a constant or expand to a constant)
2) You know at runtime

Code:
int myVar;
if(iWantVar1)
myVar = var1;
else
myVar = var2;
Note that way 2 only works for reading the variable, not writing. If you want something like the aliases, you either must know at compile time or deal with pointers (afaik)

I hope this is what you wanted...