Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 200599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:07:33+00:00 2026-05-11T17:07:33+00:00

I’ve just started learning Qt, using their tutorial. I’m currently on tutorial 7, where

  • 0

I’ve just started learning Qt, using their tutorial. I’m currently on tutorial 7, where we’ve made a new LCDRange class. The implementation of LCDRange (the .cpp file) uses the Qt QSlider class, so in the .cpp file is

#include <QSlider>

but in the header is a forward declaration:

class QSlider;

According to Qt,

This is another classic trick, but one that’s much less used often. Because we don’t need QSlider in the interface of the class, only in the implementation, we use a forward declaration of the class in the header file and include the header file for QSlider in the .cpp file.

This makes the compilation of big projects much faster, because the compiler usually spends most of its time parsing header files, not the actual source code. This trick alone can often speed up compilations by a factor of two or more.

Is this worth doing? It seems to make sense, but it’s one more thing to keep track of – I feel it would be much simpler just to include everything in the header file.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T17:07:33+00:00Added an answer on May 11, 2026 at 5:07 pm

    Absolutely. The C/C++ build model is …ahem… an anachronism (to say the best). For large projects it becomes a serious PITA.

    As Neil notes correctly, this should not be the default approach for your class design, don’t go out of your way unless you really need to.

    Breaking Circular include references is the one reason where you have to use forward declarations.

    // a.h
    #include "b.h"
    struct A { B * a;  }
    
    // b.h
    #include "a.h"  // circlular include reference 
    struct B { A * a;  }
    
    // Solution: break circular reference by forward delcaration of B or A
    

    Reducing rebuild time – Imagine the following code

    // foo.h
    #include <qslider>
    class Foo
    {
       QSlider * someSlider;
    }
    

    now every .cpp file that directly or indirectly pulls in Foo.h also pulls in QSlider.h and all of its dependencies. That may be hundreds of .cpp files! (Precompiled headers help a bit – and sometimes a lot – but they turn disk/CPU pressure in memory/disk pressure, and thus are soon hitting the “next” limit)

    If the header requires only a reference declaration, this dependency can often be limited to a few files, e.g. foo.cpp.

    Reducing incremental build time – The effect is even more pronounced, when dealing with your own (rather than stable library) headers. Imagine you have

    // bar.h
    #include "foo.h"
    class Bar 
    {
       Foo * kungFoo;
       // ...
    }
    

    Now if most of your .cpp’s need to pull in bar.h, they also indirectly pull in foo.h. Thus, every change of foo.h triggers build of all these .cpp files (which might not even need to know Foo!). If bar.h uses a forward declaration for Foo instead, the dependency on foo.h is limited to bar.cpp:

    // bar.h
    class Foo;
    class Bar 
    {
       Foo * kungFoo;
       // ...
    }
    
    // bar.cpp
    #include "bar.h"
    #include "foo.h"
    // ...
    

    It is so common that it is a pattern – the PIMPL pattern. It’s use is two-fold: first it provides true interface/implementation isolation, the other is reducing build dependencies. In practice, I’d weight their usefulness 50:50.

    You need a reference in the header, you can’t have a direct instantiation of the dependent type. This limits the cases where forward declarations can be applied. If you do it explicitely, it is common to use a utility class (such as boost::scoped_ptr) for that.

    Is Build Time worth it? Definitely, I’d say. In the worst case build time grows polynomial with the number of files in the project. other techniques – like faster machines and parallel builds – can provide only percentage gains.

    The faster the build, the more often developers test what they did, the more often unit tests run, the faster build breaks can be found fixed, and less often developers end up procrastinating.

    In practice, managing your build time, while essential on a large project (say, hundreds of source files), it still makes a “comfort difference” on small projects. Also, adding improvements after the fact is often an exercise in patience, as a single fix might shave off only seconds (or less) of a 40 minute build.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 120k
  • Answers 120k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I'd suggest you you test yourself, e.g. on Java Black… May 12, 2026 at 12:04 am
  • Editorial Team
    Editorial Team added an answer I have to agree with Marc, but this is perhaps… May 12, 2026 at 12:04 am
  • Editorial Team
    Editorial Team added an answer I fixed the problem on my machine by editing this… May 12, 2026 at 12:04 am

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.