博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c++头文件和#include 学习笔记
阅读量:5965 次
发布时间:2019-06-19

本文共 4577 字,大约阅读时间需要 15 分钟。

  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 
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
avector; // vector object, so #include
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.

;文章中提到的right way to include见解独到。

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

转载于:https://www.cnblogs.com/wendao/archive/2011/12/15/cpp_header_notes.html

你可能感兴趣的文章
UIKit 框架之UIControl
查看>>
swift中变量的几种类型
查看>>
[翻译] SoundManager 音频管理器
查看>>
【Oracle】并行等待之PX Deq Credit: need buffer
查看>>
iOS开发UI篇—Quartz2D使用(矩阵操作)
查看>>
第8章 私服nexus
查看>>
网站建设对于哪些刚起步的企业是有必要的
查看>>
【SICP练习】123 练习3.54
查看>>
Spring 整合Quartz 2实现定时任务五:集群、分布式架构实现探讨
查看>>
删除通讯录所有信息
查看>>
字节流通向字符流的桥梁:InputStreamReader
查看>>
多线程调用静态方法
查看>>
Scrum与项目管理亲体验
查看>>
PostgreSQL 如何实现网络压缩传输或加密传输(openssl)
查看>>
keep-alive + vuex + mint + Infinite scroll 保存分页列表数据
查看>>
你真的了解JS数组的那些方法吗?
查看>>
Less与sass
查看>>
Android Room 之存储 Objects 中的 List
查看>>
react16-reactDom.render流程分析
查看>>
用两个队列实现栈
查看>>