简体   繁体   English

在C ++中拆分接口和实现的好处

[英]Benefits of splitting interface and implementation in C++

I'm using C++ and I'm considering putting my function implementation into .h. 我正在使用C ++,正在考虑将函数实现放入.h。 I know that .h file is for definitions and .cpp is for implementations but how splitting all files into headers and sources will benefit me. 我知道.h文件用于定义,.cpp用于实现,但是将所有文件拆分为标头和源代码将使我受益。 Well if my aim would be to create static or dynamic library than of course that would make a difference but I am creating this code for myself and not planning to make a library out of it. 好吧,如果我的目标是创建静态或动态库,那当然会有所作为,但是我是为自己创建此代码,而不是打算从中创建一个库。 So is there any other benefit from splitting source from definition? 那么从定义中分离来源还有其他好处吗?

The obvious goal is to reduce coupling : as soon as you change a header file, anything that includes it must be recompiled. 显而易见的目标是减少耦合:更改头文件后,包含该文件的任何内容都必须重新编译。 This can rapidly have a strong impact on compilation times (even in a small project). 这可能对编译时间有很大的影响(即使在小型项目中)。

You can put almost all code into .h file, it will be header-only library. 您可以将几乎所有代码放入.h文件,这将是仅标头的库。 But if you want more faster partial recompilation, or if you want put some code to shared library - you should create .cpp files. 但是,如果您希望更快地进行部分重新编译,或者要将一些代码添加到共享库中,则应创建.cpp文件。

Depending on the size of your project it will save you compile time and make it possible to know all ressources etc. (unless you put everything into one single file). 根据项目的大小,这将节省您的编译时间,并使您可以了解所有资源等信息(除非将所有内容放入一个文件中)。

The better your header files are organized the less work your compiler has to do to apply changes. 头文件组织得越好,编译器执行更改所要做的工作就越少。 Also looking in a small header file to look up some forgotten parameter information is a lot easier than scrolling through a hole cpp file. 同样,在一个小的头文件中查找一些被遗忘的参数信息比在孔cpp文件中滚动要容易得多。

One other obvious improvement is in avoiding re-compiling the code for your function in each file that uses it, instead compiling it once and using it where needed. 另一个明显的改进是避免在使用该函数的每个文件中重新编译该函数的代码,而无需对其进行一次编译并在需要的地方使用它。

Another is that it follows convention (and the standard's one definition rule), so others will find it much easier to deal with and understand. 另一个是它遵循惯例(以及标准的一个定义规则),因此其他人会发现它更易于处理和理解。

It depends on the size of the project. 这取决于项目的大小。 Up to about 500 LOC, I tend to put everything in a single file, with the function definitions in the class. 最多约500个LOC,我倾向于将所有内容放在一个文件中,并在类中包含函数定义。 Except that up to about 500 LOC, I generally use a simpler language than C++; 除了最多500个LOC外,我通常使用比C ++更简单的语言。 something like AWK. 像AWK。

As soon as the code gets big enough to warrent several source files, it's definitely an advantage to put as little as possible in the header, and that means putting all of the function definitions in the source files. 一旦代码变得足够大以至于可以存放多个源文件,那么在头文件中尽可能少地放置绝对是一个优势,这意味着将所有函数定义都放置在源文件中。 And as soon as the classes become non-trivial, you probably don't want the function definitions in the class itself, for readability reasons. 而且,一旦类变得不平凡,出于可读性的原因,您可能就不希望类本身中的函数定义了。

-- James Kanze 詹姆斯·坎泽

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM