#include<iostream>

using namespace std;

class Singleton {

static Singleton * s;
    Singleton() {

    }

public:

    ~Singleton() {
        s = NULL;
    }
    static Singleton * getInstance();
    void print() {
        cout<<this<<endl;
    }
};

Singleton* Singleton::s=NULL;
Singleton* Singleton::getInstance() {

        if(s == NULL) {
            s = new Singleton();
        }
        return s;
}


int main()
{
    Singleton * s = Singleton::getInstance();
    s->print();
    Singleton * s2 = Singleton::getInstance();
    s2->print();
    return 0;
}

Singleton* Singleton::s=NULL;
(line 23)

When I am not putting this line, it is saying that undefined reference to 's'. By default static members are NULL only. then why does it need me to put this line? Please explain. Thanks a lot in advance.

Recommended Answers

All 8 Replies

It's not about having to set s to NULL. It's because when you have a static member of a class, you have to specifically create that static variable somewhere. On line 23, you create the variable s that will be shared by all instance of the class. If you don't create it anywhere, you will get undefined reference errors because you're trying to use s but it never got created.

Thanks Moschops. But I am declaring it in the class right? Why do I need to create it outside? Please elaborate more. http://www.tutorialspoint.com/cplusplus/cpp_static_members.htm
Here, they are saying that by dafault, it will be zero if not other value in present. Please check.

So, like that all Static members have to be initialized in this way? Are you telling this thing?

A static member variable is still just a static (global) variable, that belongs to a class. All static variables have to be declared and initialized somewhere in a translation unit - them's the rules!

But I am declaring it in the class right?

Yes, but a declaration is not necessarily a definition. In the case of non-const static data members, the declaration is unconditionally not a definition. Hence why you need to provide a definition outside of the class.

Here, they are saying that by dafault, it will be zero if not other value in present.

True, but the object itself must exist. These are two different things.

@rubberman Ya that's fine. This is a rule I got it. But, don't we have reason for this? I mean global variables by default set to zero or NULL. Am I right ? Please correct me.

Not as far as I know. You can declare it, but it needs to be implemented in some translation unit. It may be that one of the newer C++ versions (C++11 or C++14) may allow that sort of lazy coding, but it is, in my opinion (and as you discovered) a dangerous practice, and not portable to a lot of compilers.

@James Then, what about Static functions? How will you explain that thing? We can give definition inside? Please correct me.

what about Static functions?

What about them? The only similarity is the static keyword.

How will you explain that thing?

An arbitrary decision by the language designers. Sometimes the answer to "why" is there's no good reason, that's just how things evolved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.