simple singleton class

Singleton is one of the common design pattern used to make a class object exist only one instant. I have read some books illustrate the implementation of singleton, I found it all complicated. I have tried to come out a very simple example on singleton class, bellow is my outcome.

#include<stdio.h>
class singleton
{
    public:
    void print()
    {
        printf("hello\n");
    }
    int a;
}singleton;

int main()
{
    //singleton s;  #cannot define a new instant like this anymore.
    singleton.a=5;
    printf("a=%d\n",singleton.a);
    singleton.print();

    return 0;
}

With the class name and the object name appear the same, you make that class object singleton. Which means you can’t instantiate a new object using the name singleton anymore. The way to access the function and variables of a singleton object is direct.

This is my initial idea of how to make singleton simple.

12 thoughts on “simple singleton class

  1. I’m pretty sure this will fail once you start working with projects that try to use this “singleton” in more than one CPP file. If you put the class in a header, and compile two CPPs that #include it, when you link them together you’ll have an object that is defined twice. Just a heads-up… Good idea though, and perfect for small jobs that need singletons!

  2. Hi All,

    the code looks to be simple and working.

    but have a doubt in the example.

    i have written a class as for singleton as mentioned above in a new cpp file

    from my main program i want to set the values(main.cpp) from the other cpp i want to access the values

    how can i work out on this?

    to be clear

    singleton.cpp

    class singleton
    {
    public:
    int a;
    char * strDSN;

    }singleton;

    main.cpp

    #include “stdafx.h”
    #include “singleton.cpp”

    int _tmain(int argc, _TCHAR* argv[])
    {
    singletonclass.a = 5;
    singletonclass.strDSN=”Test”;
    return 0;
    }

    i have some other cpp where i need to get the value

    config.cpp
    class config
    {
    // I need to read the value here

    }

    Thanks

    Srinivas

  3. Pingback: STL Singleton Template :: c/c++ linux programming by examples

  4. well i think u had miskewed the concept of singleton .singleton is which gives the global point of access and avoiding the client accessing of copy constructors well which means u know the singleton definition.

  5. Singletons are evil. It might be okay (and useful) for a Logger-class but apart from that I would highly avoid it. Few programmers use global variables in their programs since they make it very hard to keep the code modular, testable and thread safe. A singleton is just a storage for global variables with some code attached to it.

    If you still want to use them I would rewrite your code as

    #include

    int a = 0;

    void print()
    {
    printf(“hello\n”);
    }

    int main()
    {
    a = 5;

    printf(“a=%d\n”, a);
    print();
    return 0;
    }

    since that is what it is 😉

  6. well first of all i disagree, if you want to define a singleton why not define it in header and don’t forget to wrap it in defines example :

    #ifndef SINGLETON_H
    #define SINGLETON_H

    class singleton {};
    singleton;

    #endif

    that solves your problems with instances…

  7. Well its a good example of singelton..but when you are working on large project and there are multiple clients who want access to a singelton class then in that case, it will be a problem..this thing you explained simply..gr88. Now one question if you have singelton instance which is a pointer, can you tell me how we can delete singelton object.

Leave a Reply

Your email address will not be published. Required fields are marked *