哪里包含头文件/样式问题? [英] Where to include header files / A question of style?

查看:63
本文介绍了哪里包含头文件/样式问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我对我应该包含头文件的位置感到有些困惑,并且想知道是否有某些约定。

。想象一下,我写了一个

类foo并将定义放在foo.h中,并在foo.cpp中实现其

成员函数(显然#inludes foo) .h)并且进一步

假设它依赖于bar.h中定义的类栏。


似乎有以下两种情况:


1)foo.h和foo.cpp都依赖于bar.h(例如,它是

方法的参数)。


2)只有foo.cpp依赖于bar.h(例如,它用于实现

foo的方法之一。


因此,对于方案(1),我应该只在foo.h中#include bar.h还是我应该在$ f $ b中将#include包含在foo中.cpp?我意识到假设正确的标题

警卫在bar.h中没关系,但我想知道是否有一个

约定,如果有的话为什么?


方案(2)怎么样?我应该只在foo.cpp中#include bar.h,在foo.h中只包含

或b oth?再次,是否有约定,如果有,为什么?


提前感谢任何回复。我意识到这可能是一个愚蠢的问题,但我对C ++很陌生,我想尽可能遵循任何

建立的惯例。


-exits

Hello,

I''m a little confused about where I should include header files and was
wondering whether there was some convention. Imagine I''ve written a
class foo and put the definition in foo.h and the implementation of its
member functions in foo.cpp (which obviously #inludes foo.h) and further
assume that it depends on a class bar which is defined in bar.h.

It seems that there are the following two scenarios:

1) Both foo.h and foo.cpp depend on bar.h (eg, it''s an argument to a
method).

2) Only foo.cpp depends on bar.h (eg, it''s used in the implementation of
one of foo''s methods.

So, for scenario (1), should I #include bar.h in only foo.h or should I
also #include it in foo.cpp? I realize that assuming proper header
guards in bar.h it shouldn''t matter but I''m wondering if there is a
convention and if so why?

What about scenario (2)? Should I #include bar.h in only foo.cpp, only
in foo.h or in both? Again, is there a convention and if so why?

Thanks in advance for any replies. I realise this may be a silly
question but I''m pretty new to C++ and I''d like to follow any
established conventions whenever possible.

-exits

推荐答案

"退出漏斗 <前********* @ noyahoospam.com>写道...
"Exits Funnel" <ex*********@noyahoospam.com> wrote...
我对我应该包含头文件的位置感到有点困惑,并且想知道是否有某些约定。想象一下,我写了一个类
foo并将定义放在foo.h中,并在foo.cpp中实现其成员
函数(显然#inludes foo.h)并进一步假设
似乎有以下两种情况:

1)foo.h和foo.cpp依赖于bar.h(例如,它是
方法的参数)。

2)只有foo.cpp依赖于bar.h(例如,它'用于实现foo方法之一。

因此,对于方案(1),我应该只在foo.h中#include bar.h还是应该
#include它在foo.cpp中?


我把它包含在两者中。简单的规则就是这样:永远不要依赖间接

包含总是明确的。如果''foo.cpp''需要(使用)任何类或

符号''bar.h''定义,请将''bar.h''包含在''foo中.cpp''。与

''foo.h''相同。

我意识到在bar.h中假设正确的标题守卫它并不重要
但我想知道是否有一个约定,如果是这样,为什么?


没有约定。当谈到使用一种有点严格(但有限)规则的语言时,规则是唯一的事情

每个人都遵循(或者应该,无论如何) 。

方案(2)怎么样?我应该只在foo.cpp中#include bar.h,只在
foo.h或两者中?再次,是否有一个约定,如果是这样的原因?
I''m a little confused about where I should include header files and was
wondering whether there was some convention. Imagine I''ve written a class
foo and put the definition in foo.h and the implementation of its member
functions in foo.cpp (which obviously #inludes foo.h) and further assume
that it depends on a class bar which is defined in bar.h.

It seems that there are the following two scenarios:

1) Both foo.h and foo.cpp depend on bar.h (eg, it''s an argument to a
method).

2) Only foo.cpp depends on bar.h (eg, it''s used in the implementation of
one of foo''s methods.

So, for scenario (1), should I #include bar.h in only foo.h or should I
also #include it in foo.cpp?
I include it in both. The simple rule is this: never rely on indirect
inclusion, always be explicit. If ''foo.cpp'' needs (uses) any classes or
symbols ''bar.h'' defines, include ''bar.h'' into ''foo.cpp''. Same with the
''foo.h''.
I realize that assuming proper header guards in bar.h it shouldn''t matter
but I''m wondering if there is a convention and if so why?
There is no convention. When it comes to using a language that has
somewhat rigit (but limited) set of rules, the rules are the only thing
everybody follows (or supposed to, anyway).
What about scenario (2)? Should I #include bar.h in only foo.cpp, only in
foo.h or in both? Again, is there a convention and if so why?




只在需要的地方包括它。


V



Include it [only] where it''s needed.

V


* Victor Bazarov:
* Victor Bazarov:
"退出漏斗 <前********* @ noyahoospam.com>写道...
"Exits Funnel" <ex*********@noyahoospam.com> wrote...
我对我应该包含头文件的位置感到有点困惑,并且想知道是否有某些约定。想象一下,我写了一个类
foo并将定义放在foo.h中,并在foo.cpp中实现其成员
函数(显然#inludes foo.h)并进一步假设
似乎有以下两种情况:

1)foo.h和foo.cpp依赖于bar.h(例如,它是
方法的参数)。

2)只有foo.cpp依赖于bar.h(例如,它'用于实现foo方法之一。

因此,对于方案(1),我应该只在foo.h中#include bar.h还是应该
#include它在foo.cpp中?
I''m a little confused about where I should include header files and was
wondering whether there was some convention. Imagine I''ve written a class
foo and put the definition in foo.h and the implementation of its member
functions in foo.cpp (which obviously #inludes foo.h) and further assume
that it depends on a class bar which is defined in bar.h.

It seems that there are the following two scenarios:

1) Both foo.h and foo.cpp depend on bar.h (eg, it''s an argument to a
method).

2) Only foo.cpp depends on bar.h (eg, it''s used in the implementation of
one of foo''s methods.

So, for scenario (1), should I #include bar.h in only foo.h or should I
also #include it in foo.cpp?



我把它包含在两者中。简单的规则是这样的:永远不要依赖间接的包含,永远是明确的如果''foo.cpp''需要(使用)任何类或
符号''bar.h''定义,请将''bar.h''包含在''foo.cpp''中。与
''foo.h''。



I include it in both. The simple rule is this: never rely on indirect
inclusion, always be explicit. If ''foo.cpp'' needs (uses) any classes or
symbols ''bar.h'' defines, include ''bar.h'' into ''foo.cpp''. Same with the
''foo.h''.




在这种情况下,没有任何隐含的内容:[foo.h]是头文件

[foo.cpp]和_defines_外部可见的依赖关系[foo.cpp]。


-

答:因为它弄乱了人们通常阅读文字的顺序。

问:为什么这么糟糕?

A:热门发布。

问:usenet和电子邮件中最烦人的是什么?



In this case there''s nothing implicit about it: [foo.h] is the header file
for [foo.cpp] and _defines_ the externally visible dependencies of [foo.cpp].

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


" Alf P. Steinbach" <人*** @ start.no>写道...
"Alf P. Steinbach" <al***@start.no> wrote...
* Victor Bazarov:
* Victor Bazarov:
"退出漏斗" <前********* @ noyahoospam.com>写道......
"Exits Funnel" <ex*********@noyahoospam.com> wrote...
>我对我应该包含头文件的位置感到有点困惑,并且是
>想知道是否有一些惯例。想象一下,我写了一个
>上课
> foo并将定义放在foo.h中并实现其
>会员
> foo.cpp中的函数(显然#inludes foo.h)和进一步的
>假设
>这取决于bar.h中定义的类栏。
>
>似乎有以下两种情况:
>
> 1)foo.h和foo.cpp都依赖于bar.h(例如,它是
>方法的参数)。
>
> 2)只有foo.cpp依赖于bar.h(例如,它用于执行
> of
> foo的方法之一。
>
>那么,对于方案(1),我应该只在foo.h中#include bar.h还是我应该在foo.cpp中#include它?
> I''m a little confused about where I should include header files and was
> wondering whether there was some convention. Imagine I''ve written a
> class
> foo and put the definition in foo.h and the implementation of its
> member
> functions in foo.cpp (which obviously #inludes foo.h) and further
> assume
> that it depends on a class bar which is defined in bar.h.
>
> It seems that there are the following two scenarios:
>
> 1) Both foo.h and foo.cpp depend on bar.h (eg, it''s an argument to a
> method).
>
> 2) Only foo.cpp depends on bar.h (eg, it''s used in the implementation
> of
> one of foo''s methods.
>
> So, for scenario (1), should I #include bar.h in only foo.h or should I
> also #include it in foo.cpp?



我把它包含在两者中。简单的规则就是:永远不要依赖于间接的包含,总是明确的。如果''foo.cpp''需要(使用)任何类或
符号''bar.h''定义,包括''bar.h''到''foo.cpp''。与
''foo.h''相同。



I include it in both. The simple rule is this: never rely on indirect
inclusion, always be explicit. If ''foo.cpp'' needs (uses) any classes or
symbols ''bar.h'' defines, include ''bar.h'' into ''foo.cpp''. Same with the
''foo.h''.



在这种情况下,它没有任何暗示:[foo.h]是[foo.cpp]的头文件
和_defines_
[foo.cpp]的外部可见依赖项]。



In this case there''s nothing implicit about it: [foo.h] is the header file
for [foo.cpp] and _defines_ the externally visible dependencies of
[foo.cpp].




主题有不同的变化。


想象一下,你开始用

-------------------------------- foo.h

#include" bar.h" ;

class somefoocl屁股{blah blah

somebarclass * pbar;

};

---------------- ---------------- foo.cpp

#include" foo.h" //让我们依赖foo.h',包括bar.h


......不知怎的,使用pbar

------ --------------------------


现在,想象一下你看了编译的速度并决定了

你不需要''bar.h''包含在''foo.h'中,而是可以

与前锋一起生活(甚至在-place)声明:


--------------------------------- --- foo.h(版本2)

class somefooclass {blah blah

class somebarclass * pbar;

};

------------------------------------

现在你的foo.cpp(以及依赖于间接

包含的所有其他文件)被搞砸了,需要注意。如果你确实在那些开头包含

''bar.h'',就没有必要更改文件中的
,只是为了重新编译它们(因为' 'foo.h''标题改变了。)


V



There are variations on the theme.

Imagine that you begin with
-------------------------------- foo.h
#include "bar.h"
class somefooclass { blah blah
somebarclass* pbar;
};
-------------------------------- foo.cpp
#include "foo.h" // let''s rely on foo.h''s including bar.h

... use pbar somehow
--------------------------------

Now, imagine that you looked at the compilation speed and decided
that you don''t need ''bar.h'' included in ''foo.h'' and instead can
live with a forward (or even in-place) declaration:

------------------------------------ foo.h (version 2)
class somefooclass { blah blah
class somebarclass* pbar;
};
------------------------------------
Now your foo.cpp (and every other file that relied on the indirect
inclusion) is screwed and requires attention. If you did include
''bar.h'' in those to begin with, there would be no need to change
the files, only to recompile them (since ''foo.h'' header changed).

V


这篇关于哪里包含头文件/样式问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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