Tag Info

Hot answers tagged

35

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 ...


24

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 ...


20

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 ...


13

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 ...


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 ...


8

"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 ...


7

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


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

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 ...


7

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 ...


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; ...


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?


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

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

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

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 ...


3

I did it the second way back in '93-95. Took a few minutes to recompile a small app with 5-10 functions/files (on that same 486 PC .. and no, I didn't know about classes either, I was just 14-15 years old and there was no internet). So, what you teach beginners and what you use professionally is vastly different techniques, especially in C++. I think the ...


3

There are most likely several reasons. While private members can't be accessed by most other classes, they can still be accessed by friend classes. So at least in this case they may be needed in the header, so the friend class can see they exist. The recompilation of dependent files may depend on your include structure. Including the .h files in a .cpp file ...


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 ...


3

F#: It optionally allows a .fsi file to declare the symbols like C headers. See http://en.wikibooks.org/wiki/F_Sharp_Programming/Modules_and_Namespaces#Module_Signatures


3

Only thing I could find was the release of Mac OS 8. Not related to CSS release dates, HTML, XML, PHP, Javascript, Internet Explorer, Nescape Navigator, Apache Foundation, MySQL, or anything else relevant I could wrack my brain for. I suspect it's probably someone's kid's birthday or anniversary or something. In short, it's a coincidence.


3

One possible way is to generate metadata ("header files") for each compilation unit first. This normally does not require any dependency and can be performed independently. Then when actual do type checking/code generation, each compilation unit can refer to other's metadata to get type information. Otherwise you will have problem if unit A import unit B ...


3

The approach you're describing is what languages like Java and C# do. Instead of header files, the metadata is compiled into the object file. Those languages also don't compile each source file in a module (a .jar or .dll) separately; they take all the source files that make up a project and compile them all together, and then link them into an executable ...


3

A CSV is, well, a string separated by commas. Trying to separate if there is meaningful data in string as CSV as opposed to A CSV is, well, a string separated by commas is the job for a parser and part of the reason richer data formats (XML, JSON) are often used. This gets even more fun when you have different flavors of CSV format (line endings, header ...


3

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 ...


2

You can do this in D with .di files. It's useful for distributing closed source libraries and such. The .di files can also be generated automatically from the .d files. However, unless you need to distribute a closed-source library or compile times become a major issue and you need to cut down on what the compiler has to parse, it's not idiomatic.


2

If the stakeholders of your source code (say, a small library) consists of "users" (fellow developers who will use your library's functionality without getting involved in its implementation) and "developers" (you and other developers who will implement the library), then put the "users' information" in the header and "implementation note" in the source. ...


2

Including a header allows your code to call functions or methods which are written elsewhere and which may or may not be a part of your software project/build, but which can be found by the 'linker' when you are building the software. For instance, when you call a function in the standard C library then you don't want to have to put all the innards of that ...



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