简体   繁体   English

自定义头文件和包含库[C ++]

[英]Custom header files and include libraries [C++]

If I create a header like this: 如果我创建这样的标题:

#ifndef _MY_HEADER_H
#define _MY_HEADER_H

#include <iostream>
void foo();

#endif

With it's correspondent .cpp file, do I need to include iostream in the main.cpp file? 有了它对应的.cpp文件,我是否需要在io.main文件中包括iostream

不必在main.cpp版本中再次包含它,就像在main.cpp版本中那样,如果您包含相同的.h版本,则包含的内容将自动在编译中可用,为什么还要编写额外的冗余行?

To answer you question: No you don't need to include it (again). 要回答您的问题:不,您不需要(再次)包含它。

But it is good practice, to include in the header only the stuff that is required for the header to work. 但最好的做法是,仅在标头中包含标头正常工作所需的内容。 So if your foo() method requires iostream , you should include it. 因此,如果您的foo()方法需要iostream ,则应包括它。 If you create a class that uses only pointers or references to other classes you should prefer forward declarations over inclusion of the full-fledged header of the respective classes. 如果创建仅使用指向其他类的指针或引用的类,则应优先使用前向声明,而不要包含各个类的完整标题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM