Re: singleton Programming Software Development by hemanthjava Hi ithelp, Your absolutely right. Also adding to it [URL="http://beginner-java-tutorial.com/singleton.html"]Java Singleton pattern[/URL] singleton Programming Software Development by mengani123 write a sample code to create singleton class using java. ensure that only one instance will be creted and same will be returned always. please do it. sanjay Re: singleton Programming Software Development by peter_budo [QUOTE=mengani123;319339]write a sample code to create singleton class using java. ensure that only one instance will be … singleton pattern and its destructor... Programming Software Development by unclepauly … .cpp files: [CODE] // Singleton.h class Singleton { public: static Singleton GetSingleton(); void DoSomething(); ~Singleton(); private: Singleton(); static Singleton* _instance; }; [/CODE] [CODE] //Singleton.cpp Singleton* Singleton::_instance = 0;// initialize pointer… Re: singleton pattern and its destructor... Programming Software Development by unclepauly …this was the output: Singleton:: GetSingleton Singleton:: Singleton Singleton:: Singleton[ctr] Singleton:: DoSomething Singleton:: ~Singleton Singleton:: GetSingleton Singleton:: Singleton[ctr] Singleton:: DoSomething Singleton:: ~Singleton so the actual main …seem to create a copy of the singleton via the copy constructor, then this … singleton class with example....... Programming Software Development by madankumar … call those functions and instantiate it Singleton(); Singleton(const Singleton&); Singleton& operator= (const Singleton&); private: // A member …created is nothind but the first instance static Singleton* pinstance; }; Singleton* Singleton:instance = 0;// initialize pointer Singleton* Singleton::Instance () { if (pinstance 0) … Re: singleton pattern and its destructor... Programming Software Development by jencas [QUOTE=unclepauly;518666] [CODE] Singleton Singleton::GetSingleton() { std::cout << "Singleton::GetSingleton" << std::endl; if(_instance ==… NULL) { _instance = new Singleton(); } return *_instance; } [/CODE] [/QUOTE] [code] Singleton Singleton::GetSingleton() [/code] GetSingleton() returns an object, … Re: singleton class with example....... Programming Software Development by gurucode … call those functions and instantiate it Singleton(); Singleton(const Singleton&); Singleton& operator= (const Singleton&); private: // A member … is nothind but the first instance static Singleton* pinstance; }; Singleton* Singleton::pinstance(0); // initialize pointerS Singleton* Singleton::Instance () { if (pinstance==0) … Singleton class - Really confused Programming Software Development by phorce …;iostream> using namespace std; bool Singleton::instanceFlag = false; Singleton* Singleton::single = NULL; Singleton* Singleton::getInstance() { if(!instanceFlag) { single = new Singleton(); instanceFlag = true; return single; }else… Singleton class Worked example Programming Software Development by madankumar 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 Re: singleton class with example....... Programming Software Development by madankumar 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 Re: singleton pattern and its destructor... Programming Software Development by Duoas You have a very non-obvious error: the default copy constructor is in use. Make sure that GetSingleton() returns a [I]pointer[/I] to type Singleton. Hope this helps. Re: singleton pattern and its destructor... Programming Software Development by software.lease … will be clear to you. In main you did (1) Singleton::GetSingleton().DoSomething(); So first the control will goto GetSingleton, as… Re: Singleton class Worked example Programming Software Development by flower23445 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 Re: Singleton class - Really confused Programming Software Development by mike_2000_17 … as follows: // in header: class Singleton { private: Singleton() { // ... } Singleton(const Singleton&); // no implementation. non-copyable Singleton& operator=(const Singleton&); // no implementation. non-copyable… Singleton class issue Programming Software Development by nitin1 …class Singleton { static Singleton * s; Singleton() { } public: ~Singleton() { s = NULL; } static Singleton * getInstance(); void print() { cout<<this<<endl; } }; Singleton* Singleton::s=NULL; Singleton* Singleton Re: Singleton class - Really confused Programming Software Development by VernonDozier …the boolean flag. You also have a problem here. Animal *Singleton::method(string something) { if(something == "cat")… them. Presumably the whole point of the Singleton was to make memory management easy and make… type. Otherwise, why bother with polymorphism?). The Singleton is to ensure that only one animal exists. … Re: Singleton class - Really confused Programming Software Development by vijayan121 …lt;stdexcept> namespace { std::atomic<Animal*> singleton(nullptr) ; std::mutex guard ; } Animal::Animal() //…constructor { if(singleton) throw std::logic_error( "Animal is a singleton" ) ; else singleton = this ; } Animal::~Animal() { singleton = nullptr ; … Singleton Template class magic Programming Software Development by jakesee …<typename T> class Singleton { private: Singleton(const Singleton<T>&){}; Singleton& operator=(const Singleton<T>&){}; protected:… static T* _mInstance; static int i; // for debugging purposes Singleton() { i++; // for debugging purposes _mInstance = this; // this function … Re: Singleton class - Really confused Programming Software Development by phorce … << "MERRR"; } }; class Singleton { public: Animal *newAnimal(string theTypeOfAnimal); static Singleton *instance() { static Singleton instance; return &instance; } private: static… Re: Singleton class - Really confused Programming Software Development by VernonDozier … program in main. You have only one call to Singleton::instance()->newAnimal("Horse"); so that can…the line above (from main) means that the Singleton is incorrect. A Singleton class should not have a public method called &…"new Horse()". The whole point of a Singleton class is that you don't have the responsibility (… Singleton problem Programming Software Development by abhi_elementx …lt;"\nmyFunction() called.."; } }; Singleton* Singleton :: getInstance(){ static Singleton* obj = NULL; if(obj == NULL){… return obj; } int main(){ Singleton* myobj = Singleton :: getInstance(); myobj->myFunction(); Singleton* myobj2 = Singleton :: getInstance(); myobj2->myFunction(); … Re: Singleton class - Really confused Programming Software Development by L7Sqr … the implementation of the factory and less on the singleton. Nothin prevents you from making the `AnimalFactory` a… singleton but the `Create` method is static so it really … however. Are you looking to have the `AnimalFactory` as a singleton or the *classes it creates* be singletons? For instance, when… Re: Singleton problem Programming Software Development by StuXYZ … if you are going to use a singleton you want to be 100% certain that…you want this. class Singleton { private: Singleton(); Singleton& opertaor=(const Singleton); Singleton(const Singleton); public: ~Singleton(); Singleton* Instance(); }; Note … Re: Singleton Template class magic Programming Software Development by jakesee … <typename T> class Singleton { private: Singleton(const Singleton<T>&){}; Singleton& operator=(const Singleton<T>&){}; protected:… static T* _mInstance; Singleton() { This is crazy… Re: Singleton class - Really confused Programming Software Development by L7Sqr … you are looking for. The question is independent of the singleton class (which could still be used). You could transition to… Re: Singleton class - Really confused Programming Software Development by phorce **L7Sqr** I like this implementation actually :)! Would this allow me to create a Singleton though? For example, it looks like I can create multiple objects so I need a way that only ONE object can be created? Re: Singleton class Worked example Programming Software Development by anoopkris 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?. Re: Singleton class Worked example Programming Software Development by prakash02 I have a doubt, does both sc1and sc2 refers same instance or not sc1 = Singleton::getInstance(); sc2 = Singleton::getInstance(); Re: Singleton problem Programming Software Development by MrSpigot It's only executed once, so that class would work as a singleton. An alternative technique which avoids the need to clean up the memory allocation is: [code=cpp] Singleton& Singleton :: getInstance() { static Singleton obj; return obj; } [/code] Pros and cons to each method.