If you have a set of header files with include guards, is it really necessary to have forward declarations?
I've been doing this out of habit in my code, and I recommended a colleague add forward declarations to their code, but when they asked whether it was really necessary because of the include guards already present, I was at a bit of a loss. It seems like it's good practice, but wouldn't include guards solve all of the problems that forward declarations do?
Here's an example. Suppose I have a header file like so:
//This is the header file of class B
class MyClassA; //This is forward declaration
class B{
private:
MyClassA *myClass; //pointer to MyClassA
public:
B(){}
~B(){};
void print(MyClassA* myClass); //Passing by reference. Not calling any methods of MyClassA.
}
If I know that there is an include guard in MyClassA.h, would the above be better practice than the following? And, if so, why?
#include “MyClassA.h”
class B{
private:
MyClassA *myClass; //pointer to MyClassA
public:
B(){}
~B(){};
void print(MyClassA* myClass); //Passing by reference. Not calling any methods of MyClassA.
}
Would the second example also prevent a circular declaration/multiple declaration?