fork(1) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. template <class T>
  5. class Unko
  6. {
  7. private:
  8. T* pointer;
  9. public:
  10. explicit Unko(T* t) {
  11. pointer = t;
  12. cout << "Eat! (" << pointer << ')' << endl;
  13. }
  14. ~Unko() {
  15. delete pointer;
  16. cout << "Poo! (" << pointer << ')' << endl;
  17. }
  18. T& operator * () const { return *pointer; }
  19. T* operator -> () const { return pointer; }
  20.  
  21. void swap(Unko& unko) {
  22. T *tmp;
  23. tmp = pointer;
  24. pointer = unko.pointer;
  25. unko.pointer = tmp;
  26. }
  27. explicit operator bool () const { return pointer != NULL; }
  28. Unko(const Unko& unko) = delete;
  29. Unko& operator=(const Unko& unko) = delete;
  30. };
  31.  
  32. template <class T>
  33. class Unko<T[]>
  34. {
  35. private:
  36. T* pointer;
  37. public:
  38. explicit Unko(T* t) {
  39. pointer = t;
  40. cout << "Eat! (" << pointer << ')' << endl;
  41. }
  42. ~Unko() {
  43. delete [] pointer;
  44. cout << "Poo! (" << pointer << ')' << endl;
  45. }
  46. T& operator [] (int n) const { return *(pointer + n); }
  47. void swap(Unko& unko) {
  48. T *tmp;
  49. tmp = pointer;
  50. pointer = unko.pointer;
  51. unko.pointer = tmp;
  52. }
  53. explicit operator bool () const { return pointer != NULL; }
  54. Unko(const Unko& unko) = delete;
  55. Unko& operator=(const Unko& unko) = delete;
  56. };
  57.  
  58. class Value
  59. {
  60. private:
  61. int value;
  62. public:
  63. Value(int v) : value(v) {}
  64. ~Value() { cout << "Oh! God!" << endl; }
  65. int getValue() { return value; }
  66. void setValue(int v) { value = v; }
  67. };
  68.  
  69. int main() {
  70. cout << "start" << endl;
  71. {
  72. Unko<int> unko(new int);
  73. Unko<int> poop(new int);
  74. *unko = 100;
  75. *poop = 200;
  76. unko.swap(poop);
  77. cout << "unko: " << *unko << endl;
  78. }
  79. {
  80. Unko<Value> val(new Value(777));
  81. cout << "value: " << val->getValue() << endl;
  82. val->setValue(999);
  83. cout << "value: " << val->getValue() << endl;
  84. }
  85. {
  86. Unko<int[]> arr(new int[10]);
  87. for (int i = 0 ; i < 10; i++) {
  88. arr[i] = i * i;
  89. cout << "arr[" << i << "] = " << arr[i] << endl;
  90. }
  91. }
  92. cout << "finish" << endl;
  93. return 0;
  94. }
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
start
Eat! (0x8cc9008)
Eat! (0x8cc9018)
unko: 200
Poo! (0x8cc9008)
Poo! (0x8cc9018)
Eat! (0x8cc9018)
value: 777
value: 999
Oh! God!
Poo! (0x8cc9018)
Eat! (0x8cc9028)
arr[0] = 0
arr[1] = 1
arr[2] = 4
arr[3] = 9
arr[4] = 16
arr[5] = 25
arr[6] = 36
arr[7] = 49
arr[8] = 64
arr[9] = 81
Poo! (0x8cc9028)
finish