class HeapOnly
{
public:
static HeapOnly* getInstance()
{
return new HeapOnly;
}
private:
HeapOnly()
{}
HeapOnly(const HeapOnly& ho) = delete;
};
class StackOnly
{
public:
static StackOnly getInstance()
{
return StackOnly();
}
private:
StackOnly()
{}
};
class StackOnly2
{
public:
static StackOnly2 getInstance()
{
return StackOnly2();
}
void * operator new(size_t n) = delete;
private:
StackOnly2()
{}
};
class NoCopy
{
public:
NoCopy()
{}
private:
NoCopy(const NoCopy& nc) = delete;
NoCopy& operator=(const NoCopy& nc) = delete;
};
class NoH
{
private:
NoH()
{}
};
class NoH2 final
{};
class singleton
{
public:
static singleton* getInstance()
{
return &_single;
}
private:
singleton()
{}
singleton(const singleton& s) = delete;
singleton& operator=(const singleton& s) = delete;
static singleton _single;
};
singleton singleton::_single;
class singleton2
{
public:
static singleton2* getInstance()
{
if (_ptr == nullptr)
{
_mtx.lock();
if (_ptr == nullptr)
{
_ptr = new singleton2;
}
_mtx.unlock();
}
return _ptr;
}
class GC
{
public:
~GC()
{
if (_ptr)
{
delete _ptr;
}
}
};
private:
singleton2()
{}
singleton2(const singleton2& s) = delete;
singleton2& operator=(const singleton2& s) = delete;
static singleton2* _ptr;
static mutex _mtx;
static GC _gc;
};
singleton2* singleton2::_ptr = nullptr;
mutex singleton2::_mtx;
singleton2::GC singleton2::_gc;