There are lots of similar answers to this question such as this one:
- Include .cpp instead of header(.h)
- https://stackoverflow.com/a/1001723/637142 (this answer really says it is bad)
Stack overflow will prbably say I have a duplicate question but I still do not understand after looking into all the similar questions why people discourage this code:
#ifndef _FOO
#define _FOO
// my comment that I only have to write once
int some_method()
{
return 1;
}
#endif
Call this foo.cpp
. Why will it be bad practice to do something like this where foo.h
is excluded? Compile time will be slower but how much slower? I believe it will be milliseconds slower. Why is being lazy a bad thing (https://stackoverflow.com/a/9253825/637142). I just want to write something that works, is well documented etc..
Benefits I see when using this technique are:
I do not have to write comments twice. If I decide to change the comments of a function I will also have to change the comments on the header file.
If I change the signature of a method then I will also have to change it on the header file.
I use F12 shortcut on visual studio a lot to go to the definition of a method for example. Visual studio by default will go to the header file. I want to go to the actual implementation. Seeing code plus comments is better for me than just seeing comments.
The only negative side I see is https://stackoverflow.com/a/1001667/637142 . But that is less work than having to write everything twice. Just a few includes and thats it.
Edit
I know I am probably wrong and I a missing something. Since I am a C# developer its hard for me to understand it. I just do not understand why this file will be included multiple times. Yes it will be included multiple times but only the first time will be compiled. The other times it will not be compiled correct?