包括保护风格,C ++ [英] Include guard style, C++

查看:190
本文介绍了包括保护风格,C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.h文件,其中包含几个类定义。我想在这个文件中使用C ++的包含守卫;

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?

一个守卫保护所有东西

#ifndef FOO_BAR
#define FOO_BAR

class Foo
{
};

class Bar
{      
};

#endif

或多个单独的警卫。

#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.

这篇关于包括保护风格,C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆