Skip to content

Bug Report for singleton #5193

@Gauravkrprabhat

Description

@Gauravkrprabhat

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;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions