Re: forward declarations of gtkmm stuff



Hi,

Thanks a lot for your clarification, I also use forward deceleration many times, but usually when I need to declare multiple classes in same header which are using each other. But mostly I include the basic headers( e.g. cstring) all the places in source and header even thought I am using only pointer in header, just because of my understanding about ifdef in header will take care during compile time.

-Br
Naveen

On Jan 2, 2008 6:06 PM, David L <idht4n gmail com> wrote:


On Jan 2, 2008 7:54 AM, 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.

Even though the headers have ifdef wrappers to prevent
a c file from getting multiple definitions of things, the compiler
still has to grovel through the entire header file to look for the
endif.  And that only prevents multiple inclusion... it's best to
avoid single inclusion when is isn't necessary.

So it is considered good practice to forward declare
anything that can be forward declared instead of including
header files.  In my previous example, when compiling bar.cpp
that includes foo.h, the compiler doesn't need to know anything
about the details of Gtk::Window, so a forward declaration of
Window is adequate and prevents bar.cpp from including
potentially thousands of lines of header files when foo.h itself
might be only a dozen lines.  As Jonathon pointed out, it can
greatly decrease compile times.

Here's a link I found with google that talks about this:
http://www.goingware.com/tips/parameters/headers.html

hope this helps...

         David

 


-Br
Naveen


On Jan 2, 2008 5:37 PM, Jonathon Jongsma < jonathon quotidian org> wrote:
On 1/2/08, Murray Cumming <murrayc murrayc com> wrote:
>
> On Wed, 2008-01-02 at 08:45 -0600, Jonathon Jongsma wrote:
> > On 1/2/08, David L < idht4n gmail com> wrote:
> > > Sorry if this is a stupid question, but how can I forward declare
> > > gtkmm things so that gtkmm header files don't need to be
> > > included before defining a class that has gtkmm pointers?
> > >
> > > For example, I'd like to get rid of the two includes in this header file:
> > > // foo.h
> > > #include <gtkmm/window.h>
> > > #include <gtkmm/radioaction.h>
> >
> > namespace Gtk {
> > class Window;
> > class RadioAction;
> > }
>
> You are never going to discover all the things that you need to declare
> even just for Gtk::Window. The headers exist for a good reason and I
> suggest that you use them.

IMO, In this situation ( e.g. in a header), forward-declaration is
perfectly fine.  If you don't ever dereference the pointer, then you
don't need to discover anything extra that needs to be declared.  On
the other hand, if you do de-reference it then you do need to include
the header (but you don't usually need to do this in a header, usually
just in the source file where it's used).  I do this all the time in
headers and it does speed up compilation, sometimes significantly.

--
jonner
_______________________________________________
gtkmm-list mailing list
gtkmm-list gnome org
http://mail.gnome.org/mailman/listinfo/gtkmm-list


_______________________________________________
gtkmm-list mailing list
gtkmm-list gnome org
http://mail.gnome.org/mailman/listinfo/gtkmm-list





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