954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Singleton class Worked example

Hi This is Singleton class program, please let me know any changes required in this code...............

#include
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;
}

madankumar
Newbie Poster
13 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

Please ask specific questions -- doesn't it compile? If not, why? Doesn't it work right? No, then what is wrong with it.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

neoguru.....welcome aboard.I reccommend taking a look at the Rules & FAQ for the daniweb forum. Please do not re-open old threads....i'm pretty sure he'd get the answer before an entire year....look at the posting date of the question before you reply. Again ,welcome.

zandiago
Nearly a Posting Maven
2,480 posts since Jun 2007
Reputation Points: 129
Solved Threads: 26
 
neoguru.....welcome aboard.I reccommend taking a look at the Rules & FAQ for the daniweb forum. Please do not re-open old threads....i'm pretty sure he'd get the answer before an entire year....look at the posting date of the question before you reply. Again ,welcome.

Ok, sorry, chief

neoguru
Newbie Poster
1 post since Oct 2007
Reputation Points: 8
Solved Threads: 0
 

The above example compiles pretty fine. I have a query. The two lines below
bool Singleton::instanceFlag = false;
Singleton* Singleton::single = NULL

Should have come inside the constructor of this class ryt?.

anoopkris
Newbie Poster
1 post since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

>>Should have come inside the constructor of this class ryt?.

No. static objects must also be declared globally just like any other global object. Those lines were stated correctly in the original thread of 3 years ago.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

I think there is better way to implement this :-) cool and safe

#include <iostream>
using namespace std;

CSingleton.h
==========

#ifndef __CSINGLETON__
#define __CSINGLETON__

class CSingleton {
public:
    static CSingleton* getInstance();
    void destroyInstance();
    void doSomething();

private:
    CSingleton() {}
    ~CSingleton(){}
    static CSingleton* m_pInstance;
};
#endif

CSingleton.cpp
============

#include "CSingleton.h"

CSingleton* CSingleton::m_pInstance = NULL;

CSingleton* CSingleton::getInstance() {
     if(NULL == m_pInstance ) {
            m_pInstance = new CSingleton();
     }
     return m_pInstance;
}

void CSingleton::destroyInstance() {
     delete m_pInstance;
     m_pInstance = NULL;
}

void CSingleton::doSomething() {
     cout<<"Am one and only one" << endl;
}

main.cpp
=======

#include "CSingleton.h"

int main()
{
    CSingleton* inst = CSingleton::getInstance();
    inst->doSomething();
    inst->destroyInstance();
}
Hi This is Singleton class program, please let me know any changes required in this code............... #include 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; }
flower23445
Newbie Poster
1 post since Dec 2009
Reputation Points: 10
Solved Threads: 0
 

I have a doubt, does both sc1and sc2 refers same instance or not

sc1 = Singleton::getInstance();
sc2 = Singleton::getInstance();

prakash02
Newbie Poster
3 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

Ya it will be...
You could have been easily confirmed it by writing a simple sample application.

Thanks,
LJ

MyNameIsLJ
Newbie Poster
1 post since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You