I have the following doubts on header files usage.
1 - Include guards placing after comments
/* Copyright Note and licence information (multiple lines) */ #ifndef FOO_H #define FOO_H // Header file contents #endif
Herb Sutter says in his "C++ coding standards" book that code like the above is problematic. He is saying the "#ifndef" statements should appear in the first line of the header file. I haven't felt this as convincing. Is this followed by you guys/gals in header files?
2 - Using namespaces in header files
#ifndef FOO_H #define FOO_H namespace FooNameSpace{ // Header file contents } #endif
Is the above code using correct practice? I mean, do you use namespaces in header files? I know why importing a namespace in header file is pointless but what about a declaration like the above?
If the above one is the correct method, how do you do "forward declaration" of a class which is in another namespace? Is it like
#ifndef FOO_H #define FOO_H namespace AnotherNameSpace{ class AnotherFoo; // forward declaration } namespace FooNameSpace{ // Use AnotherFoo here } #endif
The "forward declaration" is the only method to avoid "cyclic dependency", correct?
We can include header files in our program by using one of the above two syntax whether it is pre-defined or user-defined header file. The “#include” preprocessor is responsible for directing the compiler that the header file needs to be processed before compilation and includes all the necessary data type and function definitions.
Those gratuitous includes cause recompilation of things that don't need to be recompiled, and can at times make it so a system can't compile. Don't #include a file in a header if the header itself doesn't need that other header file.
To import a header, use the #include, a preprocessor directive telling the compiler that it should import and process the code before compiling the rest of the code. On a typical C program, it should contain the stdio.h header file, which is the standard header file for input and output streams. We enclose the header name in angle brackets.
Different header files include different functions and different operations. Mainly used to perform input and output operations like print (), scanf (). With this header file, you can execute console input and output operations. Used to perform operations related to date and time. Used to perform jump functions.
The order of the include guards and the comments is purely a matter of style - it won't have any measurable effect on speed of compilation.
Namespaces absolutely should be used in header files for declaring functions, classes, globals, etc. What you should not do is use using
statements in header files -- it's impossible to unuse something in a source file that includes it, and you shouldn't force includers to add extra stuff to the global scope. If you need to use things from other namespaces in your headers, fully qualify every name. It can be a pain sometimes, but it's really the right thing to do.
Examples:
// WRONG! using namespace std; class MyClass { string stringVar; }; // RIGHT class MyClass { std::string stringVar; };
As for forward declarations of classes in other namespaces, you've got it exactly right. Just remember to always qualify AnotherFoo
as AnotherNameSpace::AnotherFoo
when you reference it inside your header. Indeed, forward declarations are the only way to break cyclic dependencies.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With