문제

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
도움이 되었습니까?

해결책

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

다른 팁

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

Is #pragma once a safe include guard?

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top