You need to understand that circular dependency implies that you can only use the corresponding circular dependent classes together: if you have a circular dependence between, say, A and B , you cannot use A or B independently in any program. To stick to your problem: of course, you don't need two other friends! All you need is some way to refer to some and interact with them in a way that may be limited by their actual abilities.
However, it is often possible that class objects use each other for no reason for circular dependency. To this end, it is important to determine what actually causes the dependency between the two classes / components (they are not quite equivalent, but providing a detailed definition will be somewhat long). A depends on B under these conditions:
- When
A contains a member of type B - When
A comes from type B - When
A uses a value of type B as part of the function signature. - When
A uses B in its implementation. - I probably forgot something (I remember there were more reasons why the classes would be connected).
If you have a circular dependency between two classes, there may be ways to break this dependency. Often, a dependency can be broken down by splitting one of the two classes into a base class and a derived class with a base class independent of the other class. There are a number of other approaches to breaking dependency cycles. John Lakos "Large Scale C ++" (1996) is essentially all about interdependence cycles and the motivation for why cyclic dependencies are bad (I think he would not agree with this simplifying characteristic).
... and, yes, cyclic dependencies are bad:
- They force programs to include unnecessary functions, because things are dragged and dropped, which are not needed.
- They make software testing difficult.
- They are much more difficult to talk about software.
- They greatly complicate the replacement of parts of the system.
- ... and perhaps a number of other reasons.
The above is formulated in terms of C ++. Some of the reasons for circular dependencies may be missing [directly] in C, but the same concepts apply to C.
source share