-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Description
Bug Report for https://neetcode.io/problems/singleton
//### Wrong/missing implementation
C++ solution is not correct for singleton and it breaks the principle. Copy constructor+copy assignment should be deleted manually in cpp as it is auto generated.
private:
string value;
Singleton() {}
Singleton (const Singleton&) = delete;
Singleton& operator= (const Singleton&) = delete;
//### Improvements
Also rather than returning * in getInstance we can share reference
(1)static Singleton *getInstance() {
static Singleton instance;
return &instance;
}
should be
static Singleton& getInstance() {
static Singleton instance;
return instance;
}
(2)string getValue() {
return value;
}
should be
const string getValue() const{
return value;
}
(3)void setValue(string &value) {
this->value = value;
}
should be
void setValue(const string &value) {
this->value = value;
}