Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practices for use of C++ header files [closed]

Tags:

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?

like image 658
Navaneeth K N Avatar asked Jan 04 '09 05:01

Navaneeth K N


People also ask

How to include header files in a C program?

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.

Why shouldn't I include a file in a header?

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.

How do I import a header in C?

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.

What is the use of header file in Linux?

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.


1 Answers

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

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

like image 200
Adam Rosenfield Avatar answered Sep 23 '22 00:09

Adam Rosenfield