basic question to .h and .cpp files
-
Hi, I´m writing on an application that is "small" but with several classes with their own header file... To avoid having to much files around I have written all the code in the header files...i.e. class declarations, function declarations and definitions etc. (I have read several places that it doesn´t really matter) This is probably bad code-writing, but is there any drawback to this? Do the compiler treat code in the .h file differenly? Is there code that cannot be written in the header file? (sorry if this is a stupid question, but still I feel like a beginner in this) doneirik
-
Hi, I´m writing on an application that is "small" but with several classes with their own header file... To avoid having to much files around I have written all the code in the header files...i.e. class declarations, function declarations and definitions etc. (I have read several places that it doesn´t really matter) This is probably bad code-writing, but is there any drawback to this? Do the compiler treat code in the .h file differenly? Is there code that cannot be written in the header file? (sorry if this is a stupid question, but still I feel like a beginner in this) doneirik
-
Hi, I´m writing on an application that is "small" but with several classes with their own header file... To avoid having to much files around I have written all the code in the header files...i.e. class declarations, function declarations and definitions etc. (I have read several places that it doesn´t really matter) This is probably bad code-writing, but is there any drawback to this? Do the compiler treat code in the .h file differenly? Is there code that cannot be written in the header file? (sorry if this is a stupid question, but still I feel like a beginner in this) doneirik
In general, I think everyone will agree that header files should be used for declarations, and source files for definitions. Actually, sometimes you won't be able to do without source files. For example for static variables, for initialization of global variables, etc etc
-
Hi, I´m writing on an application that is "small" but with several classes with their own header file... To avoid having to much files around I have written all the code in the header files...i.e. class declarations, function declarations and definitions etc. (I have read several places that it doesn´t really matter) This is probably bad code-writing, but is there any drawback to this? Do the compiler treat code in the .h file differenly? Is there code that cannot be written in the header file? (sorry if this is a stupid question, but still I feel like a beginner in this) doneirik
doneirik wrote: To avoid having to much files... why would you like to reduce the number of files ? to have only one that is about 80Kb weight ??? :~ wow, good luck ! lol well, in my case, i prefer having several file (one header associated with a module - .cpp), where each module implements a logical section. Well, you should not be so strict either. For example, if you have a set of functions that are about to represent some Exceptions of your own in your program, you can put them in the same file, including also the functions implementation if they are writing on few lines. Of course, this is a suggestion, you can do whatever you like, but i also think that if most of experienced programmers separate the actual instructions from declarations, they do have some reasons... doneirik wrote: Do the compiler treat code in the .h file differenly? actually, .h files are not compiled. The
#include
directive tells the pre-processor to copy the content of the .h file where such instruction is written in a file. doneirik wrote: Is there code that cannot be written in the header file? Yep, affirmative !! A header can contain : - named namespaces (namespace N { /*...*/ }
); - Types definitions (struct Point { int x, y; };
) - Templates declarations (template<class T> class Z;
) - Templates definitions (template<class T> class V { /*...*/ };
) - Functions declarations (extern int strlen(const char*);
) - inline functions definitions (inline char get(char* p) { return *p++; }
) - Datas declarations (extern int a;
) - Constants definitions (const float pi = 3.141593;
) - Enumerations (enum Light { red, yellow, green };
) - Names declarations (class Matrix
) - Inclusion directives (#include <algorithm>
) - Macros definitions (#define VERSION 12
) - Conditional compilation directives (#ifdef _cplusplus
) - Comments (/* End of file */
) In the other hand, a header might never contain : - Ordinary functions definitions (char get(char* p) { return *p++; }
) - Datas definitions (int a
) - Arrays definitions (short tbl[] = {1, 2, 3};
) - Non-named namespaces (namespace { /*...*/ }
) - Exported templates definitions (expor
-
Hi, I´m writing on an application that is "small" but with several classes with their own header file... To avoid having to much files around I have written all the code in the header files...i.e. class declarations, function declarations and definitions etc. (I have read several places that it doesn´t really matter) This is probably bad code-writing, but is there any drawback to this? Do the compiler treat code in the .h file differenly? Is there code that cannot be written in the header file? (sorry if this is a stupid question, but still I feel like a beginner in this) doneirik
Just reorg your directories and put arrange the files that way. The only implementation I put in header files is 1-line returns and/or single "set" functions. At least I'm not as bad as some people I know, they have a seperate cpp file for every function in the header! My rule is, break out another cpp if your function is 30-ish lines or longer, just for clarity. (although there should be very few cases where you need such a big function) ~Nitron.
ññòòïðïðB A
start