1

In C++, I use a singleton class and refer to the only instance in another class. I'm wondering what is the most efficient way to access this instance since it is a large data structure. I considered these things.

  1. Getting a reference from the singleton class, other than passing the data structure by value
  2. Using a global instance in my second class which uses the singleton class:

    Singleton* singleInstance;
    
    SingletonUser::SingletonUser ()
    {
        singleInstance = Singleton::getInstance(); //passes the single instance by   reference, then it will be used in the class wherever needed
    }
    
  3. Doing the same thing inside a function of the second class so that I get a reference to the singleton class's instance when I want it (Need to access it several times in several functions).

I'm not sure which practice is the best one. Can someone explain, and if there is a more efficient way, explain that too?

Thanks!

Izza
  • 2,389
  • 8
  • 38
  • 60
  • 2
    If your `getInstance()` method returns a pointer as in your 2nd example, then no matter what size the object has, you're only paying for cost of invoking the `getInstance()` method and copying a pointer. What, exactly, are you trying to optimize? – André Caron Jan 28 '12 at 04:29
  • @ André Caron: The time. The Singleton class has a data structure which is pretty large. That is why I thought of passing by reference. Is there a better way to do this? – Izza Jan 28 '12 at 04:32
  • 2
    @Izza: But if you're not passing it by value, then a copy is never being made (and if you were, it wouldn't be a singleton in the first place). What other way besides returning a reference did you have in mind? – Cameron Jan 28 '12 at 04:36
  • 4. [Not using a singleton](http://jalf.dk/blog/2010/03/singletons-solving-problems-you-didnt-know-you-never-had-since-1995/). – Georg Fritzsche Jan 28 '12 at 04:40
  • @ André Caron: Yes, that is why I chose pass by reference. Other than that, the options mentioned, either keeping a global copy of accessing it from the singleton class as it is needed. – Izza Jan 28 '12 at 04:46

3 Answers3

3

If you're passing your singleton instance by value, then it's not really a singleton, is it?

Simply return a reference (or a pointer) to the instance whenever you need to access it (#1). Caching the reference once for each client is only going to add complexity and almost certainly won't be any faster.

I'm not sure what the difference is between #1 and #3, besides added complexity. Both use a function to access the singleton instance via a reference/pointer.

Cameron
  • 96,106
  • 25
  • 196
  • 225
  • I was under the impression that if you need to do it several times in the class, that it is better to cache. But I see what you mean. Since it will happen for each user, won't be much useful. thanks! – Izza Jan 28 '12 at 04:39
  • @Izza: But you'd be caching a pointer! How would it be faster to get a pointer from your instance instead of the singleton class? – Cameron Jan 28 '12 at 04:41
  • yes, I get it, it is not a good way. Thanks for the expalnation. – Izza Jan 28 '12 at 04:48
2

Firstly in C++ avoid pointers when possible, instead pass by reference:

Singleton& Singleton::getInstance() {
    return *singleton;
}

Then in the code:

Singleton& st = Singleton::getInstance();
st.someFunction();
David C. Bishop
  • 6,437
  • 3
  • 28
  • 22
1

Your option #3 is most preferable. Try to not cache references unnecessarily - those cached references tend to be a maintenance burden as time goes on.

And just in general, try avoid the singleton pattern. In C++, a global function (non-member) is just as good and allows the design to evolve.

mcmcc
  • 822
  • 6
  • 12
  • 2
    If I read #3 right, then it's caching a reference once for each client instance (whereas #1 gets the reference from the class itself). – Cameron Jan 28 '12 at 04:37
  • 1
    @Cameron: you might be right. If so I mean #1. In any case, needless caching == bad. – mcmcc Jan 28 '12 at 04:49
  • @mcmcc: I thought singletons are preferred over global functions. Why is a global function is better than Singleton? – Izza Jan 28 '12 at 04:51
  • posted why globals over singleton as a separate question. – Izza Jan 28 '12 at 04:59