1

For example I have a following class:

class singelton
{
public:
    static singelton* Instance()
    {
        if (m_pInstance == 0)
        {
            m_pInstance = new singelton();
        }
        return m_pInstance;
    }

    void setData(std::string input) { data = input; }
    void getData() const { std::cout << data << std::endl; }
private:
    singelton() {}
    ~singelton() {}

    static singelton* m_pInstance;

    std::string data;
};

typedef singelton s;

//what is this? Why need a singleton name? I mean "singelton*".
singelton* singelton::m_pInstance = 0;

int main(int argc, char** argv)
{
    s.Instance()->setData("Something...");
    s.Instance()->getData();
    return 0;
}

What is singelton* singelton::m_pInstance = 0;? This function assigns zero/null to a singleton instance, but why need use singleton* ? That assignment like a function, but use as assignment.

Ivan
  • 882
  • 9
  • 20

3 Answers3

5

Static data members are not part of objects of a given class type; they are separate objects. As a result, the declaration of a static data member is not considered a definition. So static member must be defined outside of the class declaration.

In your example:

  • singelton * is a type of member.
  • singleton:: is class name (like namespace)
  • and m_pInstance is member name

P.S.: Because of static variables are initialised with 0 by default in C++, you don't need to explicitly set m_pInstance to 0 (or NULL). The definition only will be enough:

singelton * singelton::m_pInstance;
fasked
  • 3,455
  • 1
  • 17
  • 35
2

What is singelton* singelton::m_pInstance = 0;?

It's the initialiser for the static member variable m_pInstance, statically initialising the pointer to be null. singelton* (pointer to singelton) is the type of the variable; singlelton::m_pInstance is the (qualified) name of the variable; and = 0 is the initialiser.

This function assigns zero/null to a singleton instance, but why need use singleton* ?

No, it initialises the pointer to null; it doesn't point to anything yet. The object itself will be created, and the pointer updated to point to it, the first time someone calls Instance(). It's a pointer so that the object itself can be created when it's first needed, rather than at some arbitrary point during program startup - this is known as "lazy initialisation".

Beware that, in C++, there is no way to implement the Singleton anti-pattern correctly. This particular implementation has the problems of leaking the object, and not being thread-safe. I strongly recommend that you get rid of it: just instantiate the object in an appropriate place, with a lifetime that's longer than whatever uses it, and pass references to whatever needs it.

Mike Seymour
  • 235,407
  • 25
  • 414
  • 617
  • Could you please comment a bit more on why you consider singleton an _anti-pattern_? – Massimiliano Dec 30 '13 at 13:08
  • 1
    @Massimiliano: That's beyond the scope of the question, and is well covered [here](http://stackoverflow.com/questions/137975/), although that only discusses the conceptual problems of the anti-pattern in general, not the impossibility of implementing it correctly in C++. – Mike Seymour Dec 30 '13 at 13:09
1

Use

Singleton::Instance()->setData("Hello");

and

Singleton::Instance()->getData();

The class can only have one instance - hence called singleton

And the Singleton::Instance gives you access to that

singelton* singelton::m_pInstance = 0;

Initializes it and when you first use it the singleton is created

Ed Heal
  • 55,822
  • 16
  • 77
  • 115