c++头文件和#include 学习笔记

  why we need header files.
  1.It speeds up compile time. As your program grows, so does your code, and if everything is in a single file, then everything must be fully recompiled every time you make any little change. This might not seem like a big deal for small programs (and it isnot), but when you have a reasonable size project, compile times can take several minutes to compile the entire program.Can you imagine having to wait that long between every minor change?
  2.It keeps your code more organized. If you seperate concepts into specific files, It's easier to find the code you are looking for when you want to make modifications (or just look at it to remember how to use it and/or how it works).
  3.It allows you to separate interface from implementation.

  the #include statement is basically like a copy/paste operation. The compiler will "replace" the #include line with the actual contents of the file you're including when it compiles the file.

  the difference between Header files and Source files
  Header files are #included and not compiled, whereas source files are compiled and not #included. You can try to side-step(回避) these conventions and make a file with a source extension behave like a header or vice-versa, but you shouldn't. I won't list the many reasons why you shouldn't (other than the few i already have)--just don't.
  Don't include source files
  Suggestion
  
1.only  #include things you need to include
  2.Guard against incidental multiple inlcude with include guards.
  
example

 1 //=================================
2 // include guard
3 #ifndef __MYCLASS_H_INCLUDED__

4 #define __MYCLASS_H_INCLUDED__
5
6 //=================================
7 // forward declared dependencies
8 class Foo;

9 class Bar;
10
11 //=================================
12 // included dependencies
13 #include <vector>

14 #include "parent.h"
15
16 //=================================
17 // the actual class
18 class MyClass : public Parent // Parent object, so #include "parent.h"
19 {

20 public:
21 std::vector<int> avector; // vector object, so #include <vector>
22 Foo* foo; // Foo pointer, so forward declare Foo
23 void Func(Bar& bar); // Bar reference, so forward declare Bar
24

25 friend class MyFriend; // friend declaration is not a dependency
26 // don't do anything about MyFriend
27 };

28
29 #endif // __MYCLASS_H_INCLUDED__

  Circular Dependencies

 1 // a.h -- assume it's guarded
2 #include "b.h"
3
4 class A { B* b; };
5
6 // b.h -- assume it's guarded
7 #include "a.h"
8
9 class B { A* a };
10
11
12 // a.cpp
13 #include "a.h"

  compile a.cpp
  The compiler will do the following:

 1 #include "a.h"
2
3 // start compiling a.h
4 #include "b.h"
5
6 // start compiling b.h
7 #include "a.h"
8
9 // compilation of a.h skipped because it's guarded
10
11 // resume compiling b.h
12 class B { A* a }; // <--- ERROR, A is undeclared

  Even though you're #including "a.h", the compiler is not seeing the A class util after the B class gets compiled. This is because of the circular inclusion problem.This is why you should always forward declare when you're only using a pointer or reference. Here, "a.h" should not be #including b.h, but instead should just be forward decalring B. Likewise, b.h should be forwarde declaring A. If you make those changes, the problem is solved.
  bad design

  The circular inclusion problem may persist if both dependencied are #include dependencied(ie: they can't be forward declared). here's an example:

 1 // a.h (guarded)
2

3 #include "b.h"
4
5 class A
6 {
7 B b; // B is an object, can't be forward declared
8 };

9
10 // b.h (guarded)
11

12 #include "a.h"
13
14 class B
15 {
16 A a; // A is an object, can't be forward declared
17 };

  You may note, however, that this situation is conceptually impossible. There is a fundamental design flaw. If A has a B object, and B has an A object, then A contains a B, which contains another A, which contains another A,which contains another B, etc,etc. You have an infinite recursion problem, and either class is simply impossible to instantiate. The solution is to have one or both classes contain a pointer or reference to the other, rather than a full object. Then you can forward declare, and then you can get around the circular inclusion problem.

  此为阅读笔记,如想全面阅读请直接看原文:Cplusplus.com;文章中提到的right way to include见解独到。

  不一定要求别人说的一定对(也不可能都对),通过别人写的慢慢的体会。

write by fgd

原文地址:https://www.cnblogs.com/wendao/p/cpp_header_notes.html