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.
Header files should never contain object definitions, only type definitions and object declarations.
Headers and source files are distinctly different, and are used for different things, so it makes sense to separate them.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With