📄 autoptr.htm
字号:
<html>
<head>
<title>auto_ptr</title>
<head>
<body bgcolor="#FFFFFF">
<a name="here"></a>
<img src="autoban.gif">
<pre>
<font size=5>Class Name</font> auto_ptr
<font size=5>Header File</font> <memory>
<font size=5>Classification</font> Memory Management
</pre>
<a href="autoptr.htm#class-descrip">Class Description</a>
<h1>Member Classes</h1>
<p>
None
</p>
<h1>Methods</h1>
<pre>
<a href="autoptr.htm#autoptr1">auto_ptr(const auto_ptr&) throw();</a>
<a href="autoptr.htm#autoptr2">explicit auto_ptr(X* p = 0) throw();</a>
<a href="autoptr.htm#autoptr3">template<class Y> auto_ptr(const auto_ptr<Y>& a) throw();</a>
<a href="autoptr.htm#~autoptr">~auto_ptr();</a>
<a href="autoptr.htm#get">X* get() const throw();</a>
<a href="autoptr.htm#release">X* release() const throw();</a>
</pre>
<h1>Operators</h1>
<pre>
<a href="autoptr.htm#operator=1">auto_ptr& operator=(const auto_ptr& a) throw();</a>
<a href="autoptr.htm#operator=2">template<class Y> auto_ptr& operator=(const auto_ptr<Y>& a) throw();</a>
<a href="autoptr.htm#operator*">X& operator*() const throw();</a>
<a href="autoptr.htm#operator->">X* operator->() const throw();</a>
</pre>
<a href="autoptr.htm#example"><h3>Example</h3></a>
<hr>
<a name="class-descrip"><h3>Class Description</h3></a>
<p>
The auto_ptr is a pointer like object. Like a regular pointer, it points to
an object in memory. It can be dereferenced and incremented. However, unlike
a regular pointer, when the auto_ptr object goes out of scope, the destructor
uses the delete operator to deallocate the memory occupied by the object to
which it points.
</p>
<hr>
<pre>
<a name="autoptr2">
Method auto_ptr()</a>
Access Public
Classification Constructor
Syntax explicit auto_ptr(X* p = 0) throw();
Parameters <em>p</em> is points to the object in which the newly constructed
auto_ptr object will own.
Returns None
</pre>
<h3>Description</h3>
<p>
This constructor is passed a pointer to an object. The self reference
pointer of the auto_ptr object, this, will hold the pointer <em>p</em> and
owns the object pointed to by <em>p</em>.
</p>
<hr>
<pre>
<a name="autoptr1">
Method auto_ptr()</a>
Access Public
Classification Constructor
Syntax auto_ptr(const auto_ptr& p) throw();
Parameters <em>p</em> is a reference to the auto_ptr.
Returns None
</pre>
<h3>Description</h3>
<p>
This constructor constructs an object that is a copy of auto_ptr <em>p</em>.
The ownersip of the object pointed to by <em>p</em> is passed to the newly
constructed object.
</p>
<hr>
<pre>
<a name="autoptr3">
Method auto_ptr()</a>
Access Public
Classification Constructor
Syntax template<class Y>
auto_ptr(const auto_ptr<Y>& a) throw();
Parameters <em>a</em> is a const reference to the auto_ptr.
Returns None
</pre>
<h3>Description</h3>
<p>
This constructor constructs an object that is a copy of auto_ptr <em>a</em>.
The ownersip of the object pointed to by <em>p</em> is passed to the newly
constructed object. It requires that Y* can be implicitly converted to X*.
X is the template parameter for auto_ptr. It is the object for which
auto_ptr manages dynamic memory.
</p>
<hr>
<pre>
<a name="~autoptr">
Method auto_ptr()</a>
Access Public
Classification Destructor
Syntax ~auto_ptr();
Parameters None
Returns None
</pre>
<h3>Description</h3>
<p>
The destructor destroys an auto_ptr object. It requires that *this owns the
pointer returned by get(). If this is true then the delete operator can
delete the object and deallocate the memory.
</p>
<hr>
<pre>
<a name="get">
Method get()</a>
Access Public
Classification Accessor
Syntax X* get() const throw();
Parameters None
Returns This method returns the pointer *this holds.
</pre>
<h3>Description</h3>
</p>
The get() method returns the pointer *this holds. It is a pointer to an
object of type X.
</p>
<hr>
<pre>
<a name="release">
Method release()</a>
Access Public
Classification Accessor
Syntax X* release() const throw();
Parameters None
Returns
</pre>
<h3>Desription</h3>
<p>
The release() method returns the pointer *this holds by
calling get() method. The purpose of this method is to transfer the
ownership of the object to which the pointer returned by get() points.
</p>
<hr>
<pre>
<a name="operator=1">
Method operator=()</a>
Access Public
Classification Modifier
Syntax auto_ptr& operator=(const auto_ptr& a) throw();
Parameters <em>a</em> is a reference to the auto_ptr object assigned
to the current auto_ptr object.
Returns This operator returns a pointer to the current object.
</pre>
<h3>Description</h3>
<p>
This operator=() assigns auto_ptr <em>a</em> to the current object. The
assignment operator calls a.release() if and only if *this and <em>a</em>
are not the same object. If *this and <em>a</em> are the same object then
nothing takes place. *this will now hold the returned pointer. *this will
own the object only if <em>a</em> owned the object. *this is returned.
</p>
<hr>
<pre>
<a name="operator=2">
Method operator=()</a>
Access Public
Classification Modifier
Syntax template<class Y>
auto_ptr& operator=(const auto_ptr<Y>& a) throw();
Parameters <em>a</em> is a const reference to the auto_ptr object assigned
to the current auto_ptr object.
Returns This operator returns a pointer to the current object.
</pre>
<h3>Description</h3>
<p>
This operator=() assigns auto_ptr <em>a</em> to the current object. The
assignment operator calls a.release() if and only if *this and <em>a</em>
are not the same object. If *this and <em>a</em> are the same object then
nothing takes place. *this will now hold the returned pointer. *this will
own the object only if <em>a</em> owned the object. *this is returned.
It requires that Y* can be implicitly converted to X*.
</p>
<hr>
<pre>
<a name="operator*">
Method operator*()</a>
Access Public
Classification Accessor
Syntax X& operator*() const throw();
Parameters None
Returns This method returns *get().
</pre>
<h3>Description</h3>
<p>
The operator->() returns *get().
</p>
<hr>
<pre>
<a name="operator->">
Method operator->()</a>
Access Public
Classification Accessor
Syntax X* operator->() const throw();
Parameters None
Returns This method returns get().
</pre>
<h3>Description</h3>
<p>
The operator->() returns get().
</p>
<hr>
<a name="example"></a>
<img src="lego.gif">
<pre>
// Listing 10.1
1 #include <memory>
2 #include <iostream>
3 #include <string>
4
5 using namespace std;
6
7 void main(void)
8 {
9 string *Str1 = new string("hello");
10 string *Str2 = new string("out there");
11 auto_ptr<string> AutoPtr1(Str1);
12 auto_ptr<string> AutoPtr2(Str2);
13
14 cout << "auto ptr 1 has object " << *AutoPtr1 << endl;
15 cout << "auto ptr 1 pointer is " << AutoPtr1.get() << endl;
16 cout << "auto ptr 2 has object " << *AutoPtr2 << endl;
17 cout << "auto ptr 2 pointer is " << AutoPtr2.get() << endl;
18 AutoPtr1 = AutoPtr2;
19 cout <<"after assignment" << endl;
20 cout << "auto ptr 1 has object " << *AutoPtr1 << endl;
21 cout << "auto ptr 1 pointer is " << AutoPtr1.get() << endl;
22 cout << "auto ptr 2 has object " << *AutoPtr2 << endl;
23 cout << "auto ptr 2 pointer is " << AutoPtr2.get() << endl;
24
25 }
26
</pre>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -