单子类模型
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了单子类模型相关的知识,希望对你有一定的参考价值。
Note: This doesn't work in multi-threaded environments, check http://en.wikipedia.org/wiki/Singleton_pattern#C.2B.2BSingleton class model
then:
SingletonClass *pSC = SingletonClass::get_singleton_instance();
pSC->any_public_function();
or: SingletonClass::get_singleton_instance()->any_public_function();
at the end of the program don't forget: SingletonClass::destroy();
//.h class SingletonClass { static SingletonClass *singleton; SingletonClass(); public: static SingletonClass * get_singleton_instance(); static void destroy(); ~SingletonClass(); }; //.cpp SingletonClass *SingletonClass::singleton = NULL; SingletonClass * SingletonClass::get_singleton() { if( singleton == NULL ) singleton = new SingletonClass(); return singleton; } void SingletonClass::destroy() { if( singleton != NULL ) delete singleton; singleton = NULL; } SingletonClass::SingletonClass() { singleton = this; } SingletonClass::~SingletonClass() { }
以上是关于单子类模型的主要内容,如果未能解决你的问题,请参考以下文章
Flutter CustomSingleChildLayout 通用单子布局
场景代码题:有200个骑手都想要抢这⼀个外卖单子,如何保证只有一个骑手接到单子?