#include <iostream>
#include <mutex>
class MyCAS
{
public:
static MyCAS* GetInstance()
{
if (m_instance == nullptr)
{
std::cout << "构造创建了" << std::endl;
m_instance = new MyCAS();
static CGarhuishou cl;
}
return m_instance;
}
class CGarhuishou
{
public:
~CGarhuishou()
{
if (MyCAS::m_instance)
{
delete MyCAS::m_instance;
MyCAS::m_instance = nullptr;
}
}
};
void func()
{
std::cout << "测试单例模式" << std::endl;
}
//virtual ~MyCAS()
//{
// std::cout << "析构函数执行了" << std::endl;
// delete m_instance;
//}
private:
MyCAS() //私有化构造函数
{
}
private:
static MyCAS* m_instance;
};
MyCAS* MyCAS::m_instance = nullptr;
int main()
{
MyCAS* p_a = MyCAS::GetInstance();
MyCAS* p_b = MyCAS::GetInstance();
p_a->func();
return 0;
}
单例设计模式
最新推荐文章于 2024-08-28 23:58:14 发布