0

Possible Duplicate:
What is so bad about singletons?
Singleton pattern in C++

I want to create a singleton class. For that, I created a class with all its member and methods as static. Something like this.

class a
{
    static int a;
    static GetA();
}

Now all the classes who want to use my singleton class cannot create any object for my class and also will get same value. I just want to know whether this implementation will solve all the purpose and fulfill all criteria for creating a singleton class.

Community
  • 1
  • 1
Apoorva sahay
  • 1,640
  • 3
  • 23
  • 43
  • 1
    That is not an implementation. It says nothing about it. – Nawaz Jul 06 '12 at 12:24
  • 1
    As written, anyone can create it (since the constructor is public), but no-one can access the static instance (since it, and the accessor, are both private). That's the opposite to what you want. – Mike Seymour Jul 06 '12 at 12:25
  • @Apoorvasahay: No; but if everything is static then there's no need for a constructor anyway (except to make it private, if you want it more singletonny). Also, there's no need for it to be a class at all. – Mike Seymour Jul 06 '12 at 12:44
  • But Compilers provide a default CTOR my themselve, if do not provide one. correct if I am wrong. – Apoorva sahay Jul 06 '12 at 12:47
  • @Apoorvasahay: Yes, that's right. It's public, and in the conventional singleton pattern you want it to be private. However, your class sounds more like a collection of static variables than a singleton, in which case it shouldn't be a class at all. – Mike Seymour Jul 06 '12 at 13:08

2 Answers2

3

I prefer:

GlobalObjectMgr& GlobalObjectMgr::instance()
{
    static GlobalObjectMgr objMgr;
    return objMgr;
}

There is no class member variable required and it is created only when needed.

Dennis
  • 3,538
  • 18
  • 41
2

The conventional Singleton (anti-)pattern isn't a collection of static variables; rather, it is an object with non-static members, of which only one instance can exist.

In C++, this allows you to avoid the biggest problem with static variables: the "initialisation order fiasco". Because the initialisation order is unspecified for static variables in different translation units, there's a danger that the constructor of one might try to access another before it is initialised, giving undefined behaviour. However, it introduces other problems (a similar "destruction order fiasco", and thread safety issues in older compilers), so it's still something to avoid.

If you want a collection of static variables and functions, then put them in a namespace rather than a class:

namespace stuff {
    int a;
    void do_something();
}

If you think you want a singleton, then think again; you're generally better avoiding globally accessible objects altogether. If you still want one, then you would make a class with a private constructor, and a public accessor that returns a reference to the single instance, along the lines of:

class singleton {
public:
    singleton & get() {
        static singleton instance;
        return instance;
    }

    int a;
    void do_something();

private:
    singleton() {}
    ~singleton() {}
    singleton(singleton const &) = delete;
};
Mike Seymour
  • 235,407
  • 25
  • 414
  • 617