memory.cpp#//filename:memory# # //Comment by: Gel Cream#//e-mail: [Email protected]#//Blog:http://blog.csdn.net/mdl13412 # # //the smart pointer has only one auto_ptr in the STL that manages the life cycle of the native pointer ,#//But in itself there are many other insecure features, such as a auto_ptr to construct another#//auto_ptr can result in the transfer of object ownership, and if two pointers can point to a#//The acoustic pointer can cause the pointer to be accidentally released prematurely, and another auto_ptr to its dereference.#//will cause errors#//#//c++0x has passed:: Boost::scoped_ptr's case, so any use of Auto_ptr's#//scenarios should be replaced with scoped_ptr because they are more secure, but still cannot resolve multiple#//The smart Pointer has an early release problem caused by an object, to solve this problem, use the#//:: Boost::shared_ptr# # //my blog also has a auto_ptr I realized, we can refer to#// http://blog.csdn.net/mdl13412/article/details/6244631 # # /*# * Copyright (c) 1997 # * Silicon Graphics Computer Systems, Inc. # * # * Permission to use, copy, modify, Distr Ibute and sell this software # * and their documentation for any purpose are hereby granted without fee, # * provided that The above copyright notice appear in all copies and # * that both, copyright notice and this permission notice appear # * in supporting documentation. Silicon Graphics makes no # * representations about the suitability of this software for any # * purpose. It is provided ' as is ' without express or implied warranty. # * # */# # #ifndef __sgi_stl_memory ##define__sgi_stl_memory# # #include<stl_algobase.h># #include<stl_alloc.h># #include<stl_construct.h># #include<stl_tempbuf.h># #include<stl_uninitialized.h># #include<stl_raw_storage_iter.h> # # //Note:auto_ptr is commented out in this release because the details#//Of the interface is still being discussed by the C + + standardization#//Committee. It'll be included once the Iterface is finalized. # # #if0# #ifDefined (_mutable_is_keyword) && defined (_explicit_is_keyword) && \# defined (__stl_member_templates) # # __stl_begin_namespace # # Template<classX>classauto_ptr # {#Private: # X* PTR;//Managed native Pointers# mutableBOOLowns;//Whether you have a managed pointer# Public: # typedef X ELEMENT_TYPE; # # //explicit constructors to prevent implicit conversions#//typically receives a native pointer for construction#//The constructor cannot fail, so the exception cannot be thrown#ExplicitAuto_ptr (x* p =0) __stl_nothrow:ptr (P), owns (p) {} # #//auto_ptr can be constructed with the same type of auto_ptr#//Note: Object ownership is transferred, and only pointers are used for construction to dispose of object ownership# auto_ptr (Constauto_ptr&a) __stl_nothrow:ptr (A.ptr), owns (a.owns) {# a.owns=0; # } # # //auto_ptr can be constructed with another related type of auto_ptr#//Note: T must be able to convert to X type, object ownership transfer# template <classT> Auto_ptr (Constauto_ptr<t>&a) __stl_nothrow #: PTR (A.ptr), owns (a.owns) {# a.owns=0; # } # # //overload operator =, first determine whether it is itself, if not the transfer of object ownership# auto_ptr&operator=(Constauto_ptr&a) __stl_nothrow {#if(&a! = This) { # if(owns) #Deleteptr; # owns=a.owns; # ptr=a.ptr; # a.owns=0; # } # //personal feelings should be added here#//return *this; # } # # //same as the above operator = function, but provides a compatible type of conversion operation# template <classT> auto_ptr&operator=(Constauto_ptr<t>&a) __stl_nothrow {#if(&a! = This) { # if(owns) #Deleteptr; # owns=a.owns; # ptr=a.ptr; # a.owns=0; # } # # //personal feelings should be added here#//return *this; # } # # //auto_ptr end of life cycle, freeing object ownership for resource release purposes# ~auto_ptr () {#if(owns) #Deleteptr; # } # # //provides the same operation as the native pointer# x&operator*()Const__stl_nothrow {return*ptr;} # X*operator()Const__stl_nothrow {returnptr;} # # //gets the address of the native pointer, mainly for some functions that only accept native pointers# x*Get()Const__stl_nothrow {returnptr;} # //frees pointer ownership and returns the native pointer#//used primarily to cancel pointer hosting# x* ReleaseConst__stl_nothrow {owns =false;returnptr} #}; # # # # __stl_end_namespace ##endif/* mutable && Explicit && member Templates */# #endif/* 0 */# # # #endif/* __sgi_stl_memory */# # # //Local Variables:#//mode:c++#//End:
STL memory.cpp