What are macros?
I'm not sure what macros are. For example the following macro 开发者_JAVA百科is found in the cocos2d framework written in objective-c. What exactly is a macro,why are they useful, and how do I define them? Thanks.
#define CCRANDOM_0_1() ((random() / (float)0x7fffffff ))
Here you'll find everything you need: http://gcc.gnu.org/onlinedocs/cpp/Macros.html
But to get a basic idea, they are pieces of code that replace the macro name when you write them in your code.
In your example, if you wrote:
int i = CCRANDOM_0_1();
It would be the same as:
int i = ((random() / (float)0x7fffffff ));
it just replaceCCRANDOM_0_1()
with ((random() / (float)0x7fffffff ))
in your code
Whenever CCRANDOM+0_1()
is seen in the program (by the compiler) it will be replaced by ((random() / (float)0x7fffffff ))
精彩评论