Preprocessor directive #ifndef for C/C++ code

后端 未结 7 1702
傲寒
傲寒 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:53

    Always do this at the top of a header file. It's typically called a header guard or an include guard.

    What it does is make it so that if a header file would be included multiple times, it will only be included once. If you don't do it, then you'll end up with errors about things being defined multiple times and things like that.

    The exact define doesn't matter that much, though typically it's some variation on the file name. Basically, you're checking whether the given macro has been defined. If it hasn't, then define it, and continue with including the file. If it has, then you must have included the file previously, and the rest of the file is ignored.

提交回复
热议问题