class declaration versus class header #include

Discussion in 'C++' started by Andrew, Dec 4, 2007.

  1. Andrew

    Andrew Guest

    Hi,

    I see some people prefer to use in headers (where possible) class
    declaration:

    // B.h
    class A;

    class B
    {
    A* m_pa;
    };

    instead of including the class header file.

    // B.h
    #include "A.h"

    class B
    {
    A* m_pa;
    };


    is it just a personal preference or is it more (like faster compile
    time) ?

    thank you
    Andrew, Dec 4, 2007
    #1
    1. Advertising

  2. Andrew wrote:
    > I see some people prefer to use in headers (where possible) class
    > declaration:
    >
    > // B.h
    > class A;
    >
    > class B
    > {
    > A* m_pa;
    > };
    >
    > instead of including the class header file.
    >
    > // B.h
    > #include "A.h"
    >
    > class B
    > {
    > A* m_pa;
    > };
    >
    >
    > is it just a personal preference or is it more (like faster compile
    > time) ?


    The latter. If the compiler doesn't *need* to see the definition,
    there is no sence for it to see it.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Dec 4, 2007
    #2
    1. Advertising

  3. Andrew

    yurec Guest

    On Dec 4, 3:51 pm, Andrew <> wrote:
    > Hi,
    >
    > I see some people prefer to use in headers (where possible) class
    > declaration:
    >
    > // B.h
    > class A;
    >
    > class B
    > {
    > A* m_pa;
    >
    > };
    >
    > instead of including the class header file.
    >
    > // B.h
    > #include "A.h"
    >
    > class B
    > {
    > A* m_pa;
    >
    > };
    >
    > is it just a personal preference or is it more (like faster compile
    > time) ?
    >
    > thank you


    There is also nice chapter in Satter Exceptional c++, Compiler
    Firewalls and the Pimpl Idiom.
    Maybe you find it interesting.
    yurec, Dec 4, 2007
    #3
  4. yurec wrote:
    > [..]
    > There is also nice chapter in Satter Exceptional c++, Compiler


    <nit> Sutter's </nit>

    > Firewalls and the Pimpl Idiom.
    > Maybe you find it interesting.


    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Dec 4, 2007
    #4
  5. Andrew wrote:
    > (like faster compile time) ?


    IMO it's not really the faster compile time (after all, it's quite
    fast for the compiler to parse two header files, certainly not
    significantly slower than parsing just one) as much as it's decreasing
    dependencies between source files. The (sometimes very significantly)
    faster compile times come indirectly from this. When a header file is
    changed, the less dependencies to it there are in the program, the less
    compilation units need to be recompiled.

    Nothing is more irritating and time-consuming than having to make a
    small change in a header file and then having to wait 15 minutes for the
    program to recompile. By decreasing header dependencies it may well be
    that that recompilation time is reduced to 30 seconds or whatever along
    those lines.
    Juha Nieminen, Dec 4, 2007
    #5
  6. Juha Nieminen wrote:
    > Andrew wrote:
    >> (like faster compile time) ?

    >
    > IMO it's not really the faster compile time (after all, it's quite
    > fast for the compiler to parse two header files, certainly not
    > significantly slower than parsing just one) as much as it's decreasing
    > dependencies between source files. The (sometimes very significantly)
    > faster compile times come indirectly from this. When a header file is
    > changed, the less dependencies to it there are in the program, the
    > less compilation units need to be recompiled.


    And the final result is?...

    >
    > Nothing is more irritating and time-consuming than having to make a
    > small change in a header file and then having to wait 15 minutes for
    > the program to recompile. By decreasing header dependencies it may
    > well be that that recompilation time is reduced to 30 seconds or
    > whatever along those lines.


    So, it's not really the faster compile time, right? Then what is it?

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Dec 4, 2007
    #6
  7. Victor Bazarov wrote:
    > So, it's not really the faster compile time, right? Then what is it?


    Compiling from scratch doesn't become significantly faster, but in
    certain situations making a change and then recompiling can become much
    faster.
    Juha Nieminen, Dec 7, 2007
    #7
  8. Juha Nieminen wrote:
    > Victor Bazarov wrote:
    >> So, it's not really the faster compile time, right? Then what is it?

    >
    > Compiling from scratch doesn't become significantly faster, but in
    > certain situations making a change and then recompiling can become
    > much faster.


    So, "re-compiling" is not really "compiling" then, is it? Faster
    *re*-compiling cannot be called "faster compiling". I get it.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Dec 7, 2007
    #8
  9. Andrew

    werasm Guest

    On Dec 4, 2:51 pm, Andrew <> wrote:

    > is it just a personal preference or is it more (like faster compile
    > time) ?


    Apart from all the other valid comments, I could perhaps
    add that often, when having a name dependency to an
    interface in another namespace, or perhaps a template,
    depending on the likelihood of the class/interface changing,
    I have contemplated simply including instead of a forward
    declaration. This is even more the case when the association
    is provided from externally (not via factory), implying
    that the using class would have to include as well regardless.

    I'm not particularly fond of :

    namespace X{ namespace Y{
    template <class T, class U>
    class SomeClass;
    }
    }

    Regards,

    Werner
    werasm, Dec 7, 2007
    #9
  10. "Victor Bazarov" <> writes:

    > Juha Nieminen wrote:
    >> Andrew wrote:
    >>> (like faster compile time) ?

    >>
    >> IMO it's not really the faster compile time (after all, it's quite
    >> fast for the compiler to parse two header files, certainly not
    >> significantly slower than parsing just one) as much as it's decreasing
    >> dependencies between source files. The (sometimes very significantly)
    >> faster compile times come indirectly from this. When a header file is
    >> changed, the less dependencies to it there are in the program, the
    >> less compilation units need to be recompiled.

    >
    > And the final result is?...
    >
    >>
    >> Nothing is more irritating and time-consuming than having to make a
    >> small change in a header file and then having to wait 15 minutes for
    >> the program to recompile. By decreasing header dependencies it may
    >> well be that that recompilation time is reduced to 30 seconds or
    >> whatever along those lines.

    >
    > So, it's not really the faster compile time, right? Then what is it?


    Faster build time. The compile time for each individual file is unchanged,
    but reducing dependencies usually means fewer files need to be recompiled
    for a given change, reducing the overall build time.

    sherm--

    --
    WV News, Blogging, and Discussion: http://wv-www.com
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Sherman Pendley, Dec 7, 2007
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Aguilar, James
    Replies:
    2
    Views:
    673
    Aguilar, James
    Jul 16, 2004
  2. Replies:
    3
    Views:
    553
  3. Andreas Bogenberger
    Replies:
    3
    Views:
    875
    Andreas Bogenberger
    Feb 22, 2008
  4. mlt
    Replies:
    2
    Views:
    808
    Jean-Marc Bourguet
    Jan 31, 2009
  5. Paul Butcher
    Replies:
    12
    Views:
    683
    Gary Wright
    Nov 28, 2007
Loading...

Share This Page