Jump to content


C++ - When to use .h , and.cpp


6 replies to this topic

#1 howkerman

    New Member

  • Members
  • Pip
  • 3 posts

Posted 11 September 2009 - 12:02 PM

Hi there, I got a question about cpp.

When do I use .h files, and when should I put it in cpp files?

#2 Wernaeh

    Senior Member

  • Members
  • PipPipPipPip
  • 368 posts

Posted 11 September 2009 - 01:08 PM

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
Some call me mathematician, some just call me computer guy. Yet, I prefer the term professional weirdo :)

#3 howkerman

    New Member

  • Members
  • Pip
  • 3 posts

Posted 11 September 2009 - 01:29 PM

Thank you very much

#4 Phlex

    Member

  • Members
  • PipPip
  • 53 posts

Posted 13 September 2009 - 07: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.

#5 rouncer

    Senior Member

  • Members
  • PipPipPipPip
  • 2258 posts

Posted 14 September 2009 - 05:03 PM

Anything you want to share between files globally goes in the header file as an extern variable declare.
you used to be able to fit a game on a disk, then you used to be able to fit a game on a cd, then you used to be able to fit a game on a dvd, now you can barely fit one on your harddrive.

#6 TehOwn

    New Member

  • Members
  • Pip
  • 9 posts

Posted 24 September 2009 - 03:08 PM

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.
Conducting a survey of what is needed and wanted by those interested or involved in Games Development, please take it!
http://spreadsheets....6UC1zOV9YRlE6MA

#7 Kenneth Gorking

    Senior Member

  • Members
  • PipPipPipPip
  • 907 posts

Posted 24 September 2009 - 04:10 PM

TehOwn said:

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 :)
"Stupid bug! You go squish now!!" - Homer Simpson





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users