C++ Use of header files?

Started by
2 comments, last by turch 12 years ago
Today I got really fed up with my use of header files in C++. Somewhere along the line I've been put under the assumption that every class should have a header file. I guess my only reasoning is for "[color=#000000][font=Arial,]separating the interface from the implementation" as some random person on a google search puts it. Is it considered good coding practice to create a header file for each class to do this? Basically none of my classes depend on functions in other header files but that is really the only reason that I might want to use them. I'll probably just scrap the header file per class thing unless someone persuades me not to as it's really annoying to have to declare all my variables in one place, define them in another, and same with functions. Any advice would be appreciated.[/font]
Advertisement
Header files are used when you want to use something in two (or more) different files. If you've got some class X, and you want to use that class to create X objects in somefile.cpp and otherfile.cpp, each source file would need to know about the definition of class X, and unless you want to painstakingly type this out and copy and paste it directly into both source files, you should create a header file that defines class X and then include it in both source files.

Or are you talking about something else? Are you possible talking about putting the member function declarations in the header file and the member function definitions in the source file? i.e.

header.hpp

class MyClass
{
public:
MyClass();

void someFunction();
};


source.cpp

MyClass::MyClass()
{
// blah...
}

void MyClass::someFunction()
{
// blah...
}


If you're talking about this second case, then your question is not about having header files for each class; it's about having source files for each class. And one good reason for doing it like this (among many) is that you can change the source file all you want, and if you leave the header file the same, you only have to recompile that one source file. If you put all the code in the header file and no source file, and you make one change (maybe you add a new blank line), every file that includes the header file must be recompiled, which in large projects can take forever.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
For "normal" object-oriented classes the convention is to declare it in the header file and define it in the source file. This convention originates from technicalities in how the C/C++ compiler and linkers, and have become good practices (one class per pair of files, shielded with #ifndef-#defines, etc).

But there are of course exceptions. Inline definitions goes into the header file. Helper functions that are really private to the class can be defined as static functions in the source file only (although I tend to make it a static private class function). Same for private helper classes. Some "modules" don't map nicely to a single class, such as aggregates of closely inter-related classes, or non-object-oriented things, like algorithms.

Whatever you put in a source file (classes, methods, etc) there has to be at least some interface to it so that someone else can access it. Strive to make that interface (i.e. header file) as clean and readable as possible.

openwar - the real-time tactical war-game platform

[color=#000000][font=Arial,]Basically none of my classes depend on functions in other header files but that is really the only reason that I might want to use them.[/font]


Err then how exactly are those functions / classes used?

This topic is closed to new replies.

Advertisement