   #include and Forward Declarations
  I have a question about #include vs. using forward declartions.  Consider the simplified example below.  Is this the correct way to do it or should I just #include "Vector3f" inside the header file?A.h#ifndef A_H#define A_Hclass Vector3f;class A {public:    void doSomething(Vector3f& vec);};#endifA.cpp#include "A.h"#include "Vector3f.h"void A::doSomething(Vector3f& vec) {    vec.x = 0.0f;}
  Your way seems like a good idea. Either will work, although in practice I always try to keep my includes to the bare minimum necessary.If you include Vector3f in A.h, then keep in mind that every file that includes A.h will also include Vector3f, and every file that includes that file will include Vector3f, etc. Even if they have no need to know about Vector3f. Including files that are unnecessary seem like it could possibly lead to mistakes, and my gut tells me that you can compile faster without including a bunch of unnecessary files everywhere, though I don't have any proof to back that up.   ;  Reminded me of reading about this in Googles C++ style guide:http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Header_File_Dependencies   ;  Just #include Vector3f.h in A.h. Doing this on a large scale will prove to be a maintenance burden in the long run: if, for example, you decide to add new methods to A.h that require more than a pointer or reference to Vector3f, you'll have to remove the forward declaration and the #include directive in A.cpp. Now imagine having to do that for 10,000 translation units, evaluating them individually for each of the dependencies they have. Modifying 37 forward declarations in both header and implementation files? Fun.If you're concerned about compilation time, use a pre-compiled header or look into distributed compilation.Quote:Original post by karwostsIf you include Vector3f in A.h, then keep in mind that every file that includes A.h will also include Vector3f, and every file that includes that file will include Vector3f, etc. Even if they have no need to know about Vector3f. Including files that are unnecessary seem like it could possibly lead to mistakes, and my gut tells me that you can compile faster without including a bunch of unnecessary files everywhere, though I don't have any proof to back that up.The difference is unlikely to be significant, really. Inclusion guards ensure the contents are only substituted once in composing the translation unit source file, and compiler-specific pragmas like Visual Studio's once can ensure that the file as a whole is only processed once (inclusion guards will process the file every time the #include directive is encountered, but will skip the contents if the symbol is already defined). As mentioned above, if compilation time is a concern, look into PCHs and distcc-like solutions.   ;Quote:Original post by Oluseyiif, for example, you decide to add new methods to A.h that require more than a pointer or reference to Vector3f, you'll have to remove the forward declaration and the #include directive in A.cpp.That doesn't follow. Why would you need to remove either?;Quote:Original post by SiCraneQuote:Original post by Oluseyiif, for example, you decide to add new methods to A.h that require more than a pointer or reference to Vector3f, you'll have to remove the forward declaration and the #include directive in A.cpp.That doesn't follow. Why would you need to remove either?Whoops, you're right. You'd just need to add the new #include directive in the header. Brainfart there.Thanks, SiCrane.   ;Gamasutra analyst Matt Matthews examines U.S. video game retail sales for the month of November -- the first time since March that the industry saw a rise in year-on-year sales.  View the full article