Sphere: Related Content
#include <iostream>
using namespace std;
class Singleton
{
public:
static Singleton* Instance();
int printMessage() {cout << "hi......\n";}
protected:
Singleton();
Singleton(const Singleton&) {cout << "calling copy constructor\n";}
Singleton& operator= (const Singleton&);
private:
static Singleton* pinstance;
};
Singleton* Singleton::pinstance = 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 << "Calling Protected Constructor" << endl;
}
int main()
{
Singleton *p1 = Singleton::Instance();
cout << "printing p1 --->" << p1 << endl;
p1->printMessage();
Singleton *p2 = p1->Instance();
cout << "printing p2 --->"<< p2 << endl;
p2->printMessage();
Singleton *p3(p2);
cout << "printing p3 --->" << p3 << endl;
p3->printMessage();
Singleton & ref = * Singleton::Instance();
return 0;
}
Top Iranian Official Visits Lebanon as Hezbollah Bucks Calls to Disarm
-
The visit by Ali Larijani, the head of Iran’s top security body, came as
the Lebanese government moves to disarm Hezbollah, the militant group that
has lon...
56 minutes ago
No comments:
Post a Comment