What are the advantages and disadvantages of implementing classes in header files?

I like the concept of DRY (don't repeat yourself [oops]), but the concept of C ++ header files contradicts this programming rule. Is there a flaw in defining the entire class member in the header? If this is correct for templates, why not for regular classes? I have some ideas for the disadvantages and advantages, but what do you have?

+11
c ++
Nov 23 '09 at 15:27
source share
6 answers

Possible benefits of putting everything in header files:

  • Less redundancy (which leads to easier changes, simplified refactoring, etc.).
  • May provide the compiler / linker with better optimization options.
  • It is often easier to incorporate into an existing project.

Possible disadvantages of placing everything in the header files:

  • Longer compilation / link loops
  • Loss of separation of interface and implementation
  • Can lead to hard-to-reach circular dependencies
  • Many inlays may increase the size of the executable
  • Prevents binary compatibility of shared libraries / DLLs
  • Upset employees who prefer traditional ways to use C ++
+23
Nov 23 '09 at 15:35
source share

Well, one problem is that typical implementations change much more often than class definitions, so for a large project you have to recompile the world for every small change.

+17
Nov 23 '09 at 15:33
source share

The main reason is not to implement the class in the header file: should the consumers of your class need to know the details of its implementation? The answer is almost always no. They just want to know which interface they can use to interact with the class. Having an implementation of the class visible in the header makes it very difficult to understand what kind of interface it is.

In addition to considerations of compactness and separation of the interface from the implementation, there are also commercial motives. If you are developing a library for sale, you (probably) do not want to pass on the implementation details of the library you are selling.

+6
Nov 23 '09 at 15:40
source share

You do not repeat. You write code only once in one header. It is repeated by the preprocessor, but this is not your problem, and this is not a violation of DRY.

If this is correct for templates, why not for regular classes

This is not exactly what you need to do for templates. This is the only one that really works as a whole.

In any case, if you implement the class in the header, you get the following advantages and disadvantages:

  • The full implementation is visible wherever it is used, which simplifies assembly if necessary.
  • The same code will be analyzed and compiled several times, which will lead to higher compilation times.
  • On the other hand, if everything is in the headers, this can lead to fewer translation units, so the compiler should work fewer times. Ultimately, you can get a single translation unit, which includes only once, which can lead to very fast compilations.

And ... what is it really.

Most of my code tends to be in the headers, but that is because most of my code is templates.

+6
Nov 23 '09 at 16:21
source share

The main disadvantage (in addition to long assemblies) is the lack of a clear separation of interface and implementation.

Ideally, you will not need to see an implementation of an intuitive and well-documented interface.

+2
Nov 23 '09 at 19:12
source share

Not mentioned: virtual functions are created for each of them, so you can inflate your executable file (I'm not sure if this is true for all compilers).

There is an alternative:
Do a lot of things in classes declared in your source file. For example, pimpl-idiom, but there are also people who are afraid to declare classes from the header file. However, this makes sense for private classes.

0
Nov 23 '09 at 15:57
source share



All Articles