template<typename T>
class Singleton
{
public:
static T& getInstance() {
static T t;
return t;
}
Singleton(const Singleton&) = delete;
Singleton& operator=(const Singleton&) = delete;
protected:
Singleton() = default;
~Singleton() = default;
};
例:
#include<iostream>
class Test:public Singleton<Test>
{
public:
void myprint()
{
std::cout<<"test Singleton"<<std::endl;
}
};
int main()
{
Test::getInstance().myprint();
return 0;
}
标签:11,Singleton,const,C++,static,单例,public
From: https://www.cnblogs.com/penuel/p/17858875.html