简体   繁体   English

如果我可以在.h文件中包含所有C ++代码,为什么要使用.cpp文件?

[英]Why to use .cpp files if I can have all of my C++ code in .h file?

Why to use .cpp files if I can have all of my C++ code in .h file? 如果我可以在.h文件中包含所有C ++代码,为什么要使用.cpp文件? I mean .cpp files are quite strange to use if all code can be wrote in .h file? 我的意思是.cpp文件是很奇怪的,如果所有代码都可以在.h文件中写入? Can any one clerefy? 任何人都可以清洁吗?

A few reasons: 原因如下:

(1) Incremental Build Times (1)增量构建时间

When projects grow larger, managing the build time is problematic especially for C++ projects. 当项目变大时,管理构建时间是有问题的,特别是对于C ++项目。 Building 1 or 5 minutes after a minor change makes a big difference. 在一次微小的改变后建立1或5分钟会产生很大的不同。 This is emphasized by most changes in large projects being small and require a lot of testing. 大型项目的大多数变化很小需要大量测试,这一点得到了强调。 Add to that any attempt of TDD and refactoring, and you are a dead slug with sicilian shoes. 再加上TDD和重构的任何尝试,你是一个用西西里鞋的死slu ..

Splitting into header and body, and moving it to libs improves incremental build times tremendously. 拆分成标题和正文,并将其移动到库可以极大地改善增量构建时间。

(2) Statics (2)静力学
For many things you need a single instance of a type, ie 对于很多事情,你需要一个类型的单个实例,即

// .cpp
static Foo foo;

There is no way (that I'm aware of) allowing this in a header-only project. 没有办法(我知道)允许在仅头文件项目中使用它。 Compiler specific solutions are limited, eg __declspec(selectany) in MSVC is limited to POD types. 编译器特定的解决方案是有限的,例如MSVC中的__declspec(selectany)仅限于POD类型。

[edit] C++17 now allows inline also for variable initialization, so this is not a blocking issue anymore. [编辑] C ++ 17现在也允许内联进行变量初始化,因此这不再是阻塞问题。

(3) Implementation hiding (3)实施隐藏
.cpp / .h separation is the only way to clearly separate a public interface from implementation details. .cpp / .h分离是将公共接口与实现细节明确分开的唯一方法。 You can throw class members into a private section, but that doesn't work for other entities. 您可以将类成员引入private部分,但这对其他实体不起作用。 (Even the header/body separation is leaky unless you add additional techniques such as PIMPL, so this argument is a bit weak IMO, but again, in a large project I'd dearly miss this efficient if imperfect method). (即使标题/正文分离是泄漏的,除非你添加其他技术,如PIMPL,所以这个论点有点弱IMO,但同样,在一个大型项目中,我非常怀念这种有效的,如果不完美的方法)。


Great question, anyway - you've recognized that there's something afoul with the C/C++ build model, which I consider an ancient relic of horrible implications. 很棒的问题,无论如何 - 你已经认识到与C / C ++构建模型存在某种冲突,我认为这是一个可怕含义的古老遗物。

You should try how far you can push a "header only" model (or, at least, a "almost only headers", to allow for statics). 您应该尝试在多大程度上推送“仅标题”模型(或者至少是“几乎只有标题”,以允许静态)。 You might get quite far - also it would be interesting to hear from people who have tried. 你可能会走得很远 - 听到那些尝试过的人也会很有意思。

It might be worth a try to use static libraries to separate and encapsulate implementations, and otherwise keep all your code in headers. 可能值得尝试使用静态库来分离和封装实现,否则将所有代码保存在头文件中。 I can see a few problems with that, but it's nto that our current modus operandi is trouble free. 我可以看到一些问题,但我们当前的运作方式是无故障的。

You could put all your code into .h files. 可以将所有代码放入.h文件中。 Contrary to popular belief, this won't duplicate the code across your .obj files. 与流行的看法相反,这不会复制.obj文件中的代码。 Modern compilers are much smarter than that. 现代编译器比这更聪明。

Compilation is somewhat an issue though. 编译虽然有些问题。 If you have 20 .h files all included into main.cpp, compiling main.cpp will take a while. 如果你有20个.h文件全部包含在main.cpp中,编译main.cpp将需要一段时间。 And it will be recompiled, including all 20 of your implementation .h files, every time one of your include files changes. 每当你的一个包含文件发生变化时,它将被重新编译,包括你的所有20个实现.h文件。

Then there's style. 那就是风格。 It just looks wrong to me. 这对我来说只是看错了。 But this is a matter of preference. 但这是一个偏好问题。

Then there are references. 然后有参考。 If ClassA uses ClassB, and ClassB uses ClassA, which one do you include first? 如果ClassA使用ClassB,而ClassB使用ClassA,那么您首先包含哪一个?

Header files (.h) are meant to be used to define the interface so that your classes and code can be used in other translation units. 头文件(.h)用于定义接口,以便您的类和代码可以在其他翻译单元中使用。 If you place implementation in the .h file then you end up with multiple copies of the same code, compiled into each translation unit that includes that .h file. 如果将实现放在.h文件中,那么最终会得到相同代码的多个副本,并编译到包含该.h文件的每个翻译单元中。 That defeats the point of splitting your code into small pieces that can be studied and developed in isolation. 这使得将代码分成可以单独研究和开发的小块的观点失败了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C ++:我想在所有CPP文件之间使用这个配置类,如何初始化它? - C++: I have this config class that I want to use between all my CPP files, how do I initialize it? CPP | .h文件(C ++) - CPP | .h files (C++) 我如何使用一堆.h和.cpp文件创建可以在另一个c ++项目中使用的静态库 - How do i use a bunch of .h and .cpp files to create a static library that can be used in another c++ Project C++如何在使用相同的.h时使用相同的.h并选择不同的.cpp? - C++ how can i use the same .h and choose different .cpp when using the same .h? 如何布局我的 C++ 程序? (我应该把 .h 和 .cpp 文件放在哪里?) - How do I layout my C++ program? (where should I put the .h and .cpp files?) 在哪里下载 C++ STL 源代码 .h 和 .cpp 文件? - Where to download C++ STLsource code both .h and .cpp files? c ++何时包含cpp,即使我们有.h文件 - c++ when to include cpp even if we have .h file C ++最佳实践-什么时候应该将项目分为.h和.cpp文件? - Best practice in C++ - when should I divide my project into .h and .cpp files? 我如何在将我的类分成 ah 和 .cpp 文件时使用聚合? - how can i use aggregation while separating my classes into a .h and .cpp files? 生成C ++文件:.h和.cpp - Build C++ files: .h and .cpp
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM