// autopointer // - not valid for STL containers // - not valid for arrays #include <iostream> using std::cout; using std::endl; #include <string> #include <memory> using std::auto_ptr; // throw an exception void g() { bool some_condition = false; if (some_condition == false) throw "Any exception"; } void func() { // create and initialize an auto_ptr auto_ptr<string> pstr(new string); // dereference and assign *pstr = "hello world"; cout << "Created an autopointer string: " << *pstr << endl; // call a member function cout << ".. with size" << pstr->size() << endl; // could be an integer auto_ptr<int> index (new int); *index = 30; cout << "Created an integer index of " << *index << endl; g(); } int main() { try { func(); } catch(...) { cout << "exiting with no memory leak" << endl; } }
Hosted by www.Geocities.ws

1