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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2887465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 11:23:19  来源:igfitidea点击:

C++ header and implementation files: what to include?

c++headerimplementation

提问by neuromancer

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

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

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

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

回答by Merlyn Morgan-Graham

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.

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

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:

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

SomeClass.cpp -

SomeClass.cpp -

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

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

SomeClass.h -

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.

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

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.

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

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.

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

回答by Opera

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.

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

A good rule of thumb is to NEVER include a .cpp file, because the #includedirective 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.

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

回答by Raghad

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

通常最好写在头文件.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.

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