Sphere: Related Content
#include <iostream>
using namespace std;
class CSingleton {
public:
static CSingleton &GetInstance();
void showMesg()
{
cout << "Hi This is Sujit Das" << endl;
}
#if 1
protected:
// need default ctor for GetInstance.
// ctor is protected, not private in case you want to derive.
CSingleton() { }
//CSingleton(const CSingleton& o) { }
CSingleton& operator=(const CSingleton& o) {cout << "Assignment operator" << endl; }
#endif
#if 0
private:
CSingleton() { }
CSingleton(const CSingleton& o) { }
//CSingleton& operator=(const CSingleton& o) { }
#endif
};
CSingleton& CSingleton::GetInstance() {
static CSingleton theInstance; // one-and-only instance
return theInstance;
}
main()
{
// These lines will not compile:
CSingleton x = CSingleton::GetInstance(); // error: private copy ctor!
x.showMesg();
CSingleton y = CSingleton::GetInstance(); // error: private copy ctor!
y.showMesg(); // error: private assignment!
CSingleton z = x;
z.showMesg();
}
National Heritage Board is holding a pop-up museum till Dec 14 featuring
everyday objects contributed by Singaporeans
-
Called Museum of U & Me, the pop-up tells the history of Singapore through
everyday objects owned by Singaporeans, such as measurements of Princess
Diana t...
12 minutes ago
No comments:
Post a Comment