* Kristian Nielsen <knielsen@stripped> [09/06/29 19:14]:
> > Just to clarify, when we mention forward declarations here, we
> > are talking about forward declarations of *classes* only, not
> > variables, so the "extern int foo" is not an example of a
> > forward declaration we are considering. For functions and
> > variables, it is a requirement to use an include file.
>
> Mats, could you describe what the benefits are of using forward
> declarations of structs/classes in place of #including the
> definition?
>
> Clearly, self-references in structs (whether direct or
> recursive) require forward declarations, but otherwise?
>
> It seems to me to be a simpler if code referencing struct/class
> FOO would always do so by including file_defining_FOO.h? What am
> I missing? I don't remember seeing this style much in C/C++
> code...
A good design is a design that well reflects requirements in the
structure of the computer program. In human language,
when creating a component, a good engineer avoids
unnecessary dependencies as much as possible.
Inclusion with #include is a stronger dependency than inclusion
with a forward declaration. Especially in C++, where
your component becomes aware of such intricate parts of the
included component as private members of classes.
This is why forward declarations are preferred.
Most C++ engineers I met found this fact more or less
self-evident.
However, if you are really interested in the subject
I warmly recommend "Large-Scale C++ Software Design"
by John Lakos. This books represents the viewpoint
of no-unnecessary-dependencies camp very well.
--