View Full Version : C++ - When to use .h , and.cpp
howkerman
09-11-2009, 05:02 AM
Hi there, I got a question about cpp.
When do I use .h files, and when should I put it in cpp files?
Wernaeh
09-11-2009, 06:08 AM
As a simple rule of thumbs for starters:
Put everything in a .H file that is required at some place else.
This mostly includes interface, variable and function declarations.
The corresponding definitions belong into .CPP files.
For instance, a method declaration such as
void myMethod(int someParameter);
goes in a .h file. This file then can be #included by some other file, and provides all external information about myMethod: It has a single integer parameter, and doesn't return any value.
The corresponding definition
void myMethod(int someParameter)
{
std::cout << someParameter << std::endl;
}
goes into the .cpp file. As the name already says it, a definition _defines_ what a method actually does - it contains actual code.
Each method may only have one definition in a single .cpp file (each CPP file is compiled exactly once, so if there were two definitions: which one should be used ? ).
Yet each method may be declared once within any .cpp file of some compilation process.
This comes due to the two pass compilation of CPP files:
First, each CPP file is compiled standalone. At this point, the compiler needs to know the syntax (i.e. parameters, return types) of all methods - even if they are defined in another CPP file.
Then comes the linker stage: All method calls from each CPP file are linked to corresponding method definitions within other CPP files. (This is where double definitions make problems)
Hope this helps.
Cheers,
- Wernaeh
howkerman
09-11-2009, 06:29 AM
Thank you very much
Phlex
09-13-2009, 12:55 PM
That is similar to classes as well, as a slightly more advanced expansion of the above example (but still has declaration in .h and definition in .cpp)
Header.h
class MyClass
{
MyClass(int myHeight);
int m_myHeight;
};
then in the cpp
Source.cpp
#include "Header.h"
MyClass::MyClass(int myHeight)
{
m_myHeight = myHeight;
}
So again, in the header we have declaration, then in the source file we have the definition of that declaration.
rouncer
09-14-2009, 10:03 AM
Anything you want to share between files globally goes in the header file as an extern variable declare.
TehOwn
09-24-2009, 08:08 AM
As a rule, everything that you can AVOID putting in the H file... Don't put it in there.
Every time the .h file changes, every other file linking to it has to be recompiled, which might not sound like a big deal for small projects, but it can be a huge deal for larger projects.
Generally, think of it like this.
When you use a car, you have access to certain features:
The Steering Wheel
The Speedometer
The Gearstick
The Acceleration Pedal
etc
These are things that you need to "know about" in order to operate a car.
You don't, however, need to know how they work. You don't need to know what's inside the gearbox, how it switches, and everything else about the inner workings of the car.
The .h file is everything that you need to know to USE what is in the .cpp file. All the inner workings are hidden away in the .cpp file, while the .h is literally just definitions of what you need to know to USE it.
A class should very nearly always have a .h file and a .cpp file.
The .h file contains information on which methods (class functions) the class has and which members (variables / properties) the class stores.
The .cpp file contains all the code for the functions. It contains all of the information on what to do when the methods are called (run).
Hopefully I explained this in a way that was easy to read.
Kenneth Gorking
09-24-2009, 09:10 AM
The .cpp file contains all the code for the functions. It contains all of the information on what to do when the methods are called (run).
The exception being templates :)
vBulletin, Copyright ©2000-2010, Jelsoft Enterprises Ltd.