Why is #define bad and what is the proper substitute?

后端 未结 6 1139
情深已故
情深已故 2020-11-30 12:37
#define dItemName        L\"CellPhone\"
6条回答
  •  时光取名叫无心
    2020-11-30 13:30

    #define is a preprocessor instruction that defines a macro. In your case macro dItemName with value L"CellPhone".

    Macros are bad mostly because they are processed before the actual code is. This means that they aren't subjected to scopes and to the rules of C++ syntax. If you've got a variable somewhere called dItemName, things won't probably work: you'll get hard-to-understand compilation errors due to that.

    The solution is to declare dItemName as a variable (in this case a const variable).

提交回复
热议问题