Lecture overview -- Keyboard shortcut: 'u'  Previous page: Resource acquisition is initialization - RAII -- Keyboard shortcut: 'p'  Next page: Object copying -- Keyboard shortcut: 'n'  Lecture notes - all slides and notes together  slide -- Keyboard shortcut: 't'  Help page about these notes  Alphabetic index  Course home  Lecture 3 - Page 12 : 36
Notes about C++
Abstraction Mechanisms, Part 1
Auto Pointers

An auto pointer, auto_ptr, is an encapsulation of a pointer that uses the RAII resource management idea

  • The C++ Programming Language: Page 367
 

 

  • An auto pointer uses destructive copy semantics

    • An assignment of auto pointers, lhs = rhs, modifies the lhs as well as the rhs

    • rhs becomes NULL

  • The get method in auto_ptr returns the encapsulated pointer

y:/Kurt/Files/Advanced-programming-cpp/cpp/kn/auto-pointers/point.hThe usual class Point - nothing of particular interest.


y:/Kurt/Files/Advanced-programming-cpp/cpp/kn/auto-pointers/auto-ptr1.ccAn illustration of auto_ptr<Point>.


y:/Kurt/Files/Advanced-programming-cpp/cpp/kn/auto-pointers/program-outputThe program output.


An auto pointer 'owns the pointer' - no other auto pointer should exist to the object

Due to the special meaning of copying, auto pointers cannot be used as the element type in standard containers