- 是否有一些首選的方法來組織包括指令?
- 將
.cpp
文件而不是.h
文件包含在需要的文件中是否更好?翻譯單位是否受到某種程度的影響?
- 如果我在
.h
文件和.cpp
文件中都需要它,我應該將它包含在.h
文件中嗎?這很重要嗎?
- 將已定義的文件保存在預編譯頭文件(
stdafx.h
)中,例如std和第三方庫,是否是一種很好的做法?我自己的文件如何,我應該在創建它們時將它們包含在stdafx.h
文件中?
// myClass.h
#include <string>
// ^-------- should I include it here? --------
class myClass{
myClass();
~myClass();
int calculation()
};
// myClass.cpp
#include "myClass.h"
#include <string>
// ^-------- or maybe here? --------
[..]
int myClass::calculation(){
std::string someString = "Hello World";
return someString.length();
}
// stdafx.h
#include <string.h>
// ^--------- or perhaps here, and then include stdafx.h everywhere? -------
杜佩。這已被問了很多次,他經常只是沒有鏈接正確的一個:http://stackoverflow.com/questions/2762568/cc-include-file-order-best-practices – AJG85
@ AJG85該問題問什麼*命令*包含應該在英寸我不要求命令 - 我問包括應該在哪裏,在哪個文件中。 – Default