C++头文件和实现文件:包含什么?

发布于 2024-09-02 17:00:55 字数 90 浏览 1 评论 0原文

有一个.h 文件和一个.cpp 文件,它们具有相同的名称但扩展名不同。

如果我想使用 .cpp 文件中的内容,是否包含 .h 文件或 .cpp 文件?

There is a .h file and a .cpp file with the same name but different extension.

If I want to use what's in the .cpp file, do I include the .h file or the .cpp file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

拍不死你 2024-09-09 17:00:55

简单的答案是您几乎总是希望包含 .h 文件并编译 .cpp 文件。 CPP 文件(通常)是真实代码,H 文件(通常)是前向声明。

更长的答案是,您可以包含其中任何一个,并且它可能对您有用,但两者都会给出略有不同的结果。

“包含”的作用基本上是将文件复制/粘贴到该行。扩展名是什么并不重要,它都会以相同的方式包含文件的内容。

但按照惯例,C++ 代码通常是这样编写的:

SomeClass.cpp -

#include "SomeClass.h"
#include <iostream>

void SomeClass::SomeFunction()
{
  std::cout << "Hello world\n";
}

SomeClass.h -

class SomeClass
{
  public:
    void SomeFunction();
};

如果包含其中任何一个,则可以使用其中的代码。但是,如果您有多个文件包含相同的 .cpp 文件,则可能会收到有关重新定义的错误。头文件(.h 文件)通常只包含前向声明,没有实现,因此将它们包含在多个位置不会给您带来有关重新定义的错误。

如果您在包含其他 .cpp 文件中的 .cpp 文件时设法成功编译而没有错误,那么您仍然可能会得到重复的代码。如果您在多个其他文件中包含相同的 .cpp 文件,就会发生这种情况。就像你把这个函数写了两次一样。这将使您的程序在磁盘上更大,编译时间更长,并且运行速度更慢。

主要的警告是,此实现/前向声明约定不适用于使用模板的代码。模板代码仍将作为 .h 文件提供给您,但它(通常)直接在 .h 文件中实现,并且不会附带 .cpp 文件。

The simple answer is that you almost always want to include .h files, and compile .cpp files. CPP files are (usually) the true code, and H files are (usually) forward-declarations.

The longer answer is that you may be able to include either, and it might work for you, but both will give slightly different results.

What "include" does is basically copy/paste the file in at that line. It doesn't matter what the extension is, it will include the contents of the file the same way.

But C++ code is, by convention, usually written this way:

SomeClass.cpp -

#include "SomeClass.h"
#include <iostream>

void SomeClass::SomeFunction()
{
  std::cout << "Hello world\n";
}

SomeClass.h -

class SomeClass
{
  public:
    void SomeFunction();
};

If you include either of those, you can use the code from it. However, if you have multiple files that include the same .cpp file, you may get errors about re-definition. Header files (.h files) usually contain only forward declarations, and no implementations, so including them in multiple places won't give you errors about re-definition.

If you somehow manage to compile without errors when including .cpp files from other .cpp files, you can still end up with duplicate code. This happens if you include the same .cpp files in multiple other files. It's like you wrote the function twice. This will make your program bigger on disk, take longer to compile, and run a bit slower.

The main caveat is that this implementation/forward declaration convention doesn't hold true for code that uses templates. Template code will still be handed to you as .h files, but it (usually) is implemented directly in the .h file, and won't have accompanying .cpp files.

药祭#氼 2024-09-09 17:00:55

通常最好写在头文件.h中,

#ifndef H_someClass
#define H_someClass

class SomeClass {
public:
    void SomeFunction();
};

#endif

这样当你需要在其他文件中包含.cpp文件时,就不会出现重新定义的错误。

Usually it's better to write in the header file .h

#ifndef H_someClass
#define H_someClass

class SomeClass {
public:
    void SomeFunction();
};

#endif

so that you won't get errors about re-definition when you need to include the .cpp file in other files.

甜嗑 2024-09-09 17:00:55

.h 文件通常包含类声明,.cpp 文件通常包含类定义(实现)。您应该将 .h 包含在 .cpp 文件中。

一个好的经验法则是永远不要包含 .cpp 文件,因为 #include 指令只是将包含文件的内容复制到包含文件中。您可能会以一些多重包含/定义结束,但您绝对不想这样做。

The .h file usually contains the class declaration and the .cpp file the class definition (implementation). You should include the .h in the .cpp file.

A good rule of thumb is to NEVER include a .cpp file, because the #include directive just copies the content of the included file into the including file. You may end with some multiple inclusion / definition and you definitely don't want to do that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文