Function pointer in singleton

Hi Tech Gurus,

I am trying to understand singleton code. Below I have copied from net my doubt is in below code why do we need static Singleton *single; the pointer to an object? What does it do?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
 using namespace std;

class Singleton
{
private:
    static bool instanceFlag;
    static Singleton *single;
    Singleton()
    {
        //private constructor
    }
public:
    static Singleton* getInstance();
    void method();
    ~Singleton()
    {
        instanceFlag = false;
    }
};

bool Singleton::instanceFlag = false;
Singleton* Singleton::single = NULL;
Singleton* Singleton::getInstance()
{
    if(! instanceFlag)
    {
        single = new Singleton();
        instanceFlag = true;
        return single;
    }
    else
    {
        return single;
    }
}

void Singleton::method()
{
    cout << "Method of the singleton class" << endl;
}

int main()
{
    Singleton *sc1,*sc2;
    sc1 = Singleton::getInstance();
    sc1->method();
    sc2 = Singleton::getInstance();
    sc2->method();

    return 0;
}
You need line 22, because you need to instantiate the pointer.

Because static members (instanceFlag,single) have only a single occurrance across all instances of the class, you must initialize them outside of the initialization of any class instance.

BTW, instanceFlag is really not needed. At line 25, you can just check if single is a nullptr or not.
yepMe wrote:
I am trying to understand singleton code.

The best way to understand singleton code is to understand why they should be used sparingly, if at all. :-)

Edit: Reading that back I sound like a bit of a douche. Wasn't trying to be; genuinely think that it's important to understand why they are often a pretty bad idea. People tend to develop an over-reliance on this pattern without seeing the damage it can do.
Last edited on
Thanks AbstractionAnon,
for the reply but my doubt is line no 7 , why we need that? we can achieve singleton without that also.
we can achieve singleton without that also

No, you need line 7. single IS the pointer to the single instance of Singleton.

This can get a little confusing. The Singleton class is just normal class. What gives it its singleton properties is the static member pointer (single) and the fact that getInstance() always returns the same value (the "real" instance created at line 27).

Note that lines 45 and 47 both call getInstance(). The first time getInstance() is called, it allocates a new instance of Singleton and assigns it to the static pointer single. Any subsequent calls to getInstance() will not allocate a new instance and will return the existing static value of single. Since the member pointer single is static, there is only one instance across all instances of Singleton.

Since Singleton's only constructor is private, it is not possible to construct an instance of Singleton directly. If you want to access something in the Singleton class, you must get a pointer to it via a call to getInstance() (which returns the pointer at line 7).

Keep in mind that this is a very simplistic example. This Singleton class doesn't have anything in it besides the logic to create / return a pointer to a single instance of the class. In practice, you would add attributes and/or functions to Singleton, or to a class derived from it (preferred).

To reiterate what iHutch105 said, you need to think carefully about why you need a singleton class. They are often misused. Perhaps all you need is a static member of an existing class.
Last edited on
Topic archived. No new replies allowed.