C/C++头文件

C/C++头文件

上节我们介绍了类型定义(typedef)。C++类型定义(typedef)

本节我们介绍头文件。

  • 在一个团队里面共同开发时,代码通常在不同的文件中。另外一些共通的东西,例如宏定义,类型定义等都该放到不同的文件中。

什么是#include

  • 预处理指令 - 编译前包含指定文件内容到当前文件中,即使用被包含文件替换源文件中的#include指令。

  • #include 指令有两种形式:

    • #include <iostream> ⬅文件名包含在尖括号中<标准系统目录>
    • #include "test.h" ⬅文件名在双引号中<当前目录>

头文件的作用

  • 代码重用:不同人写的相同作用的代码可能不同,为了防止组合在一起的时候浪费时间修改。

  • 封装:把某些具有共性的函数定义放在同一个源文件里。

  • 提高代码的可维护性

头文件的使用

  • 此处写两个源文件(.cpp)并且包含相同的变量。

    //testA.cpp
    #include <iostream>
    
    struct Position{
    	int x;
        int y;
    };
    
    void testA(){
        std::cout << "此处是testA" << std::endl;
    }
    
    //想要使用别的源文件的函数必须要声明
    void testB();
    
    int main(){
        struct Position pos;
        pos.x = 10;
        pos.y = 10;
        testA();
        testB();
        
        return 0;
    }
    
    //testB.cpp
    #include <iostream>
    
    struct Position{
    	int x;
        int y;
    };
    
    //函数前面除非带上static否者就是局部的函数,如果main函数所在的头文件不声明, 只在当前源文件有效
    void testB(){
        struct Position pos;
        pos.x = 10;
        pos.y = 10;
        std::cout << "此处是testB" << std::endl;
    }
    

    输出结果:

    此处是testA
    此处是testB
    

    上面的例子,两个源文件中的函数都是对外可见的(可以看见函数体)

  • 另外当testA中结构体增加了新的变量,那么testB可能不知道,在之后的开发中可能会产生歧义。此时需要将共同的内容放到一个头文件中(.h)

    //test.h
    #include <iostream>
    struct Position {
        int x;
        int y;
    };
    
    //想要使用别的源文件的函数必须要声明
    void testA();
    void testB();
    
    //testA.cpp
    #include <iostream>
    #include "test.h" //包含当前目录的头文件
    
    void testA(){
        std::cout << "此处是testA" << std::endl;
    }
    
    int main(){
        struct Position pos;
        pos.x = 10;
        pos.y = 10;
        testA();
        testB();
        
        return 0;
    }
    
    //testB.cpp
    #include <iostream>
    #include "test.h" //包含当前目录的头文件
    
    //函数前面除非带上static否者就是局部的函数,如果main函数所在的头文件不声明, 只在当前源文件有效
    void testB(){
        struct Position pos;
        pos.x = 10;
        pos.y = 10;
        std::cout << "此处是testB" << std::endl;
    }
    

    上面把共同的结构体与函数声明放到了头文件中。效果相同,但是更加具有共通性,更加简洁。

保护措施

  • 注意头文件不要相互包含,会导致编译失败!!
    例如:

    //testA.h
    #include <iostream>
    #include "testB.h"
    
    void testA(){
         std::cout << "此处是testA" << std::endl;
    }
    
    //testB.h
    #include <iostream>
    //相互包含,TestA.h中也包含了TestB.h
    #include "testA.h"
    
    void testB()){
         std::cout << "此处是testB" << std::endl;
    }
    
  • 保护措施:(再头文件前面加上会让头文件只会被包含一次)

    • #pragma onece ()
    • #ifndef ...
      #define ...
      #endif
    //testA.h
    //只包含一次指令
    #pragma once
    
    #include <iostream>
    
    void testA(){
         std::cout << "此处是testA" << std::endl;
    }
    
    //testB.h
    //条件编译
    #ifndef _TESTB_H_
    #define _TESTB_H_
    
    #include <iostream>
    
    void testB(){
         std::cout << "此处是testA" << std::endl;
    }
    
    #endif
    
    //main.cpp
    #include <iostream>
    //由于#pragma once存在,头文件只包含一次
    #include "testA.h"
    #include "testA.h"
    #include "testA.h"
    //由于条件编译指令存在,头文件只包含一次
    #include "testB.h"
    #include "testB.h"
    #include "testB.h"
    
    int main(){
        
        return 0;
    }
    

本节介绍了头文件。下节我们进入面向对象的编程。

  • 7
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 11
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值