I am learning singleton design pattern and came across this piece of code
class Singleton
{
private:
static Singleton * pinstance_;
static std::mutex mutex_;
protected:
static Singleton *GetInstance(const std::string& value);
};
Singleton* Singleton::pinstance_{nullptr}; //can this line be removed?
std::mutex Singleton::mutex_; //can this line be removed?
Singleton *Singleton::GetInstance(const std::string& value)
{
std::lock_guard<std::mutex> lock(mutex_);
if (pinstance_ == nullptr)
{
pinstance_ = new Singleton(value);
}
return pinstance_;
}
since pinstance_
and mutex_
are all members of Singleton class, they are accessible by GetInstance
method. So my question is: can these two definition lines be removed?