Strings and Definitions

Started by
13 comments, last by SiCrane 15 years, 8 months ago
Hey guys :), This question seems to be a very easy one but I couldn't find anything online to help me out so that's why I'm posting. Well now for the problem, when you have a definition such as - #define Up 'W' and a function such as - [code Win32] GetAsyncKeyState(Up) - would it be the same as just saying/typing - [code Win32] GetAsyncKeyState('W') - ? Thanks a lot.
Advertisement
For better or worse (mostly worse), yes.
Since when did GetAsyncKeyState accept a char argument? Even if it did, you would be better of with const char Up='W'. Actually, if you don't make it const, you can change which key is the up key, ie, user settings.
I'm sure others with expand on this, but keep in mind that in C++ there is (almost) zero reason to use macros.

So whenever you think to use a macro, first consider whether there a way to achieve your goal without using a macro.

For example:

const char kUP = 'W';
Thanks for the answears guys :).

It never did accept chars but a '' is different then a "" which is why '' works and "" doesn't :). So there's one question resolved but now I got another which doesn't reall need another thread :) - How do I let the user select which keys he wants to use(for the game I'm making)? Also what is the virtual keycode for the @ symbol?
Moved to For Beginners.
Well I guess that made sense... although its a little offensive if you think about it :D

Anyway don't answer my question because I just used common sense to figure out that I needed to use INTs to store the key values...

RESOLVED
Oh damn yet another problem... what is the VK key code for @? I keep looking yet I can't find it... hard to believe its no where on the net... the other VKs are... if someone can find it I'm more stupid then a donkey :S

So the request is to please help me find the VK for @.

EDIT - I just realized fpsgamers's post. Why wouldn't you use macros? I use them all the time to keep the program somewhat easier for me to use it next time or for me to make simple customizations without having to spend a long time looking for the variable associated with it. Is there some kind of drawback to using macros?

Thank you :(.
Quote:Original post by WinRad
EDIT - I just realized fpsgamers's post. Why wouldn't you use macros? I use them all the time to keep the program somewhat easier for me to use it next time or for me to make simple customizations without having to spend a long time looking for the variable associated with it. Is there some kind of drawback to using macros?

Yes, there is a drawback to using macros. First, macros don't follow any scoping rules. For example, if you did

#define PI 3.14

It would mean that you could never have any variable named PI, because if you ever wrote PI, it would just get replaced by 3.14. However, if you did

namespace mynamespace
{
    const double PI = 3.14;
}

You wouldn't prevent others from using the variable name PI outside of mynamespace. Let's say you are using a physics API in you project called PhysicsAPI. You didn't write PhysicsAPI, but you are including it in your project. If the people that wrote PhysicsAPI used #define PI 3.14, that would totally screw you over if you had your own variable called PI or if you included another API that also tried to #define PI 3.14. However, if the PhysicsAPI people did

namespace PhysicsAPI
{
    const double PI = 3.14;
}

Then you could also include another API that does

namespace SomeRandomAPI
{
    const double PI = 3.14;
}

And you wouldn't have any problems with the variable names since they are in their own namespaces and variables obey scoping rules, unlike macros.

Another downside to macros can be seen in switch statements. For example, if I have a switch statement that switches based on an enum I give it, my compiler will warn me if I don't handle all possible values in the switch statement (and it will tell me specifically which ones I don't handle, which is great for me incase I miss one). If I use a macro, there are a billion (actually more than that) possible cases for the switch statement, which means my compiler can't give me a nice warning like it did for the enum.

And another downside to macros is that they don't provide the same type safety as const variables do. const char globalChar = 57 tells my compiler a lot more about the variable and how I want to use it than #define globalChar 57 does.

And again, another problem with macros is the infamous max macro. I'll let you research that one though. There should be plenty of posts here on GameDev that explain it.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
Well that seems to make sense... Hmm but what if the macros I use are very long and specific and I only need them in one case? For example in my game I use a macro called Player1UpDef which is very specific and most likely would never be used by me again because I add def to the end of every #define macro.

And I already know what MAX is :). Although I have to say it a little dumb of Microsoft to add a macro like that... its just too common(the word max).

Also I need to know the VK for the @ sign :).

Thanks.

This topic is closed to new replies.

Advertisement