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

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

Recommended Answers

All 8 Replies

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

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.

commented: Yes, tell it like it is to that website spammer. +11

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

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?.

>>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.

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 <iostream>
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;
}

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

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

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

Thanks,
LJ

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.