I've been reading different articles and tutorials on Header files.
I understand that Headers serve for the purpose of keeping the "interface" from the implementation. ( and other things like some compile optimization )

What I still don't get, and really can't wrap my mind around it, is do you always use headers?
I know you can write blocks of code within a header file itself. But that is where I get lost.

When I look at a video tutorial, people just define functions with their body within the Header file. Then a different article only defines the functions ( I guess that is the idea of an interface).

This code does work, I'm not getting any errors. But is this the general idea of headers and cpp files?
And my second question:
I read a lot that when using Templates it's easier to just implement the code within the Header I understand this(to prevent many implementations for something that is suppose to be so generic). But are there any other situations as well?
Im breaking my head over this for far too long. Non of the examples I've come across have given me a concrete answer. And it's really getting on my nerves =(

Thank you for taking your time,

Sidar

Edit:
I just realised I'm setting the RGB values in the destructor...LOL.
So yes I am aware of it =)

4 Answers
4

You don't "always" do anything, it all depends on the circumstances, your goals, and the coding standards of your group or organization.

C++ is a very flexible language that allows things to be done and organized in many different ways.

Some reasons separate implementation files might be used:

To keep the implementation separated from the interface, as you
suggest

To speed up compile times

To handle cyclic dependencies

So you can ship a binary library with only the header files and not
the underlying source code

Some reasons why you might not want separate implementation files:

You use templates, which "usually" have to be defined with the
declarations

You DON'T want the implementation separated from the interface. In
many cases this makes things easier to understand as you don't have
to flip back and forth between header and implementation file. This
can be counterproductive if you're dealing with large classes with
many methods, though.

You want as much of your code to be inlined by the compiler as
possible.

You are creating a code library, for which you don't want the user
to have to worry about building. Most of the Boost libraries are
this way, where you don't have to use the Boost build system, which
can be quite a chore, but instead you just include the header files
in your code, and that's all you need to do to use them.

I usually begin the work on a new class by defining all of the logic within the header file. And then later when the class is complete, or when it starts to get crowded in the header file, I will start moving the logic out into a separate implementation file. This is strictly for making the most of my time, as I am able to get things done faster and with fewer bugs when I can see everything in the same file.

It should also be noted that you don't necessarily have to use header files at all. You can define some classes directly in your .cpp files. This is often done for private classes that will never be used outside of that .cpp file.

Thank you for your reply. It makes more sense now. I guess I just have to do more C++ than Java/C# for a while to get used to it. I have to read more on those inline functions though.
–
SidarDec 26 '11 at 18:20

Header files are not used to "separate the interface" from the implementation, they already contain many implementation details unless you use the pimpl idiom. That, is a different topic and it can be done in any language that does not have "header" file like C# and Java.

Their real use comes from the fact that when the compiler must allocate room for an instance of a class, it must know its size. To compute the size, it needs a complete declaration for that class. Since you are probably going to do use instance of that class among many translation units, the only sane way to provide that declaration is to put it in a header file and #include it when needed.

When you create a pointer to an instance (or a reference), the compiler does not need to know the size of that class since pointer have typically the same 4 or 8 byte size. For those cases you can just use a forward declaration instead of #including the header.

Now, the reason why you can include code in a header file is due to the current C++ template mecanics. That code will be compiled many times but the compiler/linker will eliminate all copies and keep a single one in the final compiled code.