Unfortunately I can't comment the answer from geza.
He is not just saying "put forward declarations into a separate header". He says that you have to spilt class definition headers and inline function definitions into different header files to allow "defered dependencies".
But his illustration is not really good. Because both classes (A and B) only need an incomplete type of each other (pointer fields / parameters).
To understand it better imagine that class A has a field of type B not B*. In addition class A and B want to define an inline function with parameters of the other type:
This simple code would not work:
// A.h
#pragme once
#include "B.h"
class A{
B b;
inline void Do(B b);
}
inline void A::Do(B b){
//do something with B
}
// B.h
#pragme once
class A;
class B{
A* b;
inline void Do(A a);
}
#include "A.h"
inline void B::Do(A a){
//do something with A
}
//main.cpp
#include "A.h"
#include "B.h"
It would result in the following code:
//main.cpp
//#include "A.h"
class A;
class B{
A* b;
inline void Do(A a);
}
inline void B::Do(A a){
//do something with A
}
class A{
B b;
inline void Do(B b);
}
inline void A::Do(B b){
//do something with B
}
//#include "B.h"
This code does not compile because B::Do needs a complete type of A which is defined later.
To make sure that it compiles the source code should look like this:
//main.cpp
class A;
class B{
A* b;
inline void Do(A a);
}
class A{
B b;
inline void Do(B b);
}
inline void B::Do(A a){
//do something with A
}
inline void A::Do(B b){
//do something with B
}
This is exactly possible with these two header files for each class wich needs to define inline functions. The only issue is that the circular classes can't just include the "public header".
To solve this issue I would like to suggest a preprocessor extension: #pragma process_pending_includes
This directive should defer the processing of the current file and complete all pending includes.