I have this class in singleton.h
:
class SingletonSample {
public:
static SingletonSample& Instance();
int DoSomething() const;
private:
SingletonSample();
~SingletonSample();
SingletonSample(const SingletonSample&) = delete;
SingletonSample& operator=(const SingletonSample&) = delete;
static SingletonSample* _instance;
}
And I have this implementation in singleton.cpp
:
SingletonSample::SingletonSample() {}
SingletonSample::~SingletonSample() {}
SingletonSample& SingletonSample::Instance() {
if(!_instance) _instance = new SingletonSample();
return _instance;
}
int SingletonSample::DoSomething() const {
return 20;
}
I have a compiler error when trying to return _instance
.
I want to return a SingletonSample&
, but my _instance
is a pointer SingletonSample*
.
I'm a beginner in C++ but as far as I understand, a reference to something is just the address to that something, while a pointer to something is just a pointer to that address.
I would like to return that address being pointed by the pointer, as a reference SingletonSample&
.
However, I cannot manage to achieve that. How can I do this?