Sign in to follow this  
Deadlybones

C++ Use of header files?

Recommended Posts

Deadlybones    93
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][/color]

Share this post


Link to post
Share on other sites
Cornstalks    7030
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
[code]
class MyClass
{
public:
MyClass();

void someFunction();
};[/code]

source.cpp
[code]
MyClass::MyClass()
{
// blah...
}

void MyClass::someFunction()
{
// blah...
}[/code]

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 [i]forever[/i].

Share this post


Link to post
Share on other sites
blutzeit    1650
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.

Share this post


Link to post
Share on other sites
turch    590
[quote name='B O N E S' timestamp='1333078400' post='4926550'][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][/color][/quote]

Err then how exactly are those functions / classes used?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this