How do you use #define?

前端 未结 8 814
鱼传尺愫
鱼传尺愫 2020-12-11 15:03

I\'m wondering about instances when it makes sent to use #define and #if statements. I\'ve known about it for a while, but never incorporated it into my way of coding. How e

相关标签:
8条回答
  • 2020-12-11 15:18

    It's for conditional compilation, so you can include or remove bits of code based upon project attributes which tend to be:

    • Intended platform (Windows/Linux/XB360/PS3/Iphone.... etc)
    • Release or Debug (Generally logging, asserts etc are only included in a debug build)

    They can also be used to disable large parts of a system quickly, for example, during development of a game, I might define

    #define PLAYSOUNDS
    

    and then wrap the final call to play a sound in:

    #ifdef PLAYSOUNDS
    // Do lots of funk to play a sound
    return true;
    #else
    return true;
    

    So it's very easy for me to turn on and off the playing of sounds for a build. (Typically I don't play sounds when debugging because it gets in the way of my personal music :) ) The benefit is that you're not introducing a branch through adding an if statement....

    0 讨论(0)
  • 2020-12-11 15:19

    I often find myself defining some things that are done repetitively in certain functions. That makes the code much shorter and thus allows a better overview.

    But as always, try to find a good measure to not create a new language out of it. Might be a little hard to read for the occasional maintenance later on.

    0 讨论(0)
  • 2020-12-11 15:19

    Perhaps the most common usees of #define in C# is to differentiate between debug/release and different platforms (for example Windows and X-Box 360 in the XNA framework).

    0 讨论(0)
  • 2020-12-11 15:23

    #define is used to define compile-time constants that you can use with #if to include or exclude bits of code.

    #define USEFOREACH
    
    #if USEFOREACH
        foreach(var item in items)
         {  
    #else
        for(int i=0; i < items.Length; ++i)
         { var item = items[i];   //take item
    #endif
    
           doSomethingWithItem(item);
         }
    
    0 讨论(0)
  • 2020-12-11 15:31

    Well, defines are used often for compile time constants and macros. This can make your code a bit faster as there are really no function calls, the output values of the macros are determined at compile time. The #if's are very useful. The most simple example that I can think of is checking for a debug build to add in some extra logging or messaging, maybe even some debugging functions. You can also check different environment variables this way.

    Others with more C/C++ experience can add more I am sure.

    0 讨论(0)
  • 2020-12-11 15:32

    Is #define the only thing that determines if the code is included when compiled? If I have #define DEBUGme as a custom symbol, the only way to exclude it from compile is to remove this #define statement?

    You can undefine symbols as well

    #if defined(DEBUG)
    #undef DEBUG
    #endif
    
    0 讨论(0)
提交回复
热议问题