Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   Using macros with operator symbols (http://www.chiefdelphi.com/forums/showthread.php?t=31933)

jgannon 23-12-2004 15:01

Using macros with operator symbols
 
For reasons that make sense to my application, I want to have a macro defined that will change "FOO=x;" to "BAR+=x;". To do "FOO=x; BAR+=FOO;" would be horribly unwieldy, and would not work in this application. I have tried "#define FOO BAR+", but I get a syntax error on the "FOO=x;" line. I have also tried "#define FOO= BAR+=", but the equals sign is an invalid character in macro names. Is there some way to make this work my way, or am I going to have to do some other dirty work-around?

Dave Scheck 23-12-2004 15:32

Re: Using macros with operator symbols
 
What is the intention of your code? Are you trying to set FOO and increment BAR? Or are you just trying to increment BAR through some sort of alternative interface?

jgannon 23-12-2004 15:40

Re: Using macros with operator symbols
 
Quote:

Originally Posted by Dave Scheck
What is the intention of your code? Are you trying to set FOO and increment BAR? Or are you just trying to increment BAR through some sort of alternative interface?

FOO isn't intended to be an actual variable... it's just a macro, to make the the code significantly easier to understand for the kids I'm training. So, I guess I'm just trying to increment BAR through an alternative interface. Also, there may be a reason that I'll want to change BAR to a different variable, so I only want to have to change it in one place.

Dave Scheck 23-12-2004 15:51

Re: Using macros with operator symbols
 
How about this...

#define incr(var,n) ((var) += (n))

Then your code would look like...
incr(x,10);
incr(y,5);

This allows a single macro to increment whatever variable is passed to it.

From your original question, though, you seem to be wanting to hide the BAR variable from the user. Is there a reason for that?

Greg Ross 25-12-2004 17:11

Re: Using macros with operator symbols
 
Quote:

Originally Posted by Dave Scheck
How about this...

#define incr(var,n) ((var) += (n))

Then your code would look like...
incr(x,10);
incr(y,5);

This allows a single macro to increment whatever variable is passed to it.

From your original question, though, you seem to be wanting to hide the BAR variable from the user. Is there a reason for that?

If you want to hide the variable, how about
Code:

#define FOO(x) (BAR += (x))
When you use the macro, then you will write
Code:

FOO(5);
instead of
Code:

FOO=5
and the precompiler will output
Code:

(BAR += 5);
(I wont go into the whys for the seemingly excess parentheses. We can go into that in a separate thread if desired.)


All times are GMT -5. The time now is 02:53.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi