Tag Info

Hot answers tagged

42

You should always include all headers defining any objects used in a .cpp file in that file regardless of what you know about what's in those files. You should have include guards in all header files to make sure that including headers multiple times does not matter. The reasons: This makes it clear to developers who read the source exactly what the ...


40

The header iostream.h is a non-standard header and does not exist on all platforms. As a matter of fact it does not exist on my system (using g++ and the GNU libstdc++). So any code using it would simply not compile on my system. The iostream.h header used to be common before C++ was first standardized in 1998. But since the 98 standard used ...


37

#include <iostream.h> is a sign that the book was written prior to the first C++ standard in 1998 (the standard header is iostream). The problem is that older C++ code tends to be written in ways that are considered bad practice today. In particular, The use of C-style arrays rather than container classes like std::string and std::vector. The use ...


37

It is because the C++ compiler must know the actual size of the class in order to allocate the right amount of memory at instantiation. And the size includes all members, also private ones. One way to avoid this is using the Pimpl idiom, explained by Herb Sutter in his Guru of the Week series #24 and #28. Update Indeed, this (or more generally, the header ...


26

My view... Document how to use the function in the header file, or more accurately close to the declaration. Document how the function works (if it's not obvious from the code) in the source file, or more accurately, close to the definition. For the birds-eye thing in the header, you don't necessarily need the documentation that close - you can document ...


22

C and C++ behave very much the same in this regard -- you can have inline functions in headers. In C++, any method whose body is inside the class definition is implicitly inline. If you want to do the same in C, declare the functions static inline.


21

These answers were extracted from the book Patents, Copyright and Trademark, highly recommended. If you plan to buy one, notice that there's a newer edition than that I have. Does a single date imply that the author claims copyright of the file from that date until eternity? "The copyright lasts for the life of the author plus 70 years. However, under ...


17

In C, if you define a function in a header file, then that function will appear in each module that is compiled that includes that header file, and a public symbol will be exported for the function. So if function additup is defined in header.h, and foo.c and bar.c both include header.h, then foo.o and bar.o will both include copies of additup. When you go ...


14

The general rule of thumb is: include what you use. If you use an object directly, then include its header file directly. If you use an object A that uses B but do not use B yourself, only include A.h. Also while we are on the topic, you should only include other header files in your header file if you actually need it in the header. If you only need it in ...


9

What to put in headers: The minimal set of #include statements that are needed to make the header compilable when the header is included in some source file. Preprocessor symbol definitions of things that need to be shared and that can only accomplished via the preprocessor. Even in C, preprocessor symbols are best kept to a minimum. Forward declarations ...


9

"All problems in computer science can be solved by another level of indirection." (quote source) In one of my past projects, we once got such kind of requirement. It has been solved once and forever by inserting a "copyright placeholder" into source code stored under version control: // %%COPYRIGHT PLACEHOLDER%% Whenever any change in that text was ...


8

If you're going to use a tool such as Doxygen (note in the first example, that really looks like a Doxygen comment because it starts with /**) then it doesn't really matter - Doxygen will look through your header and source files and find all the comments to generate the documentation. However, I'd be more inclined to put the documentation comments in the ...


8

While the second version is easier to write, it is mixing interface with implementation. Source files which include header files need to be recompiled everytime the header files are changed. In the first version you'd change the header file only if you need to change the interface. In the second version you'd change the header file if you need to change the ...


8

While you can include .cpp files as you mentioned, this is a bad idea. As you mentioned, declarations belong in header files. These cause no problems when included in multiple compilation units because they do not include implementations. Including a the definition of a function or class member multiple times will normally cause a problem (but not always) ...


7

In short; The header file defines the API for a module. It's a contract listing which methods a third party can call. The module can be considered a black box to third parties. The implementation implements the module. It is the inside of the black box. As a developer of a module you have to write this, but as a user of a third party module you ...


7

The class definition needs to be sufficient for the compiler to produce an identical layout in memory wherever you've used an object of the class. For example, given something like: class X { int a; public: int b; }; The compiler will typically have a at offset 0, and b at offset 4. If the compiler saw this as just: class X { public: int b; ...


7

I keep wondering whether or not I should explicitly include all headers used directly in a particular file Yes. You never know when those other headers might change. It makes all the sense in the world to include, in each translation unit, the headers you know that translation unit needs. We have header guards to ensure that double-inclusion is not ...


6

http://stackoverflow.com/questions/1305947/why-does-c-need-a-separate-header-file http://stackoverflow.com/questions/333889/in-c-why-have-header-files-and-cpp-files http://stackoverflow.com/questions/752793/should-c-eliminate-header-files ...and more


6

Probably somebody did that as an example of a cache expires a long time ago and then everybody copied it. Interestingly it's often expressed as "Mon, 26 July 1997", but the actual date occurred on a Saturday. It's not that interesting in terms of UTC seconds (seconds since 1970) since it is 869893200. Maybe it's just an example of CTRL-C/CTRL-V coding?


6

Read more on the role of the C and C++ preprocessor, which is conceptually the first "phase" of the C or C++ compiler (historically it was a separate program /lib/cpp; now, for performance reasons it is integrated inside the compiler proper cc1 or cc1plus). Read in particular the documentation of the GNU cpp preprocessor. So in practice the compiler ...


5

Some source control systems may not add the header files to source control if they are also not added to the solution file. Your mileage may vary, depending on the source control you use and how you use it.


5

I had proposed a quite similar approach here: http://stackoverflow.com/questions/4248831/shared-config-file-between-php-and-c/4248881#4248881 for a similar issue. I would suggest to write a small script that generates code for C++ and JavaScript, with the configuration you want to have common. You can also use two templates in a template library, that you ...


4

The main reason for a header is to be able to #include it in some other file, so you can use the functions in one file from that other file. The header includes (only) enough to be able to use the functions, not the functions themselves, so (we hope) compiling it is considerably faster. Maintaining the two separately most results from nobody ever having ...


4

Objective-C (and and its C++ counterpart, Objective-C++) can #include C (C or C++) header files as well as #import Objective-C header files. That pairs are even used is by convention. In additon, ASP, Perl, Python, Pascal, and, since the file extension used for its includes doesn't have to follow a standard, PHP. Edit: Most assembler source code also use ...


4

Does a single date imply that the author claims copyright of the file from that date until eternity? The date is usually when the file was created (i.e.: first copyright). If the file was changed, then the date range (or list of years) covers the period of changes until the last change (last copyright). Copyright is time-limited, so the timestamp is ...


4

Mainly just to give you single click editing. The solution explorer doesn't effect the dependancy of the build/compiler. edit. Sometime's it's necessary if you need to 'compile' a header eg. a COM .idl or some Qt moc header file.


4

I wouldn't recommend that you make non-standard use of a standard HTTP header. Primarily because it can be misleading to other developers that know how the Authoriziation header is meant to be used in HTTP authentication, but also to avoid any potential issues with other parts of your stack having conflicting awareness of the same request header. Whatever ...


4

This answer looks at header files from a language design perspective. My point is not that you shouldn't use headers in your C or C++ programs – headers are required by these languages. Instead, this answer is an argument that you shouldn't design new languages that use header files. Header files are bad language design, at least from a modern ...


3

We solved this problem (about 25 years ago) by creating a bunch of #defines (e.g. public, private, etc., that resolved to <nothing>) that could be used in the source file and were scanned by an awk script (horrors!) to auto-generate the .h files. This mean that all of the comments lived in the source and were copied (when appropriate) into the generated ...



Only top voted, non community-wiki answers of a minimum length are eligible