3 ms·
There is nothing wrong with having code in headers.
by clarry 10y ago
There is nothing wrong with having code in headers.
- pjc50 10y agoIf you put a bare function or variable in a header, then use it in multiple compilation units, the linker will reject your multiply-defined symbols. (Templates get de-duplicated).
- vonmoltke 10y agoUnless you mark them 'static', which is common practice for constants and small inlined functions.
- gpderetta 10y agoInline is the keyword here (litterally). Inline, in C++, is defined as disabling the requirement of having exactly one definition of a function (it is also a weak hint for the compiler to perform inlining). Template functions are implicitly inline. Inline in C has a similar but subtly different definition.
- Annatar 10y agoIt's called a header for a reason, unless we are now redefining the meaning and intent of headers, so that people could stick C++ template code in them?
- clarry 10y agoOh, where is the officially sanctioned definition of the meaning and intent of a header? What we call them doesn't matter. There is nothing wrong with putting code in one. You can call them source files if you prefer.
- Annatar 10y agoOh, where is the officially sanctioned definition of the meaning and intent of a header? So glad you asked: The C Programming language, 2nd edition, page 82, chapter "FUNCTIONS AND PROGRAM STRUCTURE", as follows: "There is one more thing to worry about--the definitions and declarations shared among the files. As much as possible, we want to centralize this, so that there is only one copy to get right and keep right as the program evolves. Accordingly, we will place this common material in a header file, calc.h, which will be included as necessary." https://youtu.be/4PaWFYm0kEw?t=2237 https://youtu.be/4PaWFYm0kEw?t=2237
- clarry 10y agoRight, definitions and declarations. Common material. Code.