home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 May / Chip_2002-05_cd1.bin / chplus / cpp / 3 / stl.exe / autoptr.cpp < prev    next >
C/C++ Source or Header  |  1998-02-09  |  4KB  |  132 lines

  1. #include "stlexam.h"
  2. #pragma hdrstop
  3. /**************************************************************************
  4.  *
  5.  * autoptr.cpp - Illustrate the use of the auto_ptr class
  6.  *    Section 13
  7.  *
  8.  * $Id: autoptr.cpp,v 1.11 1996/08/28 01:18:57 smithey Exp $
  9.  *
  10.  ***************************************************************************
  11.  *
  12.  * (c) Copyright 1994, 1995 Rogue Wave Software, Inc.
  13.  * ALL RIGHTS RESERVED *
  14.  * The software and information contained herein are proprietary to, and
  15.  * comprise valuable trade secrets of, Rogue Wave Software, Inc., which
  16.  * intends to preserve as trade secrets such software and information.
  17.  * This software is furnished pursuant to a written license agreement and
  18.  * may be used, copied, transmitted, and stored only in accordance with
  19.  * the terms of such license and with the inclusion of the above copyright
  20.  * notice.  This software and information or any other copies thereof may
  21.  * not be provided or otherwise made available to any other person.
  22.  *
  23.  * Notwithstanding any other lease or license that may pertain to, or
  24.  * accompany the delivery of, this computer software and information, the
  25.  * rights of the Government regarding its use, reproduction and disclosure
  26.  * are as set forth in Section 52.227-19 of the FARS Computer
  27.  * Software-Restricted Rights clause.
  28.  * 
  29.  * Use, duplication, or disclosure by the Government is subject to
  30.  * restrictions as set forth in subparagraph (c)(1)(ii) of the Rights in
  31.  * Technical Data and Computer Software clause at DFARS 252.227-7013.
  32.  * Contractor/Manufacturer is Rogue Wave Software, Inc.,
  33.  * P.O. Box 2328, Corvallis, Oregon 97339.
  34.  *
  35.  * This computer software and information is distributed with "restricted
  36.  * rights."  Use, duplication or disclosure is subject to restrictions as
  37.  * set forth in NASA FAR SUP 18-52.227-79 (April 1985) "Commercial
  38.  * Computer Software-Restricted Rights (April 1985)."  If the Clause at
  39.  * 18-52.227-74 "Rights in Data General" is specified in the contract,
  40.  * then the "Alternate III" clause applies.
  41.  *
  42.  **************************************************************************/
  43.  
  44. #ifndef _RWSTD_HEADER_REQUIRES_HPP
  45. #include <vector>
  46. #include <utility>
  47. #include <string>
  48. #else
  49. #include <vector.hpp>
  50. #include <utility.hpp>
  51. #include <string.hpp>
  52. #endif
  53.  
  54. #ifdef _RW_STD_IOSTREAM
  55. #include <iostream>
  56. #else
  57. #include <iostream.h>
  58. #endif
  59.      
  60. // In this program we'll use auto_ptr to store pointers to strings
  61. // in a vector without having to worry about deleting the strings
  62. // themselves
  63.  
  64. #ifndef _RWSTD_NO_NAMESPACE
  65. namespace std {
  66. #endif        
  67.  
  68. template <class X>
  69. ostream& operator<<(ostream& os, const auto_ptr<X>& a)
  70. {
  71.    if (a.get())
  72.       cout << *a;
  73.    return os;
  74. }
  75.  
  76. #ifndef _RWSTD_NO_NAMESPACE
  77.   }
  78. #endif        
  79.  
  80. int main()
  81. {
  82.   #ifndef _RWSTD_NO_NAMESPACE
  83.   using namespace std; 
  84.   #endif        
  85.   {
  86.     // First the wrong way
  87.     vector<string*,allocator<string*> > v;
  88.     v.insert(v.begin(), new string("Florence"));
  89.     v.insert(v.begin(), new string("Milan"));
  90.     v.insert(v.begin(), new string("Venice"));
  91.     
  92.     // Now remove the first element
  93.     v.erase(v.begin());
  94.     // Whoops, memory leak
  95.     // string("Venice") was removed, but not deleted
  96.     // We were supposed to handle that ourselves
  97.   }
  98.   {
  99.     // Now the right way
  100.     vector<auto_ptr<string>,allocator<string>  > v;
  101.     v.insert(v.begin(), auto_ptr<string>(new string("Florence")));
  102.     v.insert(v.begin(), auto_ptr<string>(new string("Milan")));
  103.     v.insert(v.begin(), auto_ptr<string>(new string("Venice")));
  104.     // Everything is fine since auto_ptrs transfer ownership of their pointers when copied
  105.  
  106.  
  107.     cout << endl << "The Vector Container of Strings Contains : " << endl << endl << "\t";
  108.  
  109.     //Now copy this to the output
  110.     copy (v.begin(), v.end(), ostream_iterator<auto_ptr<string> ,char,char_traits<char> >(cout, "\n\t"));
  111.  
  112.     // Now remove the first element
  113.     v.erase(v.begin());
  114.        
  115.     // Success
  116.     // When auto_ptr(string("Venice")) is erased (and destroyed) 
  117.     // string("Venice") is deleted
  118.     
  119.  
  120.     //Now show access the auto_ptr with -> operator 
  121.     v[0]->assign("Rome");
  122.      
  123.     //Copy the contents of the auto_ptr to the output
  124.     cout << endl << "The first string in the vector container is now  " << v[0] << endl;
  125.      
  126.     
  127.   }
  128.  
  129.   return 0;
  130. }
  131.  
  132.