Advertisement

This is almos embarassing to ask, but....

Started by March 31, 2002 01:28 PM
4 comments, last by the_recon 22 years, 11 months ago
I have never been able to get along with the .h files. Being able to create my own and using ''em never works out so I was wondering... how DO I create an .h file?? Js
Js
Put in a .h file everything that you need to share in between several .c (.cpp, .cc, .C ...) files.

- functions declarations
- inline function definitions (i.e. the function's code)
- class definitions
- variable declarations with extern (The variable must be defined in a single source file).
- global constants
- typedefs

Remember to put #include guards around your header file
#ifndef HEADER_H#define HEADER_H... the actual code for header.h#endif /* HEADER_H */   


Also remember that some dependencies can be solved by using a declaration (a.k.a forward) instead of a definition.
e.g. if Bar only uses references or pointers to Foo, or uses it as a return type, you don't need to have a complete definition for Foo.
class Foo;class Bar{  Foo& ref;  Foo* ptr;  Foo FooFunction( Foo*, Foo& );};   

Which lets you avoid recursion problems (e.g. Foo needs Bar, Bar needs Foo), and removes the need for #includeing a header from another.

As for the .h file itself, it is a mere text file


[edited by - Fruny on March 31, 2002 2:40:10 PM]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Advertisement
the same way you create a cpp, c, or txt file (or whatever)
just type your code into it and save it with an .h extension
if you''re asking how to USE them, its always best to
make sure you only include the h file once, so doing
#ifndef _THIS_H_FILE
#define _THIS_H_FILE

//code goes here

//at the very bottom put this:

#endif

all you gotta do is include the h file in the cpp you need it
for..

ie)
// Test.cpp
#include Test.h
ect..

dunno if that just confuses you more.. maybe you should elaborate
your question a bit
-eldee;another space monkey;[ Forced Evolution Studios ]
quote:
Original post by eldee
#ifndef _THIS_H_FILE



The Standard forbids the use of symbols starting with a single underscore followed by an uppercase letter. Those (and others) are reserved for standard libraries.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Basically your header file is your declaration of certain classes. heres a little example. If you wanted to make a player class.

this is how it would look:


  #ifndef PLAYER#define PLAYER //in here you could place defines and other includesclass CPlayer{   public:      CPlayer();         //Constructor to CPlayer      ~CPlayer();        //Deconctructor to CPlayer   private:};#endif  


Then in a cpp file all you need to do is:


  #include "player.h"CPlayer::CPlayer(){    //Constructor data here.}  


basically the header is the declaration and the cpp file to the header implemation(sp?).

Hope that helps

Jeff D
Suffered seven plagues, but refused to let the slaves go free. ~ Ross Atherton
Thanks! You''ve all been very helpful, all of you!
I can finally get along with .h files =)

Js
Js

This topic is closed to new replies.

Advertisement