# C++ Use of header files?

This topic is 2452 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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]

##### Share on other sites
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.

 class MyClass { public: MyClass(); void someFunction(); };

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

##### Share on other sites
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 on other sites
[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?

1. 1
2. 2
Rutin
21
3. 3
4. 4
A4L
15
5. 5
khawk
14

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633737
• Total Posts
3013612
×