A<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
		<id>http://www.cppe.ru/index.php?action=history&amp;feed=atom&amp;title=C%2B%2B_Tutorial%2FPointer%2Fauto_ptr</id>
		<title>C++ Tutorial/Pointer/auto ptr - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://www.cppe.ru/index.php?action=history&amp;feed=atom&amp;title=C%2B%2B_Tutorial%2FPointer%2Fauto_ptr"/>
		<link rel="alternate" type="text/html" href="http://www.cppe.ru/index.php?title=C%2B%2B_Tutorial/Pointer/auto_ptr&amp;action=history"/>
		<updated>2026-04-17T21:26:26Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://www.cppe.ru/index.php?title=C%2B%2B_Tutorial/Pointer/auto_ptr&amp;diff=2621&amp;oldid=prev</id>
		<title> в 14:21, 25 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://www.cppe.ru/index.php?title=C%2B%2B_Tutorial/Pointer/auto_ptr&amp;diff=2621&amp;oldid=prev"/>
				<updated>2010-05-25T14:21:17Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr style=&quot;vertical-align: top;&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Предыдущая&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Версия 14:21, 25 мая 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; style=&quot;text-align: center;&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
			</entry>

	<entry>
		<id>http://www.cppe.ru/index.php?title=C%2B%2B_Tutorial/Pointer/auto_ptr&amp;diff=2622&amp;oldid=prev</id>
		<title>Admin: 1 версия:&amp;#32;Импорт контента...</title>
		<link rel="alternate" type="text/html" href="http://www.cppe.ru/index.php?title=C%2B%2B_Tutorial/Pointer/auto_ptr&amp;diff=2622&amp;oldid=prev"/>
				<updated>2010-05-25T10:30:07Z</updated>
		
		<summary type="html">&lt;p&gt;1 версия: Импорт контента...&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==class auto_ptr: improved standard conforming implementation==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;/* The following code example is taken from the book&lt;br /&gt;
 * &amp;quot;The C++ Standard Library - A Tutorial and Reference&amp;quot;&lt;br /&gt;
 * by Nicolai M. Josuttis, Addison-Wesley, 1999&lt;br /&gt;
 *&lt;br /&gt;
 * (C) Copyright Nicolai M. Josuttis 1999.&lt;br /&gt;
 * Permission to copy, use, modify, sell and distribute this software&lt;br /&gt;
 * is granted provided this copyright notice appears in all copies.&lt;br /&gt;
 * This software is provided &amp;quot;as is&amp;quot; without express or implied&lt;br /&gt;
 * warranty, and with no claim as to its suitability for any purpose.&lt;br /&gt;
 */&lt;br /&gt;
/* class auto_ptr&lt;br /&gt;
 * - improved standard conforming implementation&lt;br /&gt;
 */&lt;br /&gt;
namespace std {&lt;br /&gt;
    // auxiliary type to enable copies and assignments (now global)&lt;br /&gt;
    template&amp;lt;class Y&amp;gt;&lt;br /&gt;
    struct auto_ptr_ref {&lt;br /&gt;
        Y* yp;&lt;br /&gt;
        auto_ptr_ref (Y* rhs)&lt;br /&gt;
         : yp(rhs) {&lt;br /&gt;
        }&lt;br /&gt;
    };&lt;br /&gt;
    template&amp;lt;class T&amp;gt;&lt;br /&gt;
    class auto_ptr {&lt;br /&gt;
      private:&lt;br /&gt;
        T* ap;    // refers to the actual owned object (if any)&lt;br /&gt;
      public:&lt;br /&gt;
        typedef T element_type;&lt;br /&gt;
        // constructor&lt;br /&gt;
        explicit auto_ptr (T* ptr = 0) throw()&lt;br /&gt;
         : ap(ptr) {&lt;br /&gt;
        }&lt;br /&gt;
        // copy constructors (with implicit conversion)&lt;br /&gt;
        // - note: nonconstant parameter&lt;br /&gt;
        auto_ptr (auto_ptr&amp;amp; rhs) throw()&lt;br /&gt;
         : ap(rhs.release()) {&lt;br /&gt;
        }&lt;br /&gt;
        template&amp;lt;class Y&amp;gt;&lt;br /&gt;
        auto_ptr (auto_ptr&amp;lt;Y&amp;gt;&amp;amp; rhs) throw()&lt;br /&gt;
         : ap(rhs.release()) {&lt;br /&gt;
        }&lt;br /&gt;
        &lt;br /&gt;
        // assignments (with implicit conversion)&lt;br /&gt;
        // - note: nonconstant parameter&lt;br /&gt;
        auto_ptr&amp;amp; operator= (auto_ptr&amp;amp; rhs) throw() {&lt;br /&gt;
            reset(rhs.release());&lt;br /&gt;
            return *this;&lt;br /&gt;
        }&lt;br /&gt;
        template&amp;lt;class Y&amp;gt;&lt;br /&gt;
        auto_ptr&amp;amp; operator= (auto_ptr&amp;lt;Y&amp;gt;&amp;amp; rhs) throw() {&lt;br /&gt;
            reset(rhs.release());&lt;br /&gt;
            return *this;&lt;br /&gt;
        }&lt;br /&gt;
        &lt;br /&gt;
        // destructor&lt;br /&gt;
        ~auto_ptr() throw() {&lt;br /&gt;
            delete ap;&lt;br /&gt;
        }&lt;br /&gt;
        // value access&lt;br /&gt;
        T* get() const throw() {&lt;br /&gt;
            return ap;&lt;br /&gt;
        }&lt;br /&gt;
        T&amp;amp; operator*() const throw() {&lt;br /&gt;
            return *ap;&lt;br /&gt;
        }&lt;br /&gt;
        T* operator-&amp;gt;() const throw() {&lt;br /&gt;
            return ap;&lt;br /&gt;
        }&lt;br /&gt;
        // release ownership&lt;br /&gt;
        T* release() throw() {&lt;br /&gt;
            T* tmp(ap);&lt;br /&gt;
            ap = 0;&lt;br /&gt;
            return tmp;&lt;br /&gt;
        }&lt;br /&gt;
        // reset value&lt;br /&gt;
        void reset (T* ptr=0) throw() {&lt;br /&gt;
            if (ap != ptr) {&lt;br /&gt;
                delete ap;&lt;br /&gt;
                ap = ptr;&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        /* special conversions with auxiliary type to enable copies and assignments&lt;br /&gt;
         */&lt;br /&gt;
        auto_ptr(auto_ptr_ref&amp;lt;T&amp;gt; rhs) throw()&lt;br /&gt;
         : ap(rhs.yp) {&lt;br /&gt;
        }&lt;br /&gt;
        auto_ptr&amp;amp; operator= (auto_ptr_ref&amp;lt;T&amp;gt; rhs) throw() {  // new&lt;br /&gt;
             reset(rhs.yp);&lt;br /&gt;
             return *this;&lt;br /&gt;
        }&lt;br /&gt;
        template&amp;lt;class Y&amp;gt;&lt;br /&gt;
        operator auto_ptr_ref&amp;lt;Y&amp;gt;() throw() {&lt;br /&gt;
            return auto_ptr_ref&amp;lt;Y&amp;gt;(release());&lt;br /&gt;
        }&lt;br /&gt;
        template&amp;lt;class Y&amp;gt;&lt;br /&gt;
        operator auto_ptr&amp;lt;Y&amp;gt;() throw() {&lt;br /&gt;
            return auto_ptr&amp;lt;Y&amp;gt;(release());&lt;br /&gt;
        }&lt;br /&gt;
    };&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==const auto_ptr==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;/* The following code example is taken from the book&lt;br /&gt;
 * &amp;quot;The C++ Standard Library - A Tutorial and Reference&amp;quot;&lt;br /&gt;
 * by Nicolai M. Josuttis, Addison-Wesley, 1999&lt;br /&gt;
 *&lt;br /&gt;
 * (C) Copyright Nicolai M. Josuttis 1999.&lt;br /&gt;
 * Permission to copy, use, modify, sell and distribute this software&lt;br /&gt;
 * is granted provided this copyright notice appears in all copies.&lt;br /&gt;
 * This software is provided &amp;quot;as is&amp;quot; without express or implied&lt;br /&gt;
 * warranty, and with no claim as to its suitability for any purpose.&lt;br /&gt;
 */&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;memory&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
/* define output operator for auto_ptr&lt;br /&gt;
 * - print object value or NULL&lt;br /&gt;
 */&lt;br /&gt;
template &amp;lt;class T&amp;gt;&lt;br /&gt;
ostream&amp;amp; operator&amp;lt;&amp;lt; (ostream&amp;amp; strm, const auto_ptr&amp;lt;T&amp;gt;&amp;amp; p)&lt;br /&gt;
{&lt;br /&gt;
    // does p own an object ?&lt;br /&gt;
    if (p.get() == NULL) {&lt;br /&gt;
        strm &amp;lt;&amp;lt; &amp;quot;NULL&amp;quot;;         // NO: print NULL&lt;br /&gt;
    }&lt;br /&gt;
    else {&lt;br /&gt;
        strm &amp;lt;&amp;lt; *p;             // YES: print the object&lt;br /&gt;
    }&lt;br /&gt;
    return strm;&lt;br /&gt;
}&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
    const auto_ptr&amp;lt;int&amp;gt; p(new int(42));&lt;br /&gt;
    const auto_ptr&amp;lt;int&amp;gt; q(new int(0));&lt;br /&gt;
    const auto_ptr&amp;lt;int&amp;gt; r;&lt;br /&gt;
    cout &amp;lt;&amp;lt; &amp;quot;after initialization:&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
    cout &amp;lt;&amp;lt; &amp;quot; p: &amp;quot; &amp;lt;&amp;lt; p &amp;lt;&amp;lt; endl;&lt;br /&gt;
    cout &amp;lt;&amp;lt; &amp;quot; q: &amp;quot; &amp;lt;&amp;lt; q &amp;lt;&amp;lt; endl;&lt;br /&gt;
    cout &amp;lt;&amp;lt; &amp;quot; r: &amp;quot; &amp;lt;&amp;lt; r &amp;lt;&amp;lt; endl;&lt;br /&gt;
    *q = *p;&lt;br /&gt;
//  *r = *p;    // ERROR: undefined behavior&lt;br /&gt;
    *p = -77;&lt;br /&gt;
    cout &amp;lt;&amp;lt; &amp;quot;after assigning values:&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
    cout &amp;lt;&amp;lt; &amp;quot; p: &amp;quot; &amp;lt;&amp;lt; p &amp;lt;&amp;lt; endl;&lt;br /&gt;
    cout &amp;lt;&amp;lt; &amp;quot; q: &amp;quot; &amp;lt;&amp;lt; q &amp;lt;&amp;lt; endl;&lt;br /&gt;
    cout &amp;lt;&amp;lt; &amp;quot; r: &amp;quot; &amp;lt;&amp;lt; r &amp;lt;&amp;lt; endl;&lt;br /&gt;
//  q = p;      // ERROR at compile time&lt;br /&gt;
//  r = p;      // ERROR at compile time&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;after initialization:&lt;br /&gt;
 p: 42&lt;br /&gt;
 q: 0&lt;br /&gt;
 r: NULL&lt;br /&gt;
after assigning values:&lt;br /&gt;
 p: -77&lt;br /&gt;
 q: 42&lt;br /&gt;
 r: NULL&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Define output operator for auto_ptr print object value or NULL==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;/* The following code example is taken from the book&lt;br /&gt;
 * &amp;quot;The C++ Standard Library - A Tutorial and Reference&amp;quot;&lt;br /&gt;
 * by Nicolai M. Josuttis, Addison-Wesley, 1999&lt;br /&gt;
 *&lt;br /&gt;
 * (C) Copyright Nicolai M. Josuttis 1999.&lt;br /&gt;
 * Permission to copy, use, modify, sell and distribute this software&lt;br /&gt;
 * is granted provided this copyright notice appears in all copies.&lt;br /&gt;
 * This software is provided &amp;quot;as is&amp;quot; without express or implied&lt;br /&gt;
 * warranty, and with no claim as to its suitability for any purpose.&lt;br /&gt;
 */&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;memory&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
/* define output operator for auto_ptr&lt;br /&gt;
 * - print object value or NULL&lt;br /&gt;
 */&lt;br /&gt;
template &amp;lt;class T&amp;gt;&lt;br /&gt;
ostream&amp;amp; operator&amp;lt;&amp;lt; (ostream&amp;amp; strm, const auto_ptr&amp;lt;T&amp;gt;&amp;amp; p)&lt;br /&gt;
{&lt;br /&gt;
    // does p own an object ?&lt;br /&gt;
    if (p.get() == NULL) {&lt;br /&gt;
        strm &amp;lt;&amp;lt; &amp;quot;NULL&amp;quot;;         // NO: print NULL&lt;br /&gt;
    }&lt;br /&gt;
    else {&lt;br /&gt;
        strm &amp;lt;&amp;lt; *p;             // YES: print the object&lt;br /&gt;
    }&lt;br /&gt;
    return strm;&lt;br /&gt;
}&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
    auto_ptr&amp;lt;int&amp;gt; p(new int(42));&lt;br /&gt;
    auto_ptr&amp;lt;int&amp;gt; q;&lt;br /&gt;
    cout &amp;lt;&amp;lt; &amp;quot;after initialization:&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
    cout &amp;lt;&amp;lt; &amp;quot; p: &amp;quot; &amp;lt;&amp;lt; p &amp;lt;&amp;lt; endl;&lt;br /&gt;
    cout &amp;lt;&amp;lt; &amp;quot; q: &amp;quot; &amp;lt;&amp;lt; q &amp;lt;&amp;lt; endl;&lt;br /&gt;
    q = p;&lt;br /&gt;
    cout &amp;lt;&amp;lt; &amp;quot;after assigning auto pointers:&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
    cout &amp;lt;&amp;lt; &amp;quot; p: &amp;quot; &amp;lt;&amp;lt; p &amp;lt;&amp;lt; endl;&lt;br /&gt;
    cout &amp;lt;&amp;lt; &amp;quot; q: &amp;quot; &amp;lt;&amp;lt; q &amp;lt;&amp;lt; endl;&lt;br /&gt;
    *q += 13;                   // change value of the object q owns&lt;br /&gt;
    p = q;&lt;br /&gt;
    cout &amp;lt;&amp;lt; &amp;quot;after change and reassignment:&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
    cout &amp;lt;&amp;lt; &amp;quot; p: &amp;quot; &amp;lt;&amp;lt; p &amp;lt;&amp;lt; endl;&lt;br /&gt;
    cout &amp;lt;&amp;lt; &amp;quot; q: &amp;quot; &amp;lt;&amp;lt; q &amp;lt;&amp;lt; endl;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;after initialization:&lt;br /&gt;
 p: 42&lt;br /&gt;
 q: NULL&lt;br /&gt;
after assigning auto pointers:&lt;br /&gt;
 p: NULL&lt;br /&gt;
 q: 42&lt;br /&gt;
after change and reassignment:&lt;br /&gt;
 p: 55&lt;br /&gt;
 q: NULL&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Demonstrate an auto_ptr==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;memory&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
class MyClass {&lt;br /&gt;
public:&lt;br /&gt;
  MyClass() {&lt;br /&gt;
     cout &amp;lt;&amp;lt; &amp;quot;constructing\n&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
  ~MyClass() {&lt;br /&gt;
     cout &amp;lt;&amp;lt; &amp;quot;destructing\n&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
  void f() {&lt;br /&gt;
     cout &amp;lt;&amp;lt; &amp;quot;f()\n&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
};&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
  auto_ptr&amp;lt;MyClass&amp;gt; p1(new MyClass), p2;&lt;br /&gt;
  p2 = p1; // transfer ownership&lt;br /&gt;
  p2-&amp;gt;f();&lt;br /&gt;
  // can assign to a normal pointer&lt;br /&gt;
  MyClass *ptr = p2.get();&lt;br /&gt;
  ptr-&amp;gt;f();&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;constructing&lt;br /&gt;
f()&lt;br /&gt;
f()&lt;br /&gt;
destructing&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	</feed>