c++单例模式
1.实现方法
[1]把构造函数设置为private,因此不能在外部创建实例,并且提供一个public方法访问实例。
[2]把析构函数是private和public都可以,private时需要定义一个垃圾回收类自动销毁这个实例,public时可以手动delete。
2.单实例销毁的方法
【1】把单例对象放在静态区初始化:
此时在程序退出后会自动调用类的析构函数。
【2】使用垃圾回收类:
包含一个垃圾回收类,并且初始化一个该类的静态对象,当程序退出时,该静态对象会自动调用这个类的析构函数,这个类的析构函数里面再调用外部类的析构函数。
那为什么不是程序退出时直接自动调用外部类的析构函数就可以了?
因为程序退出时,在静态区的对象(非指针,而是对象的内存在静态区)会自动调用析构函数,而在堆或栈的对象不会自动调用析构函数。
因此垃圾回收类也有一个静态对象,程序退出时会自动调用静态对象的析构函数。而单例对象只是指针在静态区,对象则在堆区,因此程序退出时不会自动调用单例对象的析构函数。
3.实现代码
[1]饿汉式单例:实例作为类的静态成员变量,程序启动时在静态区创建实例,程序退出时自动调用析构函数,并且线程安全(此例子把析构设置为public,手动delete实例):
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 | #include <iostream> #include <stdio.h> using namespace std; class singleton { public : static singleton *get_instance(){ static singleton single; return &single; } ~singleton(){ printf( "~singleton() called.\n" ); } private : singleton(){ printf( "singleton() called.\n" ); } }; int main( int argc, char *argv[]) { singleton *s = singleton::get_instance(); return 0; } |
[2]懒汉式单例:第一次访问的时候创建实例(此时把析构设置为private,并且添加了垃圾回收类):
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 | #include <iostream> #include <stdio.h> using namespace std; class singleton { public : static singleton *get_instance(){ if (m_instance == NULL){ m_instance = new singleton(); printf( "get_instance() called.\n" ); } return m_instance; } ~singleton(){ printf( "~singleton() called.\n" ); } private : singleton(){ printf( "singleton() called.\n" ); } class gc { public : gc(){ printf( "gc() called.\n" ); } ~gc(){ printf( "~gc() called.\n" ); if (m_instance != NULL){ delete m_instance; m_instance = NULL; } } }; static gc m_gc; static singleton *m_instance; }; singleton *singleton::m_instance = NULL; singleton::gc singleton::m_gc; int main( int argc, char *argv[]) { singleton *s = singleton::get_instance(); return 0; } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步