• 单例的例子


    #include 
    #include 
    #include 
     
    // 使用方式1:模板参数方式
    class CTest:public boost::noncopyable // 不能复制、赋值
    {
    public:
        void Set(int i){ m_val = i; }
        void Print() const{ std::cout<<m_val<<std::endl;}
    private:
        int m_val;
    };
    // 使用typedef以及宏来简化使用
    typedef boost::serialization::singleton<CTest> singleton_ctest; // 使用模板的方式只允许单个实例
    #define sCTest singleton_ctest::get_mutable_instance() // 非const实例
    #define sCTest_const singleton_ctest::get_const_instance() // const实例
     
     
    // 使用方式2:继承方式
    class CDerived:public boost::serialization::singleton<CDerived> // 只允许单个实例,不能复制、赋值(基类派生自boost::noncopyable)
    {
    public:
        void Set(int i){ m_val = i; }
        void Print() const{ std::cout<<m_val<<std::endl;}
     
    private:
        int m_val;
    };
    #define sCDerived CDerived::get_mutable_instance() // 非const实例
    #define sCDerived_const CDerived::get_const_instance() // const实例
     
    int main()
    {
    //     singleton_ctest obj1;
    //     singleton_ctest obj2;
        sCTest.Set(10);
        sCTest_const.Print(); // 10
     
        // 操作类的非static成员
        CDerived cobj;
        CDerived cobj2;
        cobj2.Set(89);
        cobj.Set(10);
        
        // 单例的概念:并非是不能定义两个类对象本身
        // 而是当有多个对象时,通过boost提供的静态成员函数get_mutable_instance()返回实例再调用普通成员函数之后,
        // 类对象的结果保持一致,就好像类中的所有数据成员都是static一样
     
        // 这时的类好像定义了两种同名的数据成员以及成员函数:static与非static
        // 非static由普通方式调用:即先定义类对象,再调用成员函数
        // static先调用由boost提供的get_mutable_instance()(或get_const_instance())再调用成员函数的方式
        // 从而保证单例(因为类的所有成员共享static)
        // 两种调用互不影响(各自数据成员是分离的),唯一的影响是类现在派生自boost::noncopyable
     
        //操作类的“static成员”
        CDerived::get_mutable_instance().Set(20);
        CDerived::get_mutable_instance().Print(); // 20
        // same way:
        sCDerived.Set(20); 
        sCDerived.Print();  // 20
        /*sCDerived_const.Set(89); // error*/
        sCDerived_const.Print(); // 20
        cobj2.get_mutable_instance().Print(); // 20
        cobj.get_mutable_instance().Print(); // 20
     
        // 操作类的非static成员
        cobj2.Print(); // 89
        cobj.Print(); // 10
     
        // 实际使用中,我们没有必要也不应该直接定义类对象,而是应该总是通过使用类的static成员函数:
        // classname::get_mutable_instance().memberFunction(para);(或get_const_instance())来确保“单个实例”
        // 本例中定义的宏的使用方式与类对象的使用方式相同:name.member_function(para);
        // 可以将宏当做“对象”
        
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
  • 相关阅读:
    汉诺塔与二进制、满二叉树的千丝万缕
    基础ArkTS组件:帧动画,内置动画组件,跑马灯组件(HarmonyOS学习第三课【3.6】)
    如何通过AI视频智能分析,构建着装规范检测/工装穿戴检测系统?
    FPGA 图像缩放 千兆网 UDP 网络视频传输,基于B50610 PHY实现,提供工程和QT上位机源码加技术支持
    emment语法
    Mybatis Plus配置多个数据源
    【论文分享】A White Paper on Neural Network Quantization【1】
    “蔚来杯“2022牛客暑期多校训练营9 G题: Magic Spells
    代码随想录二刷 Day 30
    js给一段话,遇到的第一个括号处加上换行符
  • 原文地址:https://blog.csdn.net/qq_40178082/article/details/133318942