#include #include using namespace std; class Singleton { public: static Singleton & Instance() { static Singleton instance; return instance; } void Print() { printCalls ++; cout << "This is the " << printCalls << " time Print has been called" << endl; } int GetVar() const { return avar; } void SetVar(int i) { avar = i; } private: int cCalls {0}; int printCalls{0}; int avar{-99999}; Singleton() { cCalls ++; cout << "The singleton constructor was called " << endl; cout << "This has happened a total of " << cCalls << " times " << endl; } ~Singleton() { cout << "The singleton distructor was called " << endl; } }; void DoSomething(); int main() { Singleton & theSingleton {Singleton::Instance()}; cout << endl; theSingleton.Print(); theSingleton.SetVar(100000); DoSomething(); theSingleton.Print(); cout << "The var is now " << theSingleton.GetVar() << endl; cout << endl; return 0; } void DoSomething() { Singleton & aSingleton {Singleton::Instance()}; cout << endl << "In Do Something " << endl; aSingleton.Print(); cout << "The var is " << aSingleton.GetVar() << endl; aSingleton.SetVar(42); aSingleton.Print(); cout << "Out of Do Something " << endl << endl; return; }