0

I see the use of a Singleton pointer with online examples, so when I created my own Singleton without a pointer I became very confused as to why every example uses a pointer. Why is a pointer better than a reference? Here's my code:

class SimpleSingleton{
private:
    static int integerDataMember;
    static SimpleSingleton singletonInstance;
    
    SimpleSingleton(){}
public:
    static SimpleSingleton GetInstance(){
        return singletonInstance;
    }
    void IncrementDataMember() const { integerDataMember++; }
    int GetDataMember() const { return integerDataMember; }
    
};
SimpleSingleton SimpleSingleton::singletonInstance;
int SimpleSingleton::integerDataMember = 0;

int main()
{
    SimpleSingleton::GetInstance().IncrementDataMember();
    cout << SimpleSingleton::GetInstance().GetDataMember() << endl;
    {
        SimpleSingleton::GetInstance().IncrementDataMember();
        cout << SimpleSingleton::GetInstance().GetDataMember() << endl;
    }
    
    return 0;
}
  • There is no reference in your code. You are making copies of the singleton. But it is correct that you don't need a pointer. Returning a reference works just as well or better. Your singleton however doesn't have any state, so it is kind of pointless. You could have just made the class a namespace or the member functions `static` and used the functions directly without the `GetInstance()`. – user17732522 Feb 05 '22 at 03:04
  • You're absolutely right, the singleton has no real purpose. I've probably should have mentioned that the code was just to help me practice making a singleton, a simple one at that. However, your answer, "But it is correct that you don't need a pointer. Returning a reference works just as well or better." is a bit underwhelming. From my understanding it seems to be a standard convention than anything else. – bootysucker Feb 05 '22 at 03:17
  • 1
    It was not supposed to be a (full) answer, just a comment. The first sentence is the important part. There is no reference in your code. It should be `static SimpleSingleton& GetInstance()`. Using a `static` member as singleton instance also will cause problems with initialization order. See [this question](https://stackoverflow.com/questions/1008019/c-singleton-design-pattern) for some better patterns. – user17732522 Feb 05 '22 at 03:21
  • ohhh okay, I see. So it's more about it (using a pointer) being a better pattern overall (rather than not using a pointer). – bootysucker Feb 05 '22 at 03:33
  • 1
    @bootysucker A reference is used in the [Meyer's singleton](https://stackoverflow.com/questions/17712001/how-is-meyers-implementation-of-a-singleton-actually-a-singleton), and does not have the initialization or thread-safe issues as of C++11 (though it does have destruction issues, depending on how it's used at program exit). Is that what you are referring to? – PaulMcKenzie Feb 05 '22 at 04:00
  • When defining a singleton class, one approach is (C++11 and later) to `delete` copy/move constructors and assignment operators - which *prevents* returning an instance by value. In any event, a pointer can be initialised to point at no object (i.e. a null pointer) and a reference cannot. If construction of the instance might fail but can reasonably be retried, `GetInstance()` might return a null pointer (to tell caller of need to retry) instead of throwing. Otherwise, returning a reference is fine. The choice of approach depends on needs of the intended application. – Peter Feb 05 '22 at 05:13

0 Answers0