> Class A calls a function from class B. Class B calls a function from class
> A. This means that A needs to see B in order to compile, and B
> needs to see A.
More precisely: The implementation of A needs to see the interface of B, and
vica versa. Being explicit here solves the standard problem later:
> If all of one's #includes are in the .h files, there is no way (as far as I
> can see) to get this to compile. It can be compiled using #includes
> in the .cpp files, but this feels like a hack to me. Is that how it's
> normally done? Or is the standard advice to redesign until the situation
> is not circular?
It shoudln't be a problem to get this to compile using include guards; a.h
includes b.h which includes a.h, at which moment the include guards break
the circle and a.h and b.h are both included once. However, this is not good
design. .h should represent an interface. If and only if the interface
of A requires the interface of B should a.h include b.h. But if the classes
are that tightly coupled they should share a header.
If it is only the implementation of A which needs the interface of B the
the implementation (.cpp) should include the interface (.h). This certainly
isn't a hack.
BTW, The interface of B* isn't the interface of B. Most of the interface of B*
can be obtained by writing class B;. This allows creation, assignment and
destruction of pointers to B, only not dereferencing. But an 'class A'
interface rarely needs that.
Quote:> A concrete example:
> My program uses polymorphism to handle different types of data. Several
> objects need to be polymorphic on data type. They are created by a
> factory which knows the data type of the current part of the data set.
> One of the created objects would like to know about the factory so that
> it can request sub-objects as it needs them. This introduces the circle.
In that case, the implementation of the data set object needs an factory
interface, and the factory implementation only needs the data set interface.
So all is fine and implementable, no dependancy circle is introduced
(as long as the set of sub-objects is bounded.)
[ about comp.lang.c++.moderated. First time posters: do this! ]