C++ Singleton Example

Download here.  singleton-design-pattern

Purpose of a singleton:
To have a class which has only one instance(object)
All the objects of this class refer to same global object
that is created only once
This design pattern provides a mechanism for providing
namespaces to global variables

C++ example for singleton design pattern

// Filename  : singleton.cpp
// Author    : A.G.Raja
// License   : GPL
// Website   : https://agraja.wordpress.com

#include <iostream>

using namespace std;

class Singleton {
// constructor that is called only once
Singleton();
// object that is created only once
static Singleton *single;
public:
// method to get object
Singleton* get_instance();
// data to check the functionality
int data;
};

Singleton* Singleton::single = NULL;

Singleton* Singleton::get_instance() {
// call the constructor for the first time
// if the object has been created already
// return reference to the object already created
if(single==NULL) single = new Singleton();
return single;
}

Singleton::Singleton() {
data = 14;
}

int main() {
Singleton *s, *t ;
s = s->get_instance();
cout<<“initial value of s->data  = “<<s->data<<endl;
s->data = 25;
cout<<“modified value of s->data = “<<s->data<<endl;
t = t->get_instance();
cout<<“value of t->data          = “<<t->data<<endl;
}
// end of file singleton.cpp

Result:
initial value of s->data  = 14
modified value of s->data = 25
value of t->data          = 25

Download here.  singleton-design-pattern

Advertisements

5 thoughts on “C++ Singleton Example

  1. Pingback: System Verilog Singleton Example « Applied Electronics Journal

  2. I think this code will give reset.
    In main
    Singleton *s;
    s->get_instance();

    in this line ” s” doesnot have any address, but your are calling it to get_instance() using null pointer,
    this code will not work.

    Thanks,
    Kiran

  3. #include

    using namespace std;

    class Singleton {
    // constructor that is called only once
    Singleton();
    // object that is created only once
    static Singleton *single;
    public:
    // method to get object
    Singleton* get_instance();
    // data to check the functionality
    int data;
    };

    Singleton* Singleton::single = NULL;

    Singleton* Singleton::get_instance() {
    // call the constructor for the first time
    // if the object has been created already
    // return reference to the object already created
    if(single==NULL) single = new Singleton();
    return single;
    }

    Singleton::Singleton()
    {
    data = 14;
    }
    Singleton *s, *t ;
    int main()
    {

    s = s->get_instance();
    cout<data = “<data<get_instance();
    cout<data = “<data<data = 25;
    cout<data = “<data<get_instance();
    cout<data = “<data<<endl;

    cin.ignore();
    getchar();
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s