简体   繁体   English

在Windows上编译C ++程序

[英]compiling c++ program on windows

I am compiling C++ on VS 2005. When and why use #include and when and why use pre-decleration as class XXXX ? 我正在VS 2005上编译C ++。什么时候以及为什么使用#include以及什么时候以及为什么使用预声明作为class XXXX What is the benefit of using each option and which one is preffered? 使用每个选项的好处是什么?优先选择哪个?

I would also glad for a good tutorial on compiling. 我也很高兴有一个很好的编译教程。

Always prefer forward declaration whenever possible. 始终尽可能选择前向声明。 Changes to the referred class file will not trigger recompilation of cpp files including the class using the pre-declared one. 对引用的类文件的更改不会触发使用预声明的类重新编译包括该类的cpp文件。 This reduces a bit the dependencies. 这样可以减少依赖性。

On each place where you are effectively using the class XXXX, you will have to include that header. 在每个有效使用类XXXX的地方,都必须包含该标头。 If you derive from class XXXX, you will also have to include the header. 如果您从类XXXX派生,则还必须包括标头。

A header file is used to contain the declaration of entities that are defined in separate compilation units. 头文件用于包含在单独的编译单元中定义的实体的声明。 If you did not have a header file, you'd have to enter such declarations in every compilation unit (which is essentially what #include does for you, it inserts the contained text at that point in the file, however if you did not use a header, you'd have to do it multiple times and that is both error prone and difficult to maintain when the code changes. 如果没有头文件,则必须在每个编译单元中输入这样的声明(本质上是#include为您执行的操作,它将在该点插入所包含的文本,但是如果您不使用标头,则必须多次执行,而且标头更改时容易出错且难以维护。

You'd use a declaration directly in the .cpp file for example if the symbol being defined is only ever used within that compilation unit and therefore did not need global visibility. 例如,如果所定义的符号仅在该编译单元中使用过,因此不需要全局可见性,则可以直接在.cpp文件中使用声明。 In the case of data declarations, you also typically declare them static to give them scope limited to the compilation unit. 对于数据声明,通常还可以将它们声明为静态,以将它们的范围限制为编译单元。

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

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