定义文件:
-
#ifndef C_SINGLETON_H
-
#define C_SINGLETON_H
-
#include<iostream>
-
#include<memory>
-
using namespace std;
-
class CSingleton
-
{
-
private:
-
CSingleton(){ cout << "单例对象创建!" << endl; };
-
CSingleton(const CSingleton &);
-
CSingleton& operator=(const CSingleton &);
-
~CSingleton(){ cout << "单例对象销毁!" << endl; };
-
static void Destory(CSingleton *){ cout << "在这里销毁单例对象!" << endl; };//注意这里
-
static shared_ptr<CSingleton> myInstance;
-
public:
-
static shared_ptr<CSingleton> getInstance()
-
{
-
return myInstance;
-
}
-
};
-
#endif
主文件:
-
//主文件,用于测试用例的生成
-
#include<iostream>
-
#include<memory>
-
#include"CSingleton.h"
-
using namespace std;
-
shared_ptr<CSingleton> CSingleton::myInstance(new CSingleton(), CSingleton::Destory);
-
int main()
-
{
-
shared_ptr<CSingleton> ct1 = CSingleton::getInstance();
-
shared_ptr<CSingleton> ct2 = CSingleton::getInstance();
-
shared_ptr<CSingleton> ct3 = CSingleton::getInstance();
-
return 0;
-
}
内容来自https://blog.youkuaiyun.com/cjbct/article/details/79266057