gpt4 book ai didi

c++ - #define 标签的限制范围

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:49:27 25 4
gpt4 key购买 nike

限制 #define 标签范围并避免不必要的 token 冲突的正确策略是什么?

在以下配置中:

主.c

# include "Utility_1.h"
# include "Utility_2.h"
# include "Utility_3.h"
VOID Main() { ... }

Utility_1.h

# define ZERO "Zero"
# define ONE "One"
BOOL Utility_1(); // Uses- ZERO:"Zero" & ONE:"One"

Utility_2.h

# define ZERO '0'
# define ONE '1'
BOOL Utility_2(); // Uses- ZERO:'0' & ONE:'1'

Utility_3.h

const UINT ZERO = 0;
const UINT ONE = 1;
BOOL Utility_3(); // Uses- ZERO:0 & ONE:1

注意 Utility_1Utility_2Utility_3 是独立编写的


Error: Macro Redefinition and Token Collision
Also, Most Worrying: Compiler does not indicate what replaced what incase of token replacement

{Edit} 注意:这是一个通用问题,所以请不要提出 enumconst

即什么时候做什么:我必须使用 #define & _请在下面评论我提出的解决方案.. __

最佳答案

正确的策略是不使用

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

完全没有。如果您需要常量值,请在本例中使用 const char,并将其包装在命名空间中。

关于c++ - #define 标签的限制范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12641091/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com