cpp ファイルをインクルードせずにヘッダーを使用する必要があるのはなぜですか? 質問する

cpp ファイルをインクルードせずにヘッダーを使用する必要があるのはなぜですか? 質問する

初めての C++ プログラミング課題を終えて成績を受け取りました。しかし、採点によると、 の点数が減点されましたincluding cpp files instead of compiling and linking them。それが何を意味するのかよくわかりません。

自分のコードを振り返ってみると、クラスのヘッダー ファイルを作成せず、すべてを cpp ファイルで実行しました (ヘッダー ファイルがなくても問題なく動作しているようです...)。採点者は、一部のファイルに '#include "mycppfile.cpp";' と記述したことを意図していたのだと思います。

cpp ファイルを ' した理由は#include、次のとおりです。 - ヘッダー ファイルに入るはずのものはすべて cpp ファイルにあったので、それをヘッダー ファイルのように見せかけました。 - 猿真似をして、他のヘッダー ファイルが#includeファイル内に ' されているのを見たので、自分の cpp ファイルでも同じことをしました。

それで、私は具体的に何を間違えたのでしょうか、そしてなぜそれが悪いのでしょうか?

ベストアンサー1

私の知る限り、C++ 標準ではヘッダー ファイルとソース ファイルの間に違いはありません。言語に関する限り、合法的なコードを含むテキスト ファイルは他のファイルと同じです。ただし、違法ではないものの、ソース ファイルをプログラムに含めると、そもそもソース ファイルを分離することで得られる利点がほとんどなくなります。

#include本質的には、プリプロセッサ指定したファイル全体をアクティブファイルにコピーしてから、コンパイラそのため、プロジェクト内のすべてのソース ファイルをまとめて含めると、まったく分離せずに 1 つの巨大なソース ファイルを作成する場合と、実行したことの間に基本的に違いはありません。

「ああ、大したことじゃないよ。動けば大丈夫だよ」 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.

Unless you change something.

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.

"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.

"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.

おすすめ記事