Statements like
class SparseMatrix;
called forward ads. It tells the compiler that “somewhere” is a class of this name. This makes the compiler happy and works fine, as long as the declaration file in the forward direction uses either a pointer or a class reference with a direct link. This is because, from the point of view of the compiler, pointers or references are only 4 bytes, regardless of the contents of the class.
In the OP-code, both SparseMatrix and RegMatrix are used only as (const) links, therefore, a forward declaration is sufficient to declare this announcement.
However, if the direct declaration file does something that requires the compiler to know its size, for example.
void foo( SparseMatrix );
then the compiler will complain :-)
In the specific situation specified by the OP, I prefer to abandon the mutual #include altogether and design an interface based only on forward declarations. An implementation (e.g. .cpp files) may include both header files, but this is not a problem.
source share