Should '#include' and 'using' statements be repeated in both header and implementation files (C++)?
- by Dr. Monkey
I'm fairly new to C++, but my understanding is that a #include statement will essentially just dump the contents of the #included file into the location of that statement. This means that if I have a number of '#include' and 'using' statements in my header file, my implementation file can just #include the header file, and the compiler won't mind if I don't repeat the other statements.
What about people though?
My main concern is that if I don't repeat the '#include', 'using', and also 'typedef' (now that I think of it) statements, it takes that information away from the file in which it's used, which could lead to confusion.
I am just working on small projects at the moment where it won't really cause any issues, but I can imagine that in larger projects with more people working on them it could become a significant issue.
An example follows:
//Unit.h
#include <string>
#include <ostream>
#include "StringSet.h"
using std::string;
using std::ostream;
class Unit {
public:
//public members
private:
//private members
//unrelated side-question: should private members
//even be included in the header file?
} ;
//Unit.cpp
#include "Unit.h"
//The following are all redundant from a compiler perspective:
#include <string>
#include <ostream>
#include "StringSet.h"
using std::string;
using std::ostream;
//implementation goes here