#include <iostream>
using namespace std ;
class Singleton
{
public:
// Implement the logic here to instantiate the class for the first time by validating the
// member pointer. If member pointer is already pointing to some valid memory it means
// that the first object is created and it should not allow for the next instantiation
// so simply return the member pointer without calling its ctor.
static Singleton* Instance();
protected:
// Default ctor, copy-ctor and assignment operators should be as private so that nobody
// from outside can call those functions and instantiate it
Singleton();
Singleton(const Singleton&);
Singleton& operator= (const Singleton&);
private:
// A member pointer to itself which point to the firstly created object and this should be
// returned if another object is instantiated that means the second object what you
// thought of created is nothind but the first instance
static Singleton* pinstance;
};
Singleton* Singleton:instance = 0;// initialize pointer
Singleton* Singleton::Instance ()
{
if (pinstance 0) // is it the first call?
{
pinstance = new Singleton; // create sole instance
}
return pinstance; // address of sole instance
}
Singleton::Singleton()
{
//... perform necessary instance initializations
cout << "Hellow\n" ;
}
int main( )
{
Singleton *p1 = Singleton::Instance();
Singleton *p2 = p1->Instance();
Singleton & ref = * Singleton::Instance();
return 0 ;
}
madankumar 0 Newbie Poster
WolfPack 491 Posting Virtuoso Team Colleague
madankumar 0 Newbie Poster
~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster
gurucode 0 Newbie Poster
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.