Limit scope of #define labels

后端 未结 9 845
被撕碎了的回忆
被撕碎了的回忆 2020-12-19 05:39

What is the correct strategy to limit the scope of #define labels and avoid unwarranted token collision?

In the following configuration:

9条回答
  •  感情败类
    2020-12-19 06:34

    1. I think the correct strategy would be to place #define labels - in only the implementation, ie. c, files
    2. Further all #define could be put separately in yet another file- say: Utility_2_Def.h
      (Quite like Microsoft's WinError.h:Error code definitions for the Win32 api functions)

      Overheads:

      1. an extra file
      2. an extra #include statement

      Gains:

      1. Abstraction: ZERO is: 0, '0' or "Zero" as to where you use it
      2. One standard place to change all static parameters of the whole module

    Utility_2.h

    BOOL Utility_2();
    

    Utility_2_Def.h

    # define ZERO '0'
    # define ONE  '1'
    

    Utility_2.c

    # include "Utility_2.h"
    # include "Utility_2_Def.h"
    
    BOOL Utility_2()
    {
        ...
    }
    

提交回复
热议问题