赞成&安培;把所有code头文件在C ++中的缺点? [英] Pros & Cons of putting all code in Header files in C++?

查看:154
本文介绍了赞成&安培;把所有code头文件在C ++中的缺点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可以构建一个C ++程序,以便(几乎)所有code所在的头文件。它本质上看起来像一个C#或Java程序。但是,你至少需要一个的.cpp 文件编译时的所有头文件来拉。现在我知道有些人会绝对讨厌这种想法。但是我还没有发现这样做的任何令人信服的缺点。我可以列举一些优势:

You can structure a C++ program so that (almost) all the code resides in Header files. It essentially looks like a C# or Java program. However, you do need at least one .cpp file to pull in all the header files when compiling. Now I know some people would absolutely detest this idea. But I haven't found any convincing downsides of doing this. I can list some advantages:

[1]更快的编译时间。所有的头文件只得到解析一次,因为只有一个.cpp文件。此外,一个头文件不能被包含超过一次,否则你将得到一个构建休息。有使用另一种方法,当实现更快的编译其他的方法,但这是如此简单。

[1] Faster compile times. All header files only get parsed once, because there is only one .cpp file. Also, one header file cannot be included more than once, otherwise you will get a build break. There are other ways of achieving faster compiles when using the alternate approach, but this is so simple.

[2]它避免了循环依赖,使它们完全清楚。如果 ClassA的 ClassA.h ClassB的在 ClassB.h ,我必须把前向参比,它伸出。 (注意,这是不同于C#和放大器; Java的编译器在其中自动解决循环依赖这鼓励不好的编码实践IMO)。同样,你能避免循环依赖,如果你的code在的.cpp 文件,但在真实世界中的项目,的.cpp 文件往往包括随机的头,直到你能不能找出谁依赖谁。

[2] It avoids circular dependencies, by making them absolutely clear. If ClassA in ClassA.h has a circular dependency on ClassB in ClassB.h, I have to put a forward reference & it sticks out. (Note that this is unlike C# & Java where the compiler automatically resolves circular dependencies. This encourages bad coding practices IMO). Again, you can avoid circular dependencies if your code was in .cpp files, but in a real-world project, .cpp files tend to include random headers until you can't figure out who depends on whom.

您的想法?

推荐答案

不是在我的项目:源文件(CPP)只包括头(HPP)他们的需要。所以,当我需要重新编译,因为一个微小的变化只有一个CPP,我有相同数量的未编译文件的十倍。

Reason [1] Faster compile times

Not in my projects: source files (CPP) only include the headers (HPP) they need. So when I need to recompile only one CPP because of a tiny change, I have ten times the same number of files that are not recompiled.

也许你应该打破你的项目更合乎逻辑的源/头:A类的实现的修改应该不需要类B,C,D,E等。

Perhaps you should break down your project in more logical sources/headers: A modification in class A's implementation should NOT need the recompilation of implementations of class B, C, D, E, etc..

在code循环的依赖关系?

Circular dependencies in code?

很抱歉,但我还没有有这样那样的问题是一个现实的问题:假设A依赖于B,而B依赖于答:

Sorry, but I have yet to have this kind of problem being a real problem: Let's say A depends on B, and B depends on A:

struct A
{
   B * b ;
   void doSomethingWithB() ;
} ;

struct B
{
   A * a ;
   void doSomethingWithA() ;
} ;

void A::doSomethingWithB() { /* etc. */ }
void B::doSomethingWithA() { /* etc. */ }

要解决这个问题的一个好办法。将这个源分解成至少一个源/每类的头(类似于Java的方式的一种方式,但有一个来源,每类一标头):

A good way to resolve the problem would be to break down this source into at least one source/header per class (in a way similar to the Java way, but with one source and one header per class):

// A.hpp

struct B ;

struct A
{
   B * b ;
   void doSomethingWithB() ;
} ;

// B.hpp

struct A ;

struct B
{
   A * a ;
   void doSomethingWithA() ;
} ;

// A.cpp
#include "A.hpp"
#include "B.hpp"

void A::doSomethingWithB() { /* etc. */ }

// B.cpp
#include "B.hpp"
#include "A.hpp"

void B::doSomethingWithA() { /* etc. */ }

因此​​,没有依赖性问题,依然快速编译时间。

Thus, no dependency problem, and still fast compile times.

我错过了什么?

在真实世界中的项目,cpp文件往往包括随机的头,直到你能不能找出谁依赖谁。

in a real-world project, cpp files tend to include random headers until you can't figure out who depends on whom

当然。不过,如果你有时间来重组这些文件来构建一CPP的解决方案,那么你有时间来清理这些头。我的头的规则:

Of course. But then if you have time to reorganize those files to build your "one CPP" solution, then you have time to clean those headers. My rules for headers are:


  • 打破头,使他们尽可能模块化

  • 从不包含头文件不需要

  • 如果你需要一个符号,前瞻性声明它

  • 仅在上面的失败,包括头

不管怎样,所有的标题必须是自给自足,这意味着:

Anyway, all headers must be self-sufficient, which means:


  • 一个标题包含所有需要的头文件(且仅需要头 - 见上文)

  • 一个空cpp文件包括一个头必须编译,而无需包含任何其他

这将删除订货问题和循环依赖。

This will remove ordering problems and circular dependencies.

应该编译时一个真正的问题,我会考虑两种:

Should compile time be really an issue, I would consider either:


  • 使用precompiled头(这是STL和BOOST非常有用)

  • 通过平普尔成语减少耦合,如<一个解释href=\"http://en.wikipedia.org/wiki/Opaque_pointer\">http://en.wikipedia.org/wiki/Opaque_pointer

  • 使用网络共享编译

你是做什么的头是不是把一切。

What you are doing is not putting everything in headers.

您基本上包括所有的文件合并成一个,只有最后一个来源。

You are basically including all your files into one and only one final source.

也许你正在赢得在全项目汇编条款。

Perhaps you are winning in terms of full-project compilation.

不过,编译一个小的变化时,你总是会输。

But when compiling for one small change, you'll always lose.

编码时,我知道我经常编译的微小变化(如果仅仅是为了让编译器验证我的code),然后最后一次,做一个完整的工程变更。

When coding, I know I compile often small changes (if only to have the compiler validate my code), and then one final time, do a full project change.

如果我的项目是有组织的方式,我会失去很多时间。

这篇关于赞成&安培;把所有code头文件在C ++中的缺点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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