home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!spool.mu.edu!sol.ctr.columbia.edu!ira.uka.de!ira.uka.de!slsvaat!us-es.sel.de!reindorf
- From: reindorf@us-es.sel.de (Charles Reindorf)
- Subject: Re: Multiple Header Files Problem
- Message-ID: <1992Nov10.104447.12882@us-es.sel.de>
- Sender: news@us-es.sel.de
- Organization: SEL-Alcatel Line Transmission Systems Dept. US/ES
- References: <720993360snx@trmphrst.demon.co.uk> <rmartin.721359424@thor>
- Date: Tue, 10 Nov 92 10:44:47 GMT
- Lines: 74
-
- In article <rmartin.721359424@thor>, rmartin@thor.Rational.COM (Bob Martin) writes:
- |> 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.
- |>
- |> [ etc.]
-
- It is usual, in header files, to structure them so :
-
-
- | # ifndef <HeaderFileName>_h
- | # define <HeaderFileName>_h
- |
- |
- | ... body of include file ...
- |
- |
- | # endif
-
-
- Where of course <HeaderFileName> is some valid identifier based on the name of
- the header file (and intended to be unique).
-
- This prevents cyclic dependencies and has been in common practice for, well, ages.
-
- N.B. It can give rise to other dependency problems, e.g.
-
- Header File A.h:
-
- | # ifndef A_h
- | # define A_h
- |
- | # include <B.h>
- |
- | class A
- | {
- | public:
- | ... etc ...
- | private:
- | B *b;
- | ... etc ...
- | };
- |
- | # endif
-
- Header File B.h:
-
- | # ifndef B_h
- | # define B_h
- |
- | # include <A.h>
- |
- | class B
- | {
- | public:
- | A a_func();
- | ... etc ...
- | };
- |
- | # endif
-
- In which neither header file will scan, but then using forward declarations instead
- of direct includes generally solves these problems.
-
- All options are my own etc.
-
- --- Charles Reindorf
-
-
-