I've read lots of articles about forward declaration, but I still have a question.
Let's suppose we have:
// File a.hpp (in this question I avoid writing guards in header files, for the sake of simplicity)
class A
{
// Class B is used only by pointer, the compiler doesn't need to know the structure
// of the class, so a forward declaration is enough
public:
A(void);
void Method1(B *pB);
void Method2(B *pB);
};
// File a.cpp
#include "a.hpp"
A::A(void) { }
// Some methods of class B are used, so the compiler needs to know the declaration of the class, it cannot be forward declared
void A::Method1(B *pB)
{
// Something...
pB->SomeMethod();
// Something ...
}
void A::Method2(B *pB)
{
int var = pB->GetSomeMember();
// Something ...
}
Ok, now let's suppose to have one header file for class B declaration and another one for its forward declaration:
// File b.hpp
// Class declaration
class B
{
/* ... */
};
// File b_fwd.hpp
// Forward declaration
class B;
What I have in mind, basing on the previous considerations, is to include "b_fwd.hpp" in a.hpp (which only needs the forward declaration of class B), and "b.hpp" in the a.cpp file (which need the declaration),
as follows:
// File a.hpp
#include "b_fwd.hpp" // Forward declaration of class B
class A
{
public:
A(void);
void Method1(B *pB);
void Method2(B *pB);
};
// File a.cpp
#include "a.hpp"
#include "b.hpp" // Declaration of class B
A::A(void) { }
void A::Method1(B *pB) { /* like before ... */ }
void A::Method2(B *pB) { /* like before ... */ }
I know this works, but since in class A I included (let's say) "twice" the class B, the first time forward declared and the second time "normally", this sounds a little strange to me. I want to know if this is a good practise, and if it can be done or not in projects.