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

后端 未结 6 1148
情深已故
情深已故 2020-11-30 12:37
#define dItemName        L\"CellPhone\"
6条回答
  •  鱼传尺愫
    2020-11-30 13:29

    Define it as a constant variable. It is a good programming practice.

    const wchar_t *dItemName = L"CellPhone";
    

    In case you need to know the lenght of your string somewhere later, then define it as an array:

    const wchar_t dItemName[] = L"CellPhone";
    

    Also, why #define is bad: It transforms all places where you use word dItemName to L"CellPhone". Example:

    struct {
      int dItemName;
    } SomeStruct;
    

    will become invalid:

    struct {
      int L"CellPhone";
    } SomeStruct;
    

提交回复
热议问题