#include <iostream>
using namespace std;
template <class T>
class Unko
{
private:
T* pointer;
public:
explicit Unko(T* t) {
pointer = t;
cout << "Eat! (" << pointer << ')' << endl;
}
~Unko() {
delete pointer;
cout << "Poo! (" << pointer << ')' << endl;
}
T& operator * () const { return *pointer; }
T* operator -> () const { return pointer; }
void swap(Unko& unko) {
T *tmp;
tmp = pointer;
pointer = unko.pointer;
unko.pointer = tmp;
}
explicit operator bool () const { return pointer != NULL; }
Unko(const Unko& unko) = delete;
Unko& operator=(const Unko& unko) = delete;
};
template <class T>
class Unko<T[]>
{
private:
T* pointer;
public:
explicit Unko(T* t) {
pointer = t;
cout << "Eat! (" << pointer << ')' << endl;
}
~Unko() {
delete [] pointer;
cout << "Poo! (" << pointer << ')' << endl;
}
T& operator [] (int n) const { return *(pointer + n); }
void swap(Unko& unko) {
T *tmp;
tmp = pointer;
pointer = unko.pointer;
unko.pointer = tmp;
}
explicit operator bool () const { return pointer != NULL; }
Unko(const Unko& unko) = delete;
Unko& operator=(const Unko& unko) = delete;
};
class Value
{
private:
int value;
public:
Value(int v) : value(v) {}
~Value() { cout << "Oh! God!" << endl; }
int getValue() { return value; }
void setValue(int v) { value = v; }
};
int main() {
cout << "start" << endl;
{
Unko<int> unko(new int);
Unko<int> poop(new int);
*unko = 100;
*poop = 200;
unko.swap(poop);
cout << "unko: " << *unko << endl;
}
{
Unko<Value> val(new Value(777));
cout << "value: " << val->getValue() << endl;
val->setValue(999);
cout << "value: " << val->getValue() << endl;
}
{
Unko<int[]> arr(new int[10]);
for (int i = 0 ; i < 10; i++) {
arr[i] = i * i;
cout << "arr[" << i << "] = " << arr[i] << endl;
}
}
cout << "finish" << endl;
return 0;
}