作爲與非智能指針的區別,使用智能指針時,在創建單例模式對象時還是有區別的,具體:red display;
#include <iostream>
#include <string>
#include <memory>
using namespace std;
class Single
{
public:
virtual ~Single() = default;
static std::shared_ptr<Single> getSingleInstance()
{
if (m_Sing == nullptr)
{
//m_Sing = std::make_shared<Single>();
m_Sing.reset(new Single());
}
return m_Sing;
}
void fun()
{
cout << "test Single model" << endl;
return;
}
static std::shared_ptr<Single> m_Sing;
private:
Single() = default;
};
std::shared_ptr<Single> Single::m_Sing = nullptr;
int main()
{
auto temp = Single::getSingleInstance();
temp->fun();
return 0;
}