C++ - 单例模式

C++ 学习笔记

C++ 中单例模式的实现

简介

  • c++ 类 有复制构造函数,今天来介绍一些如何让一个类没有办法被 copy

  • 普通的类, 可以通过复制构造函数类进行 copy

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    class Copyable
    {
    };

    int main()
    {
    Copyable c1;
    Copyable c2 = c1;
    Copyable c3(c1);

    Copyable& ref = c1;
    Copyable c4 = ref;
    Copyable c5(ref);

    return 0;
    }
  • 删除一些 拷贝构造函数, 这样就不可以被 copy 了

    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
    #include<iostream>

    class Noncopyable
    {
    public:
    Noncopyable() = default;
    Noncopyable(const Noncopyable&) = delete;
    Noncopyable& operator=(const Noncopyable&) = delete;
    Noncopyable(Noncopyable&&) = delete;
    Noncopyable& operator=(Noncopyable&&) = delete;
    };

    class Copyable
    {

    };

    int main()
    {
    Copyable c1;
    Copyable c2 = c1;
    Copyable c3(c1);

    Copyable& ref = c1;
    Copyable c4 = ref;
    Copyable c5(ref);

    // -----------------

    Noncopyable n1;
    Noncopyable n2 = n1; // error
    Noncopyable n3(n1); // error

    Noncopyable& n_ref = n1;
    Noncopyable n4 = n_ref; // error
    Noncopyable n5(n_ref); // error

    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
    #include <iostream>
    #include <thread>
    #include <mutex>

    class Noncopyable
    {
    public:
    Noncopyable() = default;
    Noncopyable(const Noncopyable&) = delete;
    Noncopyable& operator=(const Noncopyable&) = delete;
    Noncopyable(Noncopyable&&) = delete;
    Noncopyable& operator=(Noncopyable&&) = delete;
    };

    template<typename T>
    class SingleTon : public Noncopyable
    {
    public:
    static T* instance()
    {
    std::call_once(once_, &SingleTon<T>::Init);
    return instance_;
    }

    static T& get_instance()
    {
    return *instance();
    }

    static void ShutDown()
    {
    delete instance_;
    }

    private:
    static void Init()
    {
    instance_ = new T();
    }

    static std::once_flag once_;
    static T* instance_;
    };

    template<typename T>
    std::once_flag SingleTon<T>::once_;

    template<typename T>
    T* SingleTon<T>::instance_ = nullptr;

    // 单例
    class SingleDemo final : public SingleTon<SingleDemo>
    {
    public:
    int data;
    };

    int main()
    {
    SingleDemo* demo1 = SingleDemo::instance();
    demo1->data = 1;
    std::cout << demo1->data << std::endl;
    demo1->data = 2;

    SingleDemo* demo2 = SingleDemo::instance();
    std::cout << demo1->data << std::endl;

    return 0;
    }
  • 上面这个例子的输出结果为

    1
    2
    1
    2