Skip to main content
GameDev.net gamedev.net
🔒 Locked

Preprocessor help (C++)

Started by Rattrap Jan 23, 2006 at 2:41 PM 6 replies 800+ views
Original Post
Rattrap
Rattrap
I'm not sure this is possible, but I want to try and assign a #define value through macro. I'm trying to avoid using this as a global constant because ultimately this number produced will only be used once. And because of some of the crossplatform nature of this code, it is only relavent if compiled for a Windows machine, though this may be unavoidable. I want to have a macro SetWinsockVersion(x,y) which would create a define that stores the result of a calculation in a preprocessor define called WINSOCKVER. So what I want is something along the lines of WINSOCKVER = SetWinsockVersion(x,y) exception I don't think this is possible using just the preprocessor. Let me know if that made no sense what so ever.
"I can't believe I'm defending logic to a turing machine." - Kent Woolworth [Other Space]
erissian
erissian
#define SetWinsockVersion(x,y) /* code goes here */#define WINSOCKVER SetWinsockVersion(arg1,arg2)


Like that? Do you know your x and y beforehand?

Does it really matter that it's only used once? It might seem like a waste of 4 bytes of memory, but I think you're trying to be too conservative.
We''re sorry, but you don''t have the clearance to read this post. Please exit your browser at this time. (Code 23)
Rattrap
Rattrap
I was just trying to avoid creating a global constant.
"I can't believe I'm defending logic to a turing machine." - Kent Woolworth [Other Space]
Sneftel
Sneftel
Don't avoid it. Global constants are the way to do it in C++. Do not use #defines for constants. If your user needs four bytes of memory, he can use a post-it note.
Rattrap
Rattrap
It this case its not as much about the memory as to try and avoid creating a "constant" that will only be used in special compiling situtations. I'm probably going to have to endup using the global constant.
"I can't believe I'm defending logic to a turing machine." - Kent Woolworth [Other Space]
Sneftel
Sneftel
Quote:
Original post by Rattrap
It this case its not as much about the memory as to try and avoid creating a "constant" that will only be used in special compiling situtations. I'm probably going to have to endup using the global constant.


You can use a namespace if you like. Alternatively, make it a static member variable of the class which actually cares about it.
stylin
stylin
Quote:
Original post by Sneftel
You can use a namespace if you like. Alternatively, make it a static member variable of the class which actually cares about it.


Depending on the compiler, you may have to employ an "enum hack" if you need the value at compile-time. Enums are similar to #defines in that their addresses cannot be taken. I would try and put it in a namespace, if at all possible though.
:stylin: "Make games, not war." "...if you're doing this to learn then just study a modern C++ compiler's implementation." -snk_kid

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.