Auto ptr

auto_ptrszablon klasy sprytnego wskaźnika dostępny w bibliotece standardowej C++ (zadeklarowany w nagłówku <memory>) udostępniający podstawową funkcjonalność RAII dla zwykłych surowych wskaźników.

Wzorzec klasy auto_ptr opisuje obiekt przechowujący wskaźnik do zaalokowanego obiektu typu Typ*, dbając o to żeby obiekt na który ten wskaźnik wskazuje został automatycznie zniszczony wraz ze zniszczeniem wskaźnika − czyli zazwyczaj po opuszczeniu zasięgu.

W większości sytuacji preferowany zamiast auto_ptr jest wzorzec shared ptr proponowany w Technical Report 1 i dostępny w bibliotece Boost, a także zgodnie ze standardem C++0x - w bibliotece standardowej C++. Wersja robocza standardu C++0x, z sierpnia 2010, odradza korzystanie z auto_ptr, zalecając zastępowanie go nowym w bibliotece standardowej szablonem unique ptr[1][2]. auto ptr został usunięty w standardzie C++17[3].

Deklaracja

Klasa auto_ptr jest deklarowana w standardzie ISO/IEC 14882, sekcja 20.4.5 jako:

 namespace std {
 
     template <class Y> struct auto_ptr_ref {};
 
     template<class X>
     class auto_ptr {
     public:
         typedef X element_type;
 
         // 20.4.5.1 construct/copy/destroy:
         explicit           auto_ptr(X* p =0) throw();
                            auto_ptr(auto_ptr&) throw();
         template<class Y>  auto_ptr(auto_ptr<Y>&) throw();
 
         auto_ptr&                      operator=(auto_ptr&) throw();
         template<class Y> auto_ptr&    operator=(auto_ptr<Y>&) throw();
         auto_ptr&                      operator=(auto_ptr_ref<X> r) throw();
 
         ~auto_ptr() throw();
 
         // 20.4.5.2 members:
         X&     operator*() const throw();
         X*     operator->() const throw();
         X*     get() const throw();
         X*     release() throw();
         void   reset(X* p =0) throw();
 
         // 20.4.5.3 conversions:
                                    auto_ptr(auto_ptr_ref<X>) throw();
         template<class Y> operator auto_ptr_ref<Y>() throw();
         template<class Y> operator auto_ptr<Y>() throw();
     };
 
 }

Semantyka

Klasa auto_ptr ma semantykę ścisłej własności co oznacza, że instancja auto_ptr jest jedynym właścicielem obiektu, na który ona wskazuje. Przy kopiowaniu auto_ptr, źródłowa instancja ustawia się na wartość null. Przykład:

       int *i = new int;
       auto_ptr<int> x(i);
       auto_ptr<int> y;

       y = x;

       cout << x.get() << endl;
       cout << y.get() << endl;

Ten kod wypisze dla pierwszego obiektu auto_ptr adres NULL i inny adres dla drugiego, pokazując, że obiekt źródłowy utracił referencje podczas przypisania (=). Nie powinno się wykonywać operacji delete na źródłowym wskaźniku i, bo zostanie on usunięty przez drugi auto_ptr.

Trzeba zauważyć, że obiekt wskazywany przez auto_ptr jest usuwany przez operator delete; oznacza to że auto_ptr można używać tylko dla wskaźników uzyskanych przez operator new. Wyłącza to wskaźniki zwracane przez malloc/calloc/realloc i operator new[].

Przypisy

  1. Working Draft, Standard for Programming Language C++ N3092. 21 sierpnia 2010. [dostęp 2010-09-30].
  2. Danny Kalev: Using unique_ptr, Part I. informIT. [dostęp 2010-09-30].
  3. Programming Language C++, Library Evolution Working Group JTC1/SC22/WG21 N4190. 2014-10-09. (ang.).

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.