Include guard style, C++

丶灬走出姿态 提交于 2019-12-11 05:34:23

问题


I have a .h file which contains several class definitions. I'd like to use C++'s include guards in this file; however, I was wondering which way of using the include guards is considered proper/correct?

One guard protecting everything

#ifndef FOO_BAR
#define FOO_BAR

class Foo
{
};

class Bar
{      
};

#endif

or multiple separate guards.

#ifndef FOO
#define FOO

class Foo
{
};

#endif

#ifndef BAR
#define BAR

class Bar
{      
};

#endif

回答1:


They are include guards, preventing double inclusion of files. So they should be defined once per file, not per class or function or whatever.




回答2:


Have you considered to use #pragma once ? Most modern compilers support it.

Is #pragma once a safe include guard?



来源:https://stackoverflow.com/questions/12327031/include-guard-style-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!