#include #include #include // has copy() #include // has assert() class foo { public: // constructor and destructor foo (const unsigned int sz) { size = sz; if (size == 0) size += 1; array = new string[size]; } ~foo(void) { delete [] array; } // copying functions foo(const foo & f) { copier(f); return; } foo& operator=(const foo & f) { if (this != &f) { delete [] array; copier(f); } return *this; } // element access string& operator[](unsigned int i) { assert(i < size); return array[i]; } const string& operator[](unsigned int i) const { assert(i < size); return array[i]; } // output friend ostream& operator<<(ostream& out, const foo & f) { for (unsigned int i = 0; i < f.size; i += 1) out << f.array[i] << endl; return out; } private: unsigned int size; string * array; void copier(const foo & f) { size = f.size; array = new string[size]; copy(f.array, f.array + size, array); return; } }; int main(void) { foo f(4); foo g(4); f[0] = f[2] = "hello"; f[1] = (f[3] = "world"); g = f; cout << "f:\n" << f; cout << "g:\n" << g; return EXIT_SUCCESS; }