• C++ 类、方法的同一声明不同实现的方式


    问题提出

    • 头文件:声明CurrentTime类和PrintTime方法。
    #ifndef CURRENT_TIME_H
    #define CURRENT_TIME_H
    class CurrentTime {
    public:
        void PrintTime();
    };
    #endif
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • main函数:创建CurrentTime对象,调用PrintTime。
    #include "current_time.h"
    
    int main()
    {
        CurrentTime t;
        t.PrintTime();
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    提问:如何使得t.PrintTime()不同的功能?

    解决方案

    编译阶段

    通过编译宏在编译阶段选择不同源码,实现不同的功能。

    • cpp文件
    #include "current_time.h"
    #include 
    void CurrentTime::PrintTime()
    {
    #ifdef RUN_TIME
        std::cout << "运行时间" << std::endl;
    #endif
    #ifdef CURRENT_TIME
        std::cout << "本地时间" << std::endl;
    #endif
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    CMakeList.txt文件:通过定义不同的编译宏,决定需要的功能。

    # add_definitions(-DCURRENT_TIME)
    add_definitions(-DRUN_TIME)
    
    # 给工程添加一个可执行程序
    add_executable(${PROJECT_NAME} 
        main.cpp
        current_time.cpp
    )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    链接阶段

    每个.cpp文件为一个编译单元,生成一个可重定位的目标文件(.o文件) 。链接阶段,通过链接不同的目标文件,实现类或者方法的不同实现。

    • local_time.cpp
    #include "current_time.h"
    #include 
    
    void CurrentTime::PrintTime()
    {
    	std::cout << "本地时间" << std::endl;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • run_time.cpp
    #include "current_time.h"
    #include 
    
    void CurrentTime::PrintTime()
    {
    	std::cout << "运行时间" << std::endl;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    CmakeList.txt文件,可执行文件链接不同的目标文件,实现PrintTime的定制化实现。

    # set(current_time local_time.cpp)
    set(current_time run_time.cpp)
    
    # 给工程添加一个可执行程序
    add_executable(${PROJECT_NAME} 
        main.cpp
        ${current_time}
    )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    动态加载阶段

    libcurrent_time.so用于实现打印时间的功能,具体打印本地时间还是系统时间,取决于生成动态库的源码为local_time.cpp还是run_time.cpp。多数情况下local_time.cpp和run_time.cpp处于不同的产品定制化代码仓内,不同的定制化代码仓都会生成libcurrent_time.so动态库,但其实现的功能不同。

    # 给工程添加一个可执行程序
    # 给工程添加一个可执行程序
    add_executable(${PROJECT_NAME} 
        main.cpp
        # ${current_time}
        # current_time.cpp
    )
    # 生成库
    add_library(current_time SHARED local_time.cpp) 
    # add_library(current_time SHARED run_time.cpp) 
    # 给工程链接库
    target_link_libraries(${PROJECT_NAME} current_time)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    在这里插入图片描述

    运行阶段

    运行阶段可以通过读取配置文件的内容来判断需要执行的功能。

  • 相关阅读:
    贝加莱使用教程1-创建X20工程和点亮LED灯
    UM Company区块链游戏登场全球 元宇宙场景再获国际关注
    C++知识黄金学习记录
    【Java小白福利】Java面试、学习教程合集!
    8年软件测试工程师感悟——写给还在迷茫中的朋友
    leetcode_1339. 分裂二叉树的最大乘积
    计算两个图形遮盖率
    【visual studio】visual studio 2022 无法 复制黏贴
    根据身份证号回填信息
    【Mysql】Mysql获取排班时间段中的休息时间段方法
  • 原文地址:https://blog.csdn.net/XindaBlack/article/details/133237133