为什么我不应该包含 cpp 文件而是使用标题? [英] Why should I not include cpp files and instead use a header?

查看:27
本文介绍了为什么我不应该包含 cpp 文件而是使用标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我完成了我的第一个 C++ 编程作业并获得了成绩.但是根据评分,我失去了包含cpp文件而不是编译和链接它们的分数.我不太清楚这意味着什么.

So I finished my first C++ programming assignment and received my grade. But according to the grading, I lost marks for including cpp files instead of compiling and linking them. I'm not too clear on what that means.

回顾我的代码,我选择不为我的类创建头文件,而是在 cpp 文件中执行所有操作(它似乎在没有头文件的情况下也能正常工作...).我猜评分者的意思是我写了'#include "mycppfile.cpp";'在我的一些文件中.

Taking a look back at my code, I chose not to create header files for my classes, but did everything in the cpp files (it seemed to work fine without header files...). I'm guessing that the grader meant that I wrote '#include "mycppfile.cpp";' in some of my files.

我使用 #include 处理 cpp 文件的理由是:- 应该进入头文件的所有内容都在我的 cpp 文件中,所以我假装它像一个头文件- 在monkey-see-monkey do fashion中,我看到其他头文件在文件中被#include,所以我对我的cpp文件做了同样的事情.

My reasoning for #include'ing the cpp files was: - Everything that was supposed to go into the header file was in my cpp file, so I pretended it was like a header file - In monkey-see-monkey do fashion, I saw that other header files were #include'd in the files, so I did the same for my cpp file.

那么我到底做错了什么,为什么不好?

So what exactly did I do wrong, and why is it bad?

推荐答案

据我所知,C++ 标准不知道头文件和源文件之间有什么区别.就语言而言,任何带有合法代码的文本文件都与其他文本文件相同.然而,虽然不违法,但将源文件包含在您的程序中几乎会消除您从一开始就分离源文件所获得的任何优势.

To the best of my knowledge, the C++ standard knows no difference between header files and source files. As far as the language is concerned, any text file with legal code is the same as any other. However, although not illegal, including source files into your program will pretty much eliminate any advantages you would've got from separating your source files in the first place.

本质上,#include 所做的是告诉预处理器获取您指定的整个文件,并在编译器之前将其复制到您的活动文件中 得到它的手.因此,当您将项目中的所有源文件都包含在一起时,您所做的与只是制作一个完全没有任何分离的巨大源文件之间从根本上没有区别.

Essentially, what #include does is tell the preprocessor to take the entire file you've specified, and copy it into your active file before the compiler gets its hands on it. So when you include all the source files in your project together, there is fundamentally no difference between what you've done, and just making one huge source file without any separation at all.

哦,那没什么.如果它可以运行,那就没问题了,"我听到你哭了.从某种意义上说,你是对的.但是现在您正在处理一个很小的小程序,以及一个不错且相对不受阻碍的 CPU 来为您编译它.你不会总是那么幸运.

"Oh, that's no big deal. If it runs, it's fine," I hear you cry. And in a sense, you'd be correct. But right now you're dealing with a tiny tiny little program, and a nice and relatively unencumbered CPU to compile it for you. You won't always be so lucky.

如果您深入研究过严肃的计算机编程领域,您会看到行数可能达到数百万而不是数十的项目.这是很多行.如果您尝试在现代台式计算机上编译其中之一,则可能需要几小时而不是几秒钟.

If you ever delve into the realms of serious computer programming, you'll be seeing projects with line counts that can reach millions, rather than dozens. That's a lot of lines. And if you try to compile one of these on a modern desktop computer, it can take a matter of hours instead of seconds.

哦,不!这听起来很可怕!但是我能阻止这种可怕的命运吗?!" 不幸的是,对此您无能为力.如果编译需要几个小时,那么编译也需要几个小时.但这只是第一次真正重要——一旦你编译了一次,就没有理由再次编译它.

"Oh no! That sounds horrible! However can I prevent this dire fate?!" Unfortunately, there's not much you can do about that. If it takes hours to compile, it takes hours to compile. But that only really matters the first time -- once you've compiled it once, there's no reason to compile it again.

除非你有所改变.

现在,如果您将两百万行代码合并成一个巨大的庞然大物,并且需要进行简单的错误修复,例如 x = y + 1,那就意味着您有再次编译所有两百万行以测试这一点.如果你发现你打算做一个 x = y - 1 代替,那么还有两百万行编译等着你.浪费了很多时间,本来可以花在做其他事情上.

Now, if you had two million lines of code merged together into one giant behemoth, and need to do a simple bug fix such as, say, x = y + 1, that means you have to compile all two million lines again in order to test this. And if you find out that you meant to do a x = y - 1 instead, then again, two million lines of compile are waiting for you. That's many hours of time wasted that could be better spent doing anything else.

但我讨厌效率低下!如果有某种方法可以分别编译我的代码库的不同部分,然后以某种方式链接它们!" 理论上是个好主意.但是,如果您的程序需要知道不同文件中发生的事情怎么办?除非您想运行一堆微小的 .exe 文件,否则不可能完全分离您的代码库.

"But I hate being unproductive! If only there was some way to compile distinct parts of my codebase individually, and somehow link them together afterwards!" An excellent idea, in theory. But what if your program needs to know what's going on in a different file? It's impossible to completely separate your codebase unless you want to run a bunch of tiny tiny .exe files instead.

但它肯定是可能的!否则编程听起来简直就是折磨!如果我找到某种方法将接口与实现分开会怎么样?从这些不同的代码中获取足够的信息就好了段以将它们标识给程序的其余部分,并将它们放在某种文件中?这样,我可以使用 #include 预处理器指令只引入编译所需的信息!"

"But surely it must be possible! Programming sounds like pure torture otherwise! What if I found some way to separate interface from implementation? Say by taking just enough information from these distinct code segments to identify them to the rest of the program, and putting them in some sort of header file instead? And that way, I can use the #include preprocessor directive to bring in only the information necessary to compile!"

嗯.你可能正在那里做一些事情.让我知道这对您有何帮助.

Hmm. You might be on to something there. Let me know how that works out for you.

这篇关于为什么我不应该包含 cpp 文件而是使用标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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