Should I #include everything I need in every header/cpp file? I am working on a 2d game engine atm (for practice mostly) and in reviewing my code I realise that I repeat string and vector in almost every file. Is this an issue and how do I deal with it?
I’ve always had the opinion that every class or module you write should stand on it’s own two legs, so to speak. I really enjoy generic programming (I’m including my own script language in the engine, with my own drafted script engine) but I also realise it could cause a lot of overhead and confusion.
I would stick to including
<string>and<vector>only where necessary.As for making sure individual header files stand on their own, I like how the Google C++ Style Guide deals with include order. Basically, always list the corresponding
foo.hppinclude before all other includes infoo.cpp. That way, we know thatfoo.hppwon’t expect something to be included before it and fail if it isn’t there.