Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- template<class T, class S>
- class A
- {
- T t;
- S* s;
- int size;
- public:
- A(const T& t = 6, int size = 2) : t(t), size(size)
- {
- s = new S[size];
- }
- A(const A& other) : t(other.t), size(other.size)
- {
- s = new S[size];
- for (int i = 0; i < size; i++)
- s[i] = other.s[i];
- }
- ~A()
- {
- cout << "~A t=" << t << " size=" << size << endl;
- delete[]s;
- }
- int getSize() const { return size; }
- S& operator[](int index) { return s[index]; }
- friend ostream& operator<<(ostream& os, const A& a)
- {
- os << a.t << " " << a.size << ": ";
- for (int i = 0; i < a.size; i++)
- os << a.s[i] << " ";
- os << endl;
- return os;
- }
- };
- template <class T>
- class Object
- {
- T t;
- int x;
- public:
- Object(const T& t = 5, int x = 5) : t(t), x(x) {}
- ~Object() { cout << "~Object t=" << t << " x=" << x << endl; }
- operator T&() { return t; }
- friend ostream& operator<<(ostream& os, const Object& o)
- {
- os << o.t << " " << o.x << endl;
- return os;
- }
- };
- void foo()
- {
- A<int, Object<int> > a1(3, 3);
- cout << a1;
- cout << "1---------------------\n";
- for (int i = 0; i < a1.getSize(); i++)
- (a1[i])++;
- cout << a1;
- cout << "2---------------------\n";
- }
- void main()
- {
- foo();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement