0

Hello Stack Overflow.

I'm studying for a CS exam I have tomorrow and I'm looking over the review sheet that our professor provided for us. One of the points says to know why Singleton is a better choice than Global.

I'm not exactly sure the reasoning, but I do remember him saying something about Singleton using less "namespace pollution." I made several Google searches comparing Singleton and Global, but pretty much all of the results said that both are poor options lol. So it just confused me anymore.

If anyone can help me clear this up, it'll be greatly appreciated. Thanks!

ModdedLife
  • 669
  • 2
  • 11
  • 24
  • 3
    Singletons *are* global objects though. It's like asking why methods are a better choice than member functions. – Pubby Mar 04 '13 at 01:54
  • 1
    Well, there's the [static order initialization fiasco](http://www.parashift.com/c++-faq/static-init-order.html) which is a downer, but neither are great. – Stephen Lin Mar 04 '13 at 01:54
  • 1
    @Pubby I presume he means lazily-constructed singletons versus statically constructed globals. – Stephen Lin Mar 04 '13 at 01:55
  • Hmm. On the review sheet, it says "Singleton uses less namespace pollution. Why is it better than Global?" – ModdedLife Mar 04 '13 at 01:55
  • 3
    Ah, [found it](http://jalf.dk/singleton). – chris Mar 04 '13 at 01:57
  • 2
    I don't agree with "uses less namespace polution". Surely there is still one name for either a global or singleton class. The names may be different, but still as much a name of some sort to potentially collide with another. But if that's the answer your tutor requires, then memorize it. – Mats Petersson Mar 04 '13 at 01:57
  • 3
    @ModdedLife You create one named global object containing a bunch of values and functions, instead of a bunch of functions and values that each have their own globally available name. – weltraumpirat Mar 04 '13 at 01:58
  • @weltraumpirat Ok, that definitely helps. Thanks. – ModdedLife Mar 04 '13 at 02:01
  • @weltraumpirat Yes, but you'd get the same effect [assuming the functions and values belong together] from having one global object with methods and member variabls - which also only has one globally visible name. – Mats Petersson Mar 04 '13 at 02:10
  • @MatsPetersson It's not entirely the same effect - the Singleton is bound to its class, whereas the global object just exists freely. But apart from that, you are right, of course. – weltraumpirat Mar 04 '13 at 09:20

1 Answers1

1

They are lazy constructed:

LargeObject   global;                    // Large object is always constructed.

class LargeObject_Singelton
{
    public: static LargeObject& getInstance()
    {
        static LargeObject  instance;     // This object is not created until the first
        return instance;                  // time getInstance() is called.
    }                                     // If you never use it this can be important.
};
Martin York
  • 257,169
  • 86
  • 333
  • 562
  • 2
    Hmm. Can you explain a little bit more in-depth please? – ModdedLife Mar 04 '13 at 02:06
  • 1
    Can be lazy constructed. Lazy construction is actually a problem if more than one thread may be accessing them (since you need another global object to lock the first call to instance). – john.pavan Mar 04 '13 at 02:07
  • @john.pavan: Not true in C++11 and in C++03 gcc it was not a problem. In C++03 in DevStuio some work but not a major problem: http://stackoverflow.com/a/449823/14065 – Martin York Mar 04 '13 at 02:10
  • 1
    @ModdedLife: Not sure what else there is to say. The global is always created. The singelton is only created when you need it. If the cost of creating the object is large you may not want to create the object unless you need it. – Martin York Mar 04 '13 at 02:12
  • Lazy construction means they're instantiated the first time the instance function is called. This is different from a global b/c global (and static member variables) are instantiated before any threads start. The lazy nature gives you control over initialization order (just reference each singleton's instance, in the correct order at the start of your main thread). – john.pavan Mar 04 '13 at 02:13
  • And there is only a benefit in Lazy construction if the object is only used rarely - it would make sense for a function like "Print" in a WordProcessing program, but not for "Open" and "Save" functions [not that any of those would be singletons in a sane program - but I think it purveys the point]. – Mats Petersson Mar 04 '13 at 02:13
  • 1
    I'd argue that if there's a large object that wasn't always needed, you'd want to be able to delete it at some point. – john.pavan Mar 04 '13 at 02:15
  • 1
    @john.pavan: Singelton is correctly destroyed when the program finishes. And why would you destroy something that is expensive to create. That jsut means you have to pay the cost again. The whole point of lazy evaluation is to put of the cost (if you don't need it then don't pay it). – Martin York Mar 04 '13 at 02:20