什么'#define xxx'(没有价值!)做,与'#define xxx yyy'(有价值)形成鲜明对比?

Avi*_*ohn 0 c++ macros c-preprocessor

#define <text> <substitute>定义一个宏.但是#define <text>,那是什么呢?那是做什么的?例如在标题保护的上下文中.

Mik*_*our 5

它还定义了一个宏,无需替换.

例如在标题保护的上下文中.

在这种情况下,我们只需要知道是否已经定义了保护宏 - 我们可以用#ifdef或检查它#ifndef.它不需要被定义为任何东西,所以我们通常不会为任何事情烦恼.这给出了通常的防守形式

#ifndef HEADER_H    // check whether it's defined, skip to "endif" if so
#define HEADER_H    // define it; doesn't need to contain anything

// header contents

#endif              // skip to here (end of header) if already defined
Run Code Online (Sandbox Code Playgroud)

包含它一次将包含内容并定义宏; 包括它第二次将无害地跳过内容,而不是重复它们(可能)导致错误.