home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!rational.com!thor!rmartin
- From: rmartin@thor.Rational.COM (Bob Martin)
- Subject: Re: Multiple Header Files Problem
- Message-ID: <rmartin.721359424@thor>
- Sender: news@rational.com
- Organization: Rational
- References: <720993360snx@trmphrst.demon.co.uk>
- Date: Tue, 10 Nov 1992 01:37:04 GMT
- Lines: 38
-
- A more interesting variation on this issue is the problem of "circular
- inclusion". Consider a source file: "a.h" which includes "b.h".
- However, "b.h" includes "a.h". This trivial example will fail to
- compile. Unless the compiler is very clever (I haven't seen one yet),
- it will sit and spin until it exhauses some resouce (usually memory),
- and then it will die a horrible death. If it prints an error at all,
- the error is nearly certain to have nothing to do with the circular
- containment.
-
- Now this may not seem to be a horrible problem. But consider that, no
- matter how many files are involved in the circularity, the malfunction
- of the compiler will be the result. This if "a.h" includes "b.h"
- which includes "c.h" which includes "d.h" which includes "e.h" which
- includes "a.h"; then the compiler will still hang up.
-
- C++ programs are notorious for long and frenetically branching
- inclusions. If care is not taken to limit the number of #includes
- placed in header files, the probability of encountering a circularity
- increases with every new class that is created. When a circularity
- does occur, finding it can take a very long time. Just realizing that
- the problem is a circularity is a challenge. Finding the sources of
- the circularity is non-trivial. And fixing it is not always that easy
- either.
-
- Thus, it is a good idea, if you are planning a major C++ effort, to
- put some thought into designing the physical dependencies of your
- source files. Moreover, use forward declarations instead of #include
- wherever possible. And to that end, avoid inline functions in your
- header files, if those inlines force you to use a #include rather than
- a forward declartion. Also, use references and pointers as opposed to
- values in data members, member function arguments or return values; if
- those values force you to use #includes rather than forward declarations.
-
- --
- Robert Martin Training courses offered in:
- R. C. M. Consulting Object Oriented Analysis
- 2080 Cranbrook Rd. Object Oriented Design
- Green Oaks, Il 60048 (708) 918-1004 C++
-