I have forgotten
my Password

Or login with:

  • Facebookhttp://facebook.com/
  • Googlehttps://www.google.com/accounts/o8/id
  • Yahoohttps://me.yahoo.com
ComputingStl

Auto_ptr

Smart pointer that deletes the instance it points to when going out of scope
+ View version details

Key Facts

Gyroscopic Couple: The rate of change of angular momentum (\inline \tau) = \inline I\omega\Omega (In the limit).
  • \inline I = Moment of Inertia.
  • \inline \omega = Angular velocity
  • \inline \Omega = Angular velocity of precession.


Blaise Pascal (1623-1662) was a French mathematician, physicist, inventor, writer and Catholic philosopher.

Leonhard Euler (1707-1783) was a pioneering Swiss mathematician and physicist.

Definition

The auto_ptr class template is defined in the standard header <memory>, and in the nonstandard backward-compatibility header <memory.h>.

template <class Type>
class auto_ptr
Parameter Description
Type The type of object for which storage is being allocated or deallocated

Description

The auto_ptr is provided by the C++ standard library as a kind of a smart pointer that helps to avoid resource leaks when exceptions are thrown.

It manages an object obtained via new and deletes that object when auto_ptr itself is destroyed. It may be used to provide exception safety for dynamically-allocated objects, for passing ownership of dynamically-allocated objects into functions and for returning dynamically-allocated objects from functions.

NOTE:
  • Copying an auto_ptr changes the object being copied by setting the contents to NULL
  • auto_ptr objects are not guaranteed to work correctly with the STL containers

Member Functions

Function Description
(constructor) Creates a new auto_ptr object
(destructor) Destroys an auto_ptr and the managed object
get Obtains a pointer to the managed object
operator* Dereference object (accesses the managed object)
operator-> Dereference object member (accesses the managed object)
release Releases ownership of the managed object
reset Deallocate object pointed and set new value
(conversion operators) Conversion operators
Example:
Example - Reset auto_ptr
Problem
The following example of program illustrates how to reset an auto_ptr.
Workings
#include <iostream>
#include <memory>
 
struct Resetter
{
  Resetter() { std::cout << "Constructor" << std::endl; }
  ~Resetter() { std::cout << "Destructor" << std::endl; }
};
 
int main()
{
  std::auto_ptr<Resetter> pReset(new Resetter);
  pReset.reset(new Resetter);
}
Solution
Output:

Constructor
Constructor
Destructor
Destructor
References