c++ - Is it worth forward-declaring library classes? -


I have started learning QT yet, using their tutorial. I am currently on Tutorial 7, where we have created a new LCD class, the implementation of the LCCRange (CCP file) uses the Qt quaslider class, so In the CPP file

  #include & lt; QSlider & gt;  

But there is a further announcement in the header:

  class QSlider;  

According to Qt,

This is another classic move, but which is less used once we do not need quaslaider in class interface In the implementation only, we use a further declaration of class in the header file and include the header file for the QSlider in the .cpp file.

This makes compilation of large projects very fast, because the compiler usually spends most of its time parsing header files, not actual source code. The movement alone can often have the speed of coordinating by one factor of two or more.

Is it worth doing? It starts to understand, but this is one more thing that I want to take care of myself - I think it would be very easy to include everything in the header file.

There is absolutely C / C + build model ... Important ... for a chronological order (best to say) for big projects It becomes a serious pta

As Neil correctly notes, this should be the default approach to designing your class, do not go out of your way, unless you really need it .

Breaking the circular contains references There is a reason where you have to use further announcements.

  // ah #include "bh" struct A {B * a; } // b.h # include "a.h" / / the reference structure contained in magnitude B {A * A; } // Solution: Break the circular references beyond B or A  

at least the time of rebuilding - imagine the following code

Include
  // foo.h #  

Now every. The CPP file that pulls directly or indirectly into Foo.h also pulls it into QSlider.h and all its dependencies. CPP files can have hundreds! (Precompiled headers do a little bit of help - and sometimes too much - but they change disk / CPU pressure under disk / disk pressure, and thus killing the "soon" range)

If the header Only one reference is needed in the declaration, this dependency can often be limited to some files, such as foo.cpp.

Reducing incremental building time - The effect is more obvious when handling your own (rather than static library) headers Imagine that you have

< Pre> // bar.h # include "foo.h" class bar {Foo * kungFoo; // ...}

Now if most of your. CPP needs to be drawn in twelve, so they are indirectly foo.h. Thus, every change of foo.h changes all of these. CPP files can be created (which may need to know Fu too!). If bar.h uses a forwarding declaration for FU, the dependency on foo.h is limited to bar.cpp:

  // bar.h class Foo; Class bar {Foo * Kungfu; // ...} // bar.cpp #include "bar.h" #include "foo.h" // ...  

It is so common that it There is a pattern - The use of it is two times: First it provides true interface / implementation isolation, succeeding in reducing other dependencies. In practice, I keep the load of their utility 50:50.

You need a reference in the header, you can not have a direct acceleration of the dependent type. This limits the cases where further announcements can be implemented. If you explicitly do this, it is common to use a utility class (such as) for it.

Is it worth the time? , I will say In the worst case construction time, the polynomial increases with the number of files in the project. Other techniques - such as fast machines and parallel builds - can only provide percentage benefits.

The faster the build, the more times the developers will test, the unit tests will be run, the faster build break may be found definite, and less often the developers end up procrastinating.

In practice, while managing the time of its construction, while on a big project (say, hundreds of source files), it still creates "the difference of comfort" on small projects. In addition, Even after this fact, adding improvement is often exercised in patience, since single fixation can only stain seconds (or less) of 40 minutes build.


Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -