[computer-go] C++: #include vs .h files

Mike Young mikewhy at sbcglobal.net
Fri Aug 11 00:07:33 PDT 2006


----- Original Message ----- 
From: "Dave Denholm" <ddenholm at esmertec.com>
To: "computer-go" <computer-go at computer-go.org>
Sent: Thursday, August 10, 2006 4:04 AM
Subject: Re: [computer-go] C++: #include vs .h files


> Peter Drake <drake at lclark.edu> writes:
>
>> That mostly worked, but I have a more subtle problem.
>>
>> I have two classes, BitBoard and BoardState. Each needs to know about
>> the other because the other appears in field declarations or method
>> signatures. If I put
>>
>> #include "BoardState.h"
>>
>> in BitBoard.h and vice versa, there is obviously a circularity. How
>> to get around this?

Refactoring seems likely if a simple forward declare isn't sufficient. That 
is, if the use is by circular containment rather than by pointer or 
reference. If the problem is usage in inline method definitions in the class 
declaration, move them outside.

> The earlier suggestion of surrounding the body of a header file with
> an ifdef would fix this.
>
> BitBoard.h has
>
> #ifndef BITBOARD_H
> #define BITBOARD_H
> #include <BoardState.h>
> ...
> #endif
>
> and similarly for BoardState.h
>
> Then whichever you include first will include the other, but it will
> not continue to recurse because when the second re-includes the first,
> it will find nothing to do because its guard is already defined.

That solves a different problem.




More information about the computer-go mailing list