Re: forward declarations of gtkmm stuff



On Wed, 2008-01-02 at 10:06 -0600, Jonathon Jongsma wrote:
> On 1/2/08, Naveen Verma <ernaveenverma gmail com> wrote:
> > Hi,
> >
> > I am sorry but somehow I got confused, I could not understand, does it make
> > any difference in compilation if we include other headers(like windows.h
> > etc) in a header file(e.g. foo.h) or in its source file(e.g. foo.c ) or in
> > both, because in my understanding headers usually have #ifdef in it so it
> > doesn't matter whether we include once or twice, it will be included only
> > once at compile time, and at least we need to include once somewhere either
> > in source or header.
> >
> > -Br
> > Naveen
> 
> Yes, what you say is correct, but the point of forward-declaration is
> somewhat different.  The point is not to prevent headers from being
> included multiple times, but to prevent them from being included at
> all if they're not absolutely necessary.  For example, say you have
> the following setup:
> 
> A.h
> A.cpp
> B.h includes A.h
> B.cpp
> C.h includes B.h
> C.cpp

However, this isn't the case when including a library such as gtkmm.
gtkmm doesn't include your application's headers. I know that's not the
point of your (helpful) explanation - I'm just saying it for the sake of
the original question.

> This is a common (and valid) setup.  In this case, if you change A.h,
> then B.cpp and C.cpp will need to be re-compiled (because they both
> include B.h which then includes A.h).  But if B.h doesn't explicitly
> need the full definition of A, then you could change it to have the
> following setup:
> 
> A.h
> A.cpp
> B.h forward-declares A
> B.cpp includes A.h
> C.h includes B.h
> C.cpp
> 
> Now, when you change A.h, you only need to re-compile B.cpp and not
> C.cpp (since C.h includes B.h which only forward-declares A, and
> doesn't include the full A.h header).  In addition, compiling C.cpp
> should be a little bit faster, because the compiler doesn't need to
> parse and compile all of A.h (and any headers that A.h includes).  For
> a simple example, the difference is not significant, but for complex
> projects, it can make a significant difference in compilation
> depedencies and compilation times.

-- 
murrayc murrayc com
www.murrayc.com
www.openismus.com



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]