问题描述
无论出于何种原因,我们公司都有一个编码指南,规定:
For whatever reason, our company has a coding guideline that states:
每个类都有自己的头文件和实现文件。 code>
Each class shall have it's own header and implementation file.
所以如果我们写了一个 MyString
类,我们需要一个关联的 MyStringh .h 和 MyString.cxx 。
So if we wrote a class called MyString
we would need an associated MyStringh.h and MyString.cxx.
有没有人看到任何编译性能的后果结果? 5000个文件中的5000个类在2500个文件中的编译速度和5000个类一样快吗?
Does anyone else do this? Has anyone seen any compiling performance repercussions as a result? Does 5000 classes in 10000 files compile just as quickly as 5000 classes in 2500 files? If not, is the difference noticeable?
[我们编写C ++并使用GCC 3.4.4作为我们的日常编译器]
[We code C++ and use GCC 3.4.4 as our everyday compiler]
推荐答案
这里的术语是翻译单元,你真的想(如果可能)每个翻译单元有一个类,即每个.cpp文件,具有相同名称的相应.h文件。
The term here is translation unit and you really want to (if possible) have one class per translation unit ie, one class implementation per .cpp file, with a corresponding .h file of the same name.
通常更有效(从编译/链接)的角度来这样做,做增量链接等等。这个想法是,翻译单元是孤立的,这样,当一个翻译单元改变时,你不必重建很多东西,就像你开始将许多抽象化成单个翻译单元一样。
It's usually more efficient (from a compile/link) standpoint to do things this way, especially if you're doing things like incremental link and so forth. The idea being, translation units are isolated such that, when one translation unit changes, you don't have to rebuild a lot of stuff, as you would have to if you started lumping many abstractions into a single translation unit.
此外,您会发现许多错误/诊断都通过文件名报告(Myclass.cpp,第22行中的错误),它有助于如果一一对应的文件和类。 (或者我想你可以称之为2:1对应)。
Also you'll find many errors/diagnostics are reported via file name ("Error in Myclass.cpp, line 22") and it helps if there's a one-to-one correspondence between files and classes. (Or I suppose you could call it a 2 to 1 correspondence).
这篇关于头文件中的多个类与每个类的单个头文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!