Preprocessor directive #ifndef for C/C++ code

后端 未结 7 1698
傲寒
傲寒 2021-02-12 22:25

In eclipse, whenever I create a new C++ class, or C header file, I get the following type of structure. Say I create header file example.h, I get this:

7条回答
  •  抹茶落季
    2021-02-12 22:58

    This is a common construct. The intent is to include the contents of the header file in the translation unit only once, even if the physical header file is included more than once. This can happen, for example, if you include the header directly in your source file, and it's also indirectly included via another header.

    Putting the #ifndef wrapper around the contents means the compiler only parses the header's contents once, and avoids redefinition errors.

    Some compilers allow "#pragma once" to do the same thing, but the #ifndef construct works everywhere.

提交回复
热议问题