对于项目使用多于1个代码文件的优点是什么? (C ++) [英] What are the advantages of using more then 1 code file for a project? (C++)

查看:267
本文介绍了对于项目使用多于1个代码文件的优点是什么? (C ++)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用多个源( .cpp )和标题( .h )文件单项目?

What are the advantages of using multiple source (.cpp) and header (.h) files in a single project?

这只是一个优惠的事情还是有真正的好处?

Is it just a preferential thing or are there true benefits?

推荐答案

p>它可以帮助你拆分代码和按主题排序。否则,你会得到一个文件,其中1000行的行...这是很难管理...

It helps you split your code and sort it by theme. Otherwise you get one file with 1000s of lines… which is hard to manage…

通常,人们有.h​​和.c一个或有时几个类。
此外,它加速了编译,因为只有修改的文件和一些相关文件需要重新编译。

Usually, people have .h and .c for one or sometimes a few classes. Also, it speeds up compilation, since only the modified files, and some related files need to be recompiled.

在C和C ++中组织代码文件

拆分任何规模合理的项目,都会带来一些优势,
其中最重要的是以下内容:

Splitting any reasonably-sized project up buys you some advantages,    the most significant of which are the following:


  • 加速编译 - 大多数编译器一次只能处理一个文件。因此,如果所有
    10000行代码都在一个文件中,并且更改了一行,那么
    必须重新编译10000行代码。另一方面,如果您的
    10000行代码均匀分布在10个文件中,则更改
    一行只需要重新编译1000行代码。
    9000行在其他9个文件将不需要重新编译。 (链接
    的时间不受影响。)

  • Speed up compilation - most compilers work on a file at a time. So if all your 10000 lines of code is in one file, and you change one line, then you have to recompile 10000 lines of code. On the other hand, if your 10000 lines of code are spread evenly across 10 files, then changing one line will only require 1000 lines of code to be recompiled. The 9000 lines in the other 9 files will not need recompiling. (Linking time is unaffected.)

增加组织 - 沿逻辑线分割代码
让你(和项目上的任何其他程序员)更容易找到
找到函数,变量,struct /类声明等等。即使
能够直接跳转到在许多编辑器和开发环境(例如
Microsoft Visual C ++)中提供的
的给定标识符,也总是有需要
手动扫描代码以查找某些内容。正如分割
代码减少了需要重新编译的代码量,它也减少了你需要读取的代码量,以便找到
。想象一下,你需要找到一个修正你对声音
代码几个星期前。如果你有一个名为GAME.C,
的大文件,这可能是很多搜索。如果你有几个小的
文件称为GRAPHICS.C,MAINLOOP.C,SOUND.C和INPUT.C,你知道
在哪里,你的浏览时间减少3/4。

Increase organization - Splitting your code along logical lines will make it easier for you (and any other programmers on the project) to find functions, variables, struct/class declarations, and so on. Even with the ability to jump directly to a given identifier that is provided in many editors and development environments (such as Microsoft Visual C++), there will always be times when you need to scan the code manually to look for something. Just as splitting the code up reduces the amount of code you need to recompile, it also reduces the amount of code you need to read in order to find something. Imagine that you need to find a fix you made to the sound code a few weeks ago. If you have one large file called GAME.C, that's potentially a lot of searching. If you have several small files called GRAPHICS.C, MAINLOOP.C, SOUND.C, and INPUT.C, you know where to look, cutting your browsing time by 3/4.

促进代码重用 - 如果您的代码被仔细分为
部分, b $ b你在另一个项目中使用该代码,节省了很多重写
。编写可重用代码比使用
逻辑文件组织有更多,但没有这样的组织,它是
很难知道代码的哪些部分协同工作和
不。因此,将子系统和类放在单个
文件或仔细划分的文件集中将有助于您以后如果
尝试在另一个项目中使用该代码。

Facilitate code reuse - If your code is carefully split up into sections that operate largely independently of each other, this lets you use that code in another project, saving you a lot of rewriting later. There is a lot more to writing reusable code than just using a logical file organization, but without such an organization it is very difficult to know which parts of the code work together and which do not. Therefore putting subsystems and classes in a single file or carefully delineated set of files will help you later if you try to use that code in another project.

在项目之间共享代码 - 此处的原则与
的重用问题相同。通过仔细分离代码到某些文件,你
使多个项目可以使用一些相同的代码
文件,而不复制它们。在项目之间共享代码文件
的好处是,您只需使用复制和粘贴就可以对任何
错误进行修复,一个项目的文件或其他文件将影响其他项目

Share code between projects - The principle here is the same as with the reuse issue. By carefully separating code into certain files, you make it possible for multiple projects to use some of the same code files without duplicating them. The benefit of sharing a code file between projects rather than just using copy-and-paste is that any bug fixes you make to that file or files from one project will affect the other project, so both projects can be sure of using the most up-to-date version.

在程序员中分配编码职责

Split coding responsibilities among programmers - For really large projects, this is perhaps the main reason for separating code into multiple files. It isn't practical for more than one person to be making changes to a single file at any given time. Therefore you would need to use multiple files so that each programmer can be working on a separate part of the code without affecting the file that the other programmers are editing. Of course, there still have to be checks that 2 programmers don't try altering the same file; configuration management systems and version control systems such as CVS or MS SourceSafe help you here. All of the above can be considered to be aspects of modularity, a key element of both structured and object-oriented design.

然后,他们会继续如何做到,潜在的陷阱,修复问题等。

Then, they go on about How to do it, Potential Pitfalls, Fixing problems, etc.

您应该检查一下。

这篇关于对于项目使用多于1个代码文件的优点是什么? (C ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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