Advertisement
karlicoss

U MAD, JAVA???

Aug 15th, 2011
546
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.02 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdexcept>
  3.  
  4. template<typename T>
  5. class final
  6. {
  7. private:
  8.     T data;
  9.     bool used;
  10.  
  11. public:
  12.     final(): used(false)
  13.     {
  14.        
  15.     }
  16.     final(const T &a)
  17.     {
  18.         used = true;
  19.         data = a;
  20.     }
  21.     final(const final<T> &a)
  22.     {
  23.         used = true;
  24.         data = a.get();
  25.     }
  26.     const T & operator=(const T &a)
  27.     {
  28.         if (used)
  29.             throw std::runtime_error("Hey! I'm final!");
  30.         else
  31.         {
  32.             used = true;
  33.             data = a;
  34.         }
  35.         return data;
  36.     }
  37.     const final<T> & operator=(const final<T> &a)
  38.     {
  39.         if (used)
  40.             throw std::runtime_error("Hey! I'm final!");
  41.         else
  42.         {
  43.             used = true;
  44.             data = a.get();
  45.         }
  46.         return *this;
  47.     }
  48.     const T & operator()() const
  49.     {
  50.         return data;
  51.     }
  52. };
  53.  
  54.  
  55. int main()
  56. {
  57.     final<int> a;
  58.     a = 5;
  59.     std::cout << a();
  60.     a = 7;
  61.     std::cout << a();
  62.     return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement