// autopointer
// - not valid for STL containers
// - not valid for arrays
#include
using std::cout;
using std::endl;
#include
#include
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 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 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;
}
}