C近拍保护的定义 [英] C Macro to protect definitions

查看:122
本文介绍了C近拍保护的定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有保护宏定义的方法吗?更具体考虑以下几点:

Is there a way to protect a macro definition? To be more specific consider the following:

#define macro1 x

// code segment 1 involving macro1 goes here 

// code segment 2 involving macro1 goes here

// code segment 3 involving macro1 goes here

在这个例子我已经把3条评论表示,涉及宏观code段。我想现在要做的就是要能够避免宏观影响code段2.有没有办法告诉编译器,以取代Macro1的的所有实例段1和段3而不是在段2?这里是一个可能的方式:

In the example I have placed 3 comments denoting code segments involving the macro. What I would like to do now is to be able to avoid having the macro affect code segment 2. Is there a way to tell the compiler to replace all instances of macro1 in segment 1 and segment 3 but not in segment 2? Here is a possible way:

#define macro1 x

// code segment 1 involving macro1 goes here 

#undef macro1

// code segment 2 involving macro1 goes here

#define macro1 x

// code segment 3 involving macro1 goes here

的缺点是,我必须重新定义宏。说我想用这个词
在我的计划NULL(不要问我为什么刚才用它去)。我想这是一个变量
但C preprocessor在大多数情况下,将其更改为0。所以我想做的事,就是要
能够阻止它只是一个很短的时间,然后让它成为它是什么了。

The drawback is that I have to define the macro again. Say I wanted to use the word NULL in my program (don't ask me why just go with it). I want this to be a variable but the C preprocessor will change it to 0 in most cases. So what I want to do, is to be able to block it for just a short time period and then let it be what it was before.

失败的尝试:

让我们假设Macro1的已定义外的地方,我们甚至不知道什么是这种宏观的价值。所有我们需要的是,以避免它替换第二段的东西。

Let us suppose that macro1 has been defined somewhere externally and we do not even know what the value of such macro is. All we want is to avoid having it replace things in the second segment.

// code segment 1 involving macro1 goes here 

#ifdef macro1
#define TEMP macro1
#undef macro1
#endif

// code segment 2 involving macro1 goes here

#ifdef TEMP
#define macro1 TEMP
#undef TEMP
#endif

// code segment 3 involving macro1 goes here

这样做是为了检查是否存在宏,如果确实如此,那么我们要的值存储到另一个变量,取消定义宏最后又定义宏的时候,我们需要它。
不幸的是,code没有,因为我们执行code段前3 Macro1的将TEMP被替换,而不是TEMP应该具有的价值的工作。

The idea is to check if the macro exists, if it does, then we want to store the value into another variable, undefine the macro and finally define the macro again when we need it. Unfortunately, this code doesn't work because before we execute code segment 3 macro1 will be replaced by TEMP, not the value that TEMP was supposed to have.

推荐答案

要拥有code不被宏冲击一块最简单的方法就是把宏的标题。 ^ h 文件,不包括被越来越编译源文件中的头文件。

The easiest way to have a chunk of code not be impacted by a macro is to put the macro in a header .h file and NOT include the header file in the source file that is getting compiled.

这意味着你可能要砍一个更大的文件,让您的code不应由宏出受到影响的部分。是的,这是一个痛苦,但它听起来像它不应该与code反正其余(或它不会是一个问题)。

This means you might have to "chop up" a bigger file to get your section of code that shouldn't be impacted by the macro out. Yes, it is a pain, but it sounds like it shouldn't be with the rest of the code anyway (or it wouldn't be an issue).

这篇关于C近拍保护的定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆