home *** CD-ROM | disk | FTP | other *** search
- #include <iostream.h>
-
- // Define the Boolean type
- typedef unsigned char Boolean;
- const Boolean TRUE = 1;
- const Boolean FALSE = 0;
-
- // GlobalValue template definition
- template <class T>
- class GlobalValue {
- public:
- // Constructors and destructor
- GlobalValue();
- GlobalValue(T* pNewGlobalValue);
- ~GlobalValue();
-
- // Public member functions
- static T* GetGlobalValuePtr();
-
- private:
- static T* pGlobalValue;
- static Boolean ShouldDelete;
- };
-
- // Initialize static data
- template <class T>
- T* GlobalValue<T>::pGlobalValue = NULL;
- template <class T>
- Boolean GlobalValue<T>::ShouldDelete = FALSE;
-
- // Constructors
- template <class T>
- GlobalValue<T>::GlobalValue()
- {
- pGlobalValue = new T;
- ShouldDelete = TRUE;
- }
-
- template <class T>
- GlobalValue<T>::GlobalValue(T* pNewGlobalValue)
- {
- pGlobalValue = pNewGlobalValue;
- ShouldDelete = FALSE;
- }
-
- // Destructor
- template <class T>
- GlobalValue<T>::~GlobalValue()
- {
- if (ShouldDelete && pGlobalValue)
- delete pGlobalValue;
- }
-
- // Public member functions
- template <class T>
- T* GlobalValue<T>::GetGlobalValuePtr()
- {
- return pGlobalValue;
- }
-
-
- // Now demonstrate use of a GlobalValue
- void AFarAwayFunc()
- {
- cout << "The int GlobalValue is ";
- cout << *GlobalValue<int>::GetGlobalValuePtr() << endl;
- }
-
- void main()
- {
- int AVeryImportantInt = 123;
- GlobalValue<int> GlobalInt(&AVeryImportantInt);
-
- // Show the current value
- AFarAwayFunc();
-
- // Change the value
- AVeryImportantInt++;
- AFarAwayFunc();
- }
-