C++之 头文件

bash:

g++ a.cpp b.cpp --save-temps -Wall

Definition of a class

  • In C++,separated .h and .cpp files are used to define one class.
  • Class declaration and prototypes in that class are in the header file(.h).
  • All the bodies of these functions are in the source file(.cpp).

The header files

  • If a function is declared in a header file,you must include the header file everywhere the function is used and where the function is defined.
  • If a class is declared in a header file,you must include the header file everywhere the class is used and where class member functions are defined.

Header = interface

  • The header is  a contract between you and the user of your code.
  • The compile enforces the contract by requirig you to declare all structures and functions before they are used.


Declarations vs. Definitions

  • A .cpp file is a compile unit
  • Only declaratons are allowed to be in .h
    • extern variables
    • function prototypes  //函数原型
    • class/struct declaration

#include

  • #include is to insert the included file into the .cpp file at where the #include statement is.
    • #include "xx.h":first search in the current directory,then the directories declared somewhere
    • #include <xx.h>:search in the specified directories
    • #include <xx>:same as #include <xx.h>

Standard header file struct

#ifndef HEADER_FLAG

#define HEADER_FLAG

//Type declaration here...

#endif   //  HEADER_FLAG

Tips for header

  1. One class declaration per header file
  2. Associated with one source file in the same prefix of file name.
  3. The contents of a header file is surrounded with #ifndef #define #endif
时间: 2024-08-09 10:38:50

C++之 头文件的相关文章

CUDA gputimer.h头文件

#ifndef __GPU_TIMER_H__ #define __GPU_TIMER_H__ struct GpuTimer { cudaEvent_t start; cudaEvent_t stop; GpuTimer() { cudaEventCreate(&start); cudaEventCreate(&stop); } ~GpuTimer() { cudaEventDestroy(start); cudaEventDestroy(stop); } void Start() {

Facebook App 的头文件会有更多的收获

最近在看一些 App 架构相关的文章,也看了 Facebook 分享的两个不同时期的架构(2013 和 2014),于是就想一窥 Facebook App 的头文件,看看会不会有更多的收获,确实有,还不少.由于在选择 ipa 上的失误,下了个 7.0 版的 Facebook(最新的是 18.1),会稍有过时,不过后来又下了个 18.1 的看了下,发现变动其实不大.以下是我从头文件中获取到的一些信息(20多万行,浏览起来还是挺累的) 让视图组件可以方便地配置 这个在 Facebook 的演讲中也提

上传伪技术~很多人都以为判断了后缀,判断了ContentType,判断了头文件就真的安全了。是吗?

今天群里有人聊图片上传,简单说下自己的经验 0.如果你的方法里面是有指定路径的,记得一定要过滤../,比如你把 aa文件夹设置了权限,一些类似于exe,asp,php之类的文件不能执行,那么如果我在传路径的时候,前面加了一个../呢,这样这种服务器端的限制就跳过了.(DJ音乐站基本上都有这个问题,以及用某编辑器的同志) 1.常用方法:这种就是根据后缀判断是否是图片文件,需要注意的是这种格式:文件:1.asp;.jpg  1.asp%01.jpg  目录: 1.jpg/1.asp 1.jpg/1.

C++中的头文件和源文件

一.C++编译模式通常,在一个C++程序中,只包含两类文件——.cpp文件和.h文件.其中,.cpp文件被称作C++源文件,里面放的都是C++的源代码:而.h文件则被称作C++头文件,里面放的也是C++的源代码.C+ +语言支持“分别编译”(separate compilation).也就是说,一个程序所有的内容,可以分成不同的部分分别放在不同的.cpp文件里..cpp文件里的东西都是相对独立的,在编 译(compile)时不需要与其他文件互通,只需要在编译成目标文件后再与其他的目标文件做一次链

头文件&lt;stdio.h&gt;

头文件<stdio.h>声明了三种类型,一些宏和很多执行输入输出的函数.1.类型:FILE 它是一个对象类型,可以记录控制流需要的所有信息,包括它的文件定位符.指向相关的缓冲的指针.记录是否发生了读/写错误的错误指示符和记录文件是否结束的文件结束符.fpos_t 它是一个对象类型,可以唯一指定文件中的每一个位置所需的所有信息.size_t 这是无符号整数类型,它是sizeof关键字的结果.2.宏:NULL 这个宏是一个空指针常量的值._IOFBF._IOLBF.IONBF 这些宏扩展了带有特定

头文件&lt;setjmp.h&gt;

头文件<setjmp.h>定义了宏setjmp,并且为了绕过正常的函数调用和返回规则声明了一个函数和一个类型. 1.类型jmp_buf 它是一个数组类型,适合存储恢复一个调用环境所需的信息. 2.宏setjmp int setjmp(jmp_buf env); 说明:宏setjmp将它的调用环境保存在它的jmp_buf类型的参数中,以供后面longjmp使用. 返回值:如果返回一个来自直接的调用,则宏setjmp返回0:如果返回一个来自longjmp的调用,则宏setjmp返回一个非0值. 3

opencv头文件

转载自:http://blog.csdn.net/aaron121211/article/details/51526901 1. .hpp文件是.h和.cpp文件在一起的2. #include <opencv2/opencv.hpp> 就包含了opencv库所有头文件,所以很多时候都是直接include 3.一些用到的库介绍 [highgui] --也就是high gui,高层GUI图形用户界面,包含媒体的I / O输入输出, 视频捕捉.图像和视频的编码解码.图形交互界面的接口等内容 [cal

头文件

1:stdio 就是指 "standard input & output"(标准输入输出),所以,源代码中如用到标准输入输出函数时,就要包含这个头文件! 2:stdlib 头文件即standard library标准库头文件.stdlib.h里面定义了五种类型.一些宏和通用工具函数. 类型例如size_t.wchar_t.div_t.ldiv_t和lldiv_t: 宏例如EXIT_FAILURE.EXIT_SUCCESS.RAND_MAX和MB_CUR_MAX等等: 常用的函数

C语言extern的使用以及头文件*.h的内容格式注意

用VS2013 分开写多文件的程序,出现了许多重定义的问题,总结解决方法如下: 在*.h文件中使用以下的格式: #ifndef <标识> #define <标识> ...... ...... #endif <标识>在理论上来说可以是自由命名的,但每个头文件的这个"标识"都应该是唯一的.标识的命名规则一般是头文件名全大写,前后加下划线,并把文件名中的"."也变成下划线,如:stdio.h #ifndef _STDIO_H_ #def

[转]Linux学习笔记——例说makefile 头文件查找路径

0.前言 从学习C语言开始就慢慢开始接触makefile,查阅了很多的makefile的资料但总感觉没有真正掌握makefile,如果自己动手写一个makefile总觉得非常吃力.所以特意借助博客总结makefile的相关知识,通过例子说明makefile的具体用法. 例说makefile分为以下几个部分,更多内容请参考[例说makefile索引博文] 1.只有单个C文件 2.含有多个C文件 3.需要包括头文件路径 4.增加宏定义 5.增加系统共享库 6.增加自定义共享库 7.一个实际的例子 [