CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

View Poll Results: Which do you use

Voters
9. You may not vote on this poll
  • #include <Header>

    5 55.56%
  • Forward declaration

    1 11.11%
  • Both

    3 33.33%
Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2009
    Posts
    1,689

    Preference question about libraries

    I just started using Qt and something stuck me as odd. In almost all examples I found, objects that are used in header files always seem to be handled with forward declarations instead of including the header to them. I see this from time to time and I'm just wondering how common it is.

    In the poll, assume that the files and classes that I am referring to are from a library, not from your code, where circular dependency occasionally makes it impossible to include.

    Code:
    class QButton;
    class QScrollArea;
    
    class myclass {
    private:
         QButton * button;
         QScrollArea * scroll;
    }
    or

    Code:
    #include <QButton>
    #include <QScrollArea>
    
    class myclass {
    private:
         QButton * button;
         QScrollArea * scroll;
    }
    I much prefer the include because that way, if I include this file elsewhere in my code, all those headers are also included and the objects can be manipulated. All libraries are header guarded, so that's not an issue.

  2. #2
    Join Date
    Oct 2008
    Posts
    1,456

    Re: Preference question about libraries

    definitely the "include <header>" version; and if there's a specific reason to use a forward declaration I define and include a "<headerfwd>" ( like <iosfwd> in STL ). BTW, a liberal use of forward declarations could also introduce hard to track bugs, like when deleting a pointer to an incomplete type ...

    PS: I assume the poll is not focused on QT only, isn't it ?
    Last edited by superbonzo; September 12th, 2011 at 11:16 AM.

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Preference question about libraries

    Not only does this make accidental circular dependencies much more unlikely, it also reduces compile times and minimizes rebuild dependencies.

    In your second case, the compiler must build the QButton header every time it builds the myclass header even though the contents of the QButton header are not strictly needed there. Certainly, if myclass.cpp is the only place to include the myclass header, it makes no difference whether QButton is included form the .h or from the .cpp. But this is not typically the case. Someone including myclass.h should not be given a dependency on <QButton> unnecessarily, as it will just slow down the compilation.

    For libraries which are relatively unchanging, rebuild dependencies aren't really an issue. But in your own code, which may change frequently during development, they certainly are. Let's say that QButton is your header, and you've just made a change to it. In the first case, only myclass.cpp needs to be rebuilt; in the second, everything that includes myclass.h needs to be rebuilt.

  4. #4
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,164

    Re: Preference question about libraries

    I use both, I usually include the header for internal code, but for cases where you do not want to expose certain parts of code, or want to hide complex code from translation units that have no need to know anything about that code (i.e. hide from client code), I use pImpl (pointer to implementation) and hence use forward declarations.

    In addition, as Lindley has pointed out, on other occasions, forward declarations sometimes make sense for compilation performance reasons or to avoid circular dependencies.

    EDIT:

    Hehehe, I only read the topic title, but didn't read the specification in the OP's post... which I guess is probably clear from my answer above. Anyway, in the context of the OP's post, I use forward declarations.
    Last edited by PredicateNormative; September 15th, 2011 at 12:35 PM.

  5. #5
    Join Date
    Oct 2006
    Location
    Singapore
    Posts
    346

    Thumbs up Re: Preference question about libraries

    I tend to use forward declarations if it would suffice. The reasons are exactly the same as what Lindley mentioned.
    Believe in your Dreams, Work for what you Believe in.
    My thoughts? Angelo's Stuff
    Some fun things I've done: RayWatch, QuickFeed, ACSVParser

    @ngelo

  6. #6
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Preference question about libraries

    I only use forward declaration with circular dependencies.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured