Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Header file etiquette

Tags:

People also ask

What should be included in a header file?

the header file (. h) should be for declarations of classes, structs and its methods, prototypes, etc. The implementation of those objects are made in cpp.

Which of the following should not be put in a header file?

Header files should never contain object definitions, only type definitions and object declarations.

Should header files be in a separate folder?

Headers and source files are distinctly different, and are used for different things, so it makes sense to separate them.

What is the main purpose of a header file?

The primary purpose of a header file is to propagate declarations to code files. Header files allow us to put declarations in one location and then import them wherever we need them.


When I am creating a header file, that is going to be used by multiple developers, is it considered good programming practice to make that header file self-sufficient in terms of all the definitions and declarations used in it.

For example:

Header file 1 : types.h

#ifndef TYPES_H
#define TYPES_H
typedef unsigned int uint16
#endif

Header file 2: myheader.h

#ifndef MYHEADER_H
#define MYHEADER_H
uint16 myfunc(void);
#endif

I have used uint16 in myheader.h without including types.h . So if anyone wants to include myheader.h in their source file they should first include "types.h" and then include "myheader.h". So this is actually forcing the developer to include header files in a specific order. I had always thought of this as bad practice, but I came across some code today at my company where to get a function declared in one file you need to include at least 4 other header files. So now I am confused, am I missing something, is there any place where this would be considered expected behaviour.