macro (#define) question

This is a discussion on macro (#define) question within the C++ Programming forums, part of the General Programming Boards category; Try using the stringizing operator #define PRNVARIABLE(a, b) cout << #a << " = " << a; void main() { ...

  1. #16
    Registered User
    Join Date
    Nov 2001
    Posts
    66

    You wanna print a Variable name?????

    Try using the stringizing operator

    #define PRNVARIABLE(a, b) cout << #a << " = " << a;

    void main()
    {
    PRNVARIABLE(a, a)
    }

    what the stringizing operator does it just prints out what you type in as a string.

    void main()
    {
    PRNVARIABLE(5 * 5, b)
    }

    would print 5 * 5 = (whatever b is)

    Ryan

  2. #17
    Darrok
    Guest
    no, if youd read what im trying to do, im trying to have it replace a macro with the name of a variable, but im trying to get it to change what the macro is defined as inside a macro so that i can output the same code twice but have it use a different variable each time

    like if i do this

    int blah
    int blah2

    #define variable blah

    #define Code(x) x

    Code(variable+=1

    will add 1 to the blah variable

    but i want it to change what variable is defined as so i can do this

    #define Code(x) x \
    /*code to change the value of the variable macro to blah2*/ \
    x

    that way when i do Code(variable+=1 itll expand to this

    blah+=1; blah2+=1;

    in my battle system i found that i was writing most code twice, once for the party members and once for the enemies. i originally had them in the same array, but it took a lot of extra code to distinguish between a party member and an enemy

  3. #18
    Registered User
    Join Date
    Nov 2001
    Posts
    66
    Sounds like like you need to overload a operator. Unless you NEED to do this with a macro.

  4. #19
    Registered User
    Join Date
    Nov 2001
    Posts
    66
    Sorry about that last reply I accidently hit Submit Reply.

    OK what you wanna do is have a macro that prints out blah+=1; and blah2+=1;

    alright if that is true try this

    #define BlahMacro(blah1, blah2) {blah+=1; blah2+=1}

    ok with this you enter a variable for each blah so in main it would look lik this

    void main()
    {
    BlahMacro(1, 2)
    }


    so in the end it will be blah = 1 + 1; and blah2 = 2 + 1;

    hope this helps.

    Ryan

  5. #20
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It sounds like you need to redo your battle system. Post the sample code and I'll take a look. Perhaps you should use a vector or a linked list to store who is in your party?
    Code:
    #define INCREMENTPARTY(x) \
        for( int ipc=0; ipc<sizeof(party); ipc++ ) \
            if( isInParty(party[ipc], partyLeader) ) party[ipc]++;
    Shrug. Anyway, run it by us and we'll take a look.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #21
    Darrok
    Guest
    no, i want to input some code, and in the code is the name of 2(or 4 in some cases) non-function macros that are replaced with the name of variables or values, but these variable names and values are changed halfway through

    here is some code that im using right now (i came up with a solution for what i want to do, but it's slower than if i'd just type the code twice)

    Code:
    GroupCode(for (x=0, temp2=0;x<NUMGROUP;x++)
    	{
    		if (GROUPCHAR[x].set && (GROUPCHAR[x].atb >= 30-GROUPCHAR[x].speed && GROUPCHAR[x].hp > 0))
    		{
    			temp = rand()%NUMGROUP2;
    			if (GROUPCHAR2[temp].hp < 1)
    			{
    				while (GROUPCHAR2[temp].hp < 1 && temp2 < NUMGROUP2) 
    				{
    					temp++;
    					if (temp >= NUMGROUP2) temp = 0;
    					temp2++;
    				}
    			}
    			GROUPCHAR[x].atb = 0;
    			fight(groupchar[x], GROUPCHAR2[temp]);
    		}
    	},;);
    GroupCode is a macro that prints out the code twice and changes some variables (im hoping that it'll just change the value of some defines though, because the variable solution is slower and now always compatable)

    this is GroupCode right now

    Code:
    #define GroupCode(code, code2) GROUPCHAR = partychar; \
    	GROUPCHAR2 = enemychar; \
    	NUMGROUP = NUMPARTY; \
    	NUMGROUP2 = NUMENEMY; \
    	code \
    	code2 \
    	GROUPCHAR = enemychar; \
    	GROUPCHAR2 = partychar; \
    	NUMGROUP = NUMENEMY; \
    	NUMGROUP2 = NUMPARTY; \
    	code
    groupchar and groupchar2 are pointers to arrays of structures that contain a character's stats
    numgroup and numgroup2 are integer variables that contain the maximum amount of enemies or party members

    however, this code needs to set some variables, which is slower than if i'd just type it by hand and change which values are being used

    what i want it to do is this (using incorrect code, indented to show you where it would go)

    Code:
    #define GroupCode(code, code2) #define GROUPCHAR partychar \
    	#define GROUPCHAR2  enemychar \
    	#define NUMGROUP NUMPARTY \
    	#define NUMGROUP NUMENEMY \
    	code \
    	code2 \
    	#define GROUPCHAR enemychar \
    	#define GROUPCHAR2 partychar \
    	#define NUMGROUP NUMENEMY \
    	#define NUMGROUP2 NUMPARTY \
    	code
    NUMENEMY and NUMPARTY are defined to be 6 and 4 respectively

  7. #22
    Registered User
    Join Date
    Dec 2001
    Posts
    8
    well, i made some typos, but you should get the idea

    that's the code to decide who a person who's not being controlled (and since i dont have any controls yet, that's everyone) is to attack

  8. #23
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    So what you're trying to do is something like -

    int x=1;
    int y=2;

    #define xy x

    #define foo #undef xy #define xy y

    I'm pretty sure there would be no way of getting the preprocessor to do that.

    You seem to be doing things back to front. Instead of passing the code into the macros why not pass the variables? The code would then make up the body of the macro. Since the variables are of the same type you could even use an inline functions instead of the macro and get the same results.
    zen

  9. #24
    Registered User
    Join Date
    Dec 2001
    Posts
    8
    because then i would need to have a macro for every section of code that does something like that, and i dont reuse any of the code, just that several parts of code follow the same pattern

  10. #25
    Registered User
    Join Date
    Dec 2001
    Posts
    8
    you wouldnt happen to know if anyone's written a custom preprocessor, would you? i was learning 65816 asm (for the snes) a while ago, and the assembler's preprocessor was just updated a few months ago and it could do that

  11. #26
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    That looks overly complex. Why don't you just do:

    #define GROUPCHAR(x) (x)?partychar:enemychar
    #define GROUPCHAR2(x) (x)?enemycharartychar
    #define NUMGROUP(x) (x)?NUMPARTY:NUMENEMY
    #define CODE(x) (x) (x)?code:code2

    Then, in your application, use them all like

    GROUPCHAR(mybool) == blah blah do stuf

    And half way trough your code, toggle the value of 'mybool' to zero or one, and have it end up using the other value.

    Quzah.
    Hope is the first step on the road to disappointment.

  12. #27
    Registered User
    Join Date
    Dec 2001
    Posts
    8
    because they're not string, they're variable names, they cant be handled by the compiler

  13. #28
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    because they're not string, they're variable names, they cant be handled by the compiler
    I'll assume you didn't even read my macros. Who said anything about strings?

    Quzah.
    Hope is the first step on the road to disappointment.

  14. #29
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    I've read all of these posts and it looks to me that all you will do is make your code more confusing, both to you and others if you approach this problem using macros. Like quzah said macros can be very useful when used right. But they can also be very harmful when used wrong. If you find that you are almost writing your own language with a macro that has C language elements in it, you are not using them correctly. If you are using macros to insert blocks of code based on conditions, you are not using them right. Macros can be abused to the point that they can insert blocks of code into the code stream, but that is not what they were designed for. If conditional blocks can handle that much better by executing blocks of code based on conditions.
    Even in assembly language your macros should not turn into your own language or a subset of assembly.

    Since this is the C++ board and you are working with objects, my bet is that writing classes for your characters would be much better. Then you could init each member of the class for the character in the appropriate constructor. You could also write functions that would alter values for the character. You would also have a class that tracked all of the characters via a linked list.
    You would not have to worry about all of the conditions and which variable to use based on them. This system could easily be extended to include dozens of characters if you write a good simple character base class and a simple container class, vector class, or some other derivative of those to track your characters within the game.

  15. #30
    Registered User
    Join Date
    Dec 2001
    Posts
    8
    Originally posted by quzah
    I'll assume you didn't even read my macros. Who said anything about strings?
    your macros are using them as though they're stings, anything that is not preprocessed that's in the macro well completely mess it up

    i cant do GROUPCHAR(1)[2].hp = 1; with that

    i was going to use classes with it, but id have to rewrite my character code (for like the 10th time in the last few days) and i really dont feel like doing that

    i wasnt use my macros for functions, simply to reprint a supplied codeblock and change some variables that are changed with the 2nd one

    in asm i just use it for conditions that would be really slow (and add a ****load of code) if not done with macros

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why?!?
    By p3rry in forum C Programming
    Replies: 3
    Last Post: 01-08-2009, 11:52 AM
  2. Would someone solve my problem?
    By Lonners in forum C Programming
    Replies: 9
    Last Post: 01-19-2008, 05:58 PM
  3. Bor to DevC++ prog convert prob
    By kryptkat in forum Windows Programming
    Replies: 16
    Last Post: 09-18-2007, 05:11 AM
  4. float toolbar!
    By c-- in forum Windows Programming
    Replies: 5
    Last Post: 02-04-2003, 08:44 AM
  5. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 05:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21