- Is there some preferred way to organize ones include directives?
- Is it better to include the files you need in the
.cppfile instead of the.hfile? Are the translation units affected somehow? - How about if I need it in both the
.hfile and.cppfile, should I just include it in the.hfile? Will it matter? - Is it a good practice to keep the already defined files in a precompiled header (
stdafx.h), for instance std and third party libraries? How about my own files, should I include them in astdafx.hfile along the way as I create them?
// 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? -------
cpp-file only. As @Pedro d’Aquino commented, you can reduce the number of includes in a header by using forward declarations whenever possible (basically whenever you only use references or pointers to a given type).