#include #include #include #include using namespace std; class ken{ public: int d; string s; ken():d(1),s("one"){} }; void main(){ // declare a vector of pointers to ken objects vector v; // declare a vector of ken objects vectorvk; // make a compiler controlled ken object ken a; // push back the ken object // vk will have a deep copy of a vk.push_back(a); // change the copy of a in vk vk[0].d = 17; vk[0].s = "seventeen"; // prove that vk holds a copy cout << a.d << endl; vector::iterator ivk = vk.begin(); cout << (*ivk).d << endl; // now use the vector of pointers ken *pk; pk = new ken(); v.push_back(new ken()); v.push_back(&a); v.push_back(pk); v[0]->d = 3; v[0]->s = "three"; vector::iterator it = v.begin()+1; (*it)->d = 4; (*it)->s = "four"; for(it = v.begin(); it != v.end(); it++) cout << (*it)->d << " " << (*it)->s << endl; delete v[0]; delete v[2]; // delete v[1] not needed a is compiler controlled. /* output 3 three 4 four 1 one */ }