93

我对 C++ 中的预处理器指令有疑问:

例如:

#ifndef QUESTION

//some code here

#ifndef QUESTION

//some code here

#endif

#endif

我们可以这样使用吗,C++编译器能正确匹配ifndefandendif吗?

4

3 回答 3

121

我们可以。该#endif语句与#if #ifdef没有#ifndef对应的#endif.

例如

#if  ----------|
#if  -----|    |
#endif ---|    |
#endif --------|
于 2011-07-13T11:54:34.340 回答
55

是的,你可以嵌套#if/#endif块。一些 C 编码风格会告诉你写

#ifdef CONDITION1
# ifdef CONDITION2
# endif
#endif

使用空格来表示嵌套级别。

于 2011-07-13T11:55:46.567 回答
0

在您的代码中,#ifndef QUESTION 部分将被丢弃,除非您使用#undef QUESTION。

祝你好运!

于 2011-07-13T11:59:04.307 回答