//: C01:Auto_ptr.cpp // From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison. // (c) 1995-2004 MindView, Inc. All Rights Reserved. // See source code use permissions stated in the file 'License.txt', // distributed with the code package available at www.MindView.net. // Illustrates the RAII nature of auto_ptr. #include #include #include using namespace std; class TraceHeap { int i; public: static void* operator new(size_t siz) { void* p = ::operator new(siz); cout << "Allocating TraceHeap object on the heap " << "at address " << p << endl; return p; } static void operator delete(void* p) { cout << "Deleting TraceHeap object at address " << p << endl; ::operator delete(p); } TraceHeap(int i) : i(i) {} int getVal() const { return i; } }; int main() { auto_ptr pMyObject(new TraceHeap(5)); cout << pMyObject->getVal() << endl; // Prints 5 } ///:~