Why use #define instead of a variable

后端 未结 8 1433
清酒与你
清酒与你 2020-12-04 06:19

What is the point of #define in C++? I\'ve only seen examples where it\'s used in place of a \"magic number\" but I don\'t see the point in just giving that val

8条回答
  •  臣服心动
    2020-12-04 06:28

    Define is evaluated before compilation by the pre-processor, while variables are referenced at run-time. This means you control how your application is built (not how it runs)

    Here are a couple examples that use define which cannot be replaced by a variable:

    1. #define min(i, j) (((i) < (j)) ? (i) : (j))
      note this is evaluated by the pre-processor, not during runtime

    2. http://msdn.microsoft.com/en-us/library/8fskxacy.aspx

提交回复
热议问题