#include using namespace std; template class vec{ public: // pointer to find array allocated in heap. T *p; // normally private, this was to get code developed quickly. // how many allocated. int capacity; // normally private // default (no parameters) constructor. vec():p(new T[10]), capacity(10){} // copy constructor - no check for self reference. vec(const vec& a):capacity(a.capacity){ cout << "In copy constructor: " << endl; cout << "address of p in this " << this->p << endl; cout << "address of p in a " << &(a.p) << endl; // taken out of the constructor list so that we // get the original address of p printed out. this->p = new T[a.capacity]; // deep copy the data. for(int i = 0; i < capacity; i++) *(p+i) = *(a.p+i); } // overload of assignment operator. vec& operator=(const vec& rhs) { // check for self reference. if(this != &rhs){ // get rid of heap allocation while pointer still available. delete [] p; // allocate new array on heap. p = new T[rhs.capacity]; capacity = rhs.capacity; // copy data from rhs to lhs. for(int i = 0; i < rhs.capacity; i++) *(p+i) = *(rhs.p+i); } return *this; } // the very necessary destructor. ~vec(){delete [] p; p = 0;} }; int main(int argc, char** argv){ vec A; int i; for(i = 0; i<10; i++) *(A.p+i) = i; for(i = 0; i<10; i++) cout<<*(A.p+i)<< endl; *(A.p+3)= 99999; vecB(A); for(i = 0; i<10; i++) cout<<*(B.p+i)<< endl; cout << " In main: address of p in B " << &(B.p) << endl; // Blocki's revenge B = *(new vec(B)); return EXIT_SUCCESS; }