| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- #include <iostream>
- #include <string>
- #include <stdlib.h>
- using namespace std;
- class Number
- {
- public:
- // 2. Define a public static accessor func
- static Number* instance();
- static void setType(string t)
- {
- type = t;
- delete inst;
- inst = 0;
- }
- virtual void setValue(int in)
- {
- value = in;
- }
- virtual int getValue()
- {
- return value;
- }
- protected:
- int value;
- // 4. Define all ctors to be protected
- Number()
- {
- cout << ":ctor: ";
- }
- // 1. Define a private static attribute
- private:
- static string type;
- static Number* inst;
- };
- class Octal : public Number
- {
- // 6. Inheritance can be supported
- public:
- friend class Number;
- void setValue(int in)
- {
- char buf[10];
- sprintf_s(buf, "%o", in);
- sscanf_s(buf, "%d", &value);
- }
- protected:
- Octal() {}
- };
- Number* Number::instance()
- {
- if (!inst)
- // 3. Do "lazy initialization" in the accessor function
- if (type == "octal")
- inst = new Octal();
- else
- inst = new Number();
- return inst;
- }
- string Number::type = "decimal";
- Number* Number::inst = 0;
- int main()
- {
- // Number myInstance; - error: cannot access protected constructor
- // 5. Clients may only use the accessor function to manipulate the Singleton
- Number::setType("octal");
- Number::instance()->setValue(42);
- cout << "value is " << Number::instance()->getValue() << endl;
- Number::setType("decimal");
- Number::instance()->setValue(64);
- cout << "value is " << Number::instance()->getValue() << endl;
- Number::setType("decimal");
- Number::instance()->setValue(42);
- cout << "value is " << Number::instance()->getValue() << endl;
- Number::setType("octal");
- Number::instance()->setValue(64);
- cout << "value is " << Number::instance()->getValue() << endl;
- }
|