⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 index.html

📁 《Big C++ 》Third Edition电子书和代码全集-Part1
💻 HTML
📖 第 1 页 / 共 2 页
字号:
</font>

<hr><h2><font color="#009999">18.3.4 Field Initializer Lists (cont.)</font></h2>
<font size="+1">

<blockquote>
<pre>class Employee
{
public:
   Employee(String employee_name, double initial_salary);
   . . .
private:
   String name;
   double salary;
};

Employee::Employee(String employee_name, double initial_salary)
   <font color="#009999">: name(employee_name), salary(initial_salary)</font>
{
}</pre>
</blockquote>

</font>

<hr><h2><font color="#009999">18.3.4 Field Initializer Lists (cont.)</font></h2>
<font size="+1">

<ul>
	<li>Necessary to invoke base-class c'tors; otherwise, the base-class' default
		c'tor will be called:

		<blockquote>
<pre>class TeachingAssistant : public Employee
public:
   TeachingAssistant(String student_name);
};

TeachingAssistant::TeachingAssistant(String student_name)
   // Teaching assistants all get same starting salary
   <font color="#009999">: Employee(student_name, 5000)</font>
{
}</pre>
		</blockquote>

	</li>
</ul>

</font>

<hr><h2><font color="#009999">18.3.4 Field Initializer Lists (cont.)</font></h2>
<font size="+1">

<ul>
	<li>Initializer lists must be used for:

		<ul>
			<li>Data fields declared as non-static <tt>const</tt></li>
			<li>References</li>
			<li>To invoke a base class' c'tor (other than default)</li>
		</ul>

	</li>
</ul>

</font>

<hr><h2><font color="#009999">18.3.4 Field Initializer Lists (cont.)</font></h2>
<font size="+1">

<ul>
	<li>E.g., necessary when assigning a size to a vector member:

		<blockquote>
<pre>class PartDescription
{
public:
   PartDescription(String part_name,
      int inventory_number);
private:
   const String name;
   const int part_number:
   vector&lt;PartDescription*&gt; subcomponents;
};
PartDescription::PartDescription(String part_name,
      int inventory_number)
   : name(part_name), part_number(inventory_number),
      subcomponents(3) {}</pre>
		</blockquote>

	</li>
</ul>

</font>

<hr><h2><font color="#009999">18.3.5 Assignment Operators</font></h2>
<font size="+1">

<ul>
	<li>Tasks performed are similar to copy c'tor</li>
	<li>Must do cleanup first</li>
	<li>Check for auto-assignment</li>
</ul>

</font>

<hr><h2><font color="#009999">18.3.5 Assignment Operators (cont.)</font></h2>
<font size="+1">

<ul>
	<li>Example:  <tt>String</tt> class:

		<blockquote>
<pre>String&amp; String::operator=(const String&amp; right)
{
   if (this != &amp;right)
   {
      delete[] buffer; <font color="#0000cc">// Get rid of old buffer</font>
      len = right.length();
      buffer = new char[len + 1];
      for (int i = 0; i &lt; len; i++)
         buffer[i] = right[i];
      buffer[len] = '\0';
   }
   return *this;
}</pre>
		</blockquote>

	</li>
</ul>

</font>

<hr><h2><font color="#009999">18.4 Destructors</font></h2>
<font size="+1">

<ul>
	<li>Perform necessary housekeeping tasks</li>
	<li>Named ~<i>ClassName</i>, no arguments, no return type</li>
	<li>Implicitly called on:
		<ul>
			<li>Local variables, at end of block</li>
			<li>Parameters, at end of function</li>
			<li>Temporary variables, at end of statement</li>
			<li>Heap values, when <tt>delete</tt> is called</li>
			<li>Static values, when <tt>main</tt> terminates</li>
			<li>Member data, when object is deallocated</li>
			<li>Base class object, when derived class object is deallocated</li>
			<li>Local variables, when an exception is thrown and execution leaves
				the block</li>
		</ul>
	</li>
</ul>

</font>

<hr><h2><font color="#009999">18.4 Destructors (cont.)</font></h2>
<font size="+1">

<table border="1" cellpadding="4" bgcolor="#00cccc">
	<tr>
		<td bgcolor="#ffffff"><font size="+1"><font color="#009999">Syntax 17.2 :
			Overloading Operator Definition</font>

<pre><i><b>ClassName</b></i>::~<i><b>ClassName</b></i>()
{
	 <b><i>statements</i></b>
}</pre>

			<table border="0" cellpadding="4">
				<tr>
					<td valign="top"><font size="+1" color="#009999">
						Example:</font></td>
					<td><font size="+1">

<pre>String::~String()
{
   delete[] buffer;
}</pre>
						</font>
					</td>
				</tr>
				<tr>
					<td><font size="+1" color="#009999">Purpose:</font></td>
					<td><font size="+1">Perform any housekeeping tasks that should be
						performed before an object is deleted.</font></td>
				</tr>
			</table>
		</font>
		</td>
	</tr>
</table>

</font>

<hr><h2><font color="#009999">18.4 Virtual Destructors (cont.)</font></h2>
<font size="+1">

<ul>
	<li>Make destructors for base classes <tt>virtual</tt></li>
	<li>E.g.:  Consider the following declarations

		<blockquote>
<pre>class Employee
{
   virtual ~Employee();
};

Employee::~Employee()
{  cout &lt;&lt; "Goodbye Employee\n"; }</pre>
		</blockquote>
	</li>
</ul>

<hr><h2><font color="#009999">18.4 Virtual Destructors (cont.)</font></h2>
<font size="+1">

<ul>
	<blockquote>
<pre>class TeachingAssistant : public Employee
{
   ~TeachingAssistant();
};

TeachingAssistant::~TeachingAssistant()
{  cout &lt;&lt; "Goodbye TeachingAssistant\n"; }</pre>
	</blockquote>

	<p>Now consider the following:</p>

	<blockquote>
	Employee *a = new TeachingAssistant();<br>
	delete a;
	</blockquote>
</ul>

</font>

<hr><h2><font color="#009999">18.4 (cont.) "The Big 3" (revisit)</font></h2>
<font size="+1">

The "Big 3":
<ul>
	<ol>
		<li>Copy constructor</li>
		<li>Assignment operator</li>
		<li>Destructor</li>
	</ol>
</ul>
<ul>
	<li>All have default behavior, as does the default c'tor</li>
	<li>Behavior sometimes needs to be extended</li>
	<li>Reason to define one =&gt; reason to define all 3 (or all 4)</li>
	<li>Build all 3 on lower-level helper methods</li>
</ul>

</font>

<hr><h2><font color="#009999">18.4.1 The Class <tt>auto_ptr</tt></font></h2>
<font size="+1">

<ul>
	<li>"Smart pointers"</li>
	<li>Declared in <tt>&lt;memory&gt;</tt></li>
	<li>Good when the lifetime of the dynamic value is tied to another object</li>
	<li>Dynamic memory <tt>auto_ptr</tt> references is automatically
		recovered</li>
	<li>Very useful with exceptions</li>
</ul>

<font size="+0"><font color="#009999">See also:</font>  Advanced Topic 18.2 -
	Overloading the memory management operators.</font>

</font>

<hr><h2><font color="#009999">18.5 Reference Counting</font></h2>
<font size="+1">

<ul>
	<li>Simple rule:  objects that allocate memory should clean it up</li>
	<li>Not usually that simple.  Consider sharing:
		<script><!-- 
			image( "refCnt1.png" )
		//--></script>
	</li>
	<li>Two options:
		<ol>
			<li>Designate one as owner (not always possible)</li>
			<li>Use <i>reference counting</i></li>
		</ol>
	</li>
</ul>

</font>

<hr><h2><font color="#009999">18.5 Reference Counting</font></h2>
<font size="+1">

<ul>
	<li>Store reference count w/the shared resource</li>
	<li>New object has reference count of 1:
		<script><!-- 
			image( "refCnt2.png" )
		//--></script>
	</li>
	<li>When data object is shared, reference count is incremented:
		<script><!-- 
			image( "refCnt3.png" )
		//--></script>
	</li>
</ul>

</font>

<hr><h2><font color="#009999">18.5 Reference Counting (cont.)</font></h2>
<font size="+1">

<ul>
	<li>When a references is destroyed (or reassigned), ref count is decremented:
		<script><!-- 
			image( "refCnt4.png" )
		//--></script>
	</li>
	<li>When the reference count is zero, the shared resource is deleted</li>
</ul>

</font>

<hr><h2><font color="#009999">18.5 Example: Reference Counting</font></h2>
<font size="+1">

<p>Consider our <tt>String</tt> class:  change the semantics for the assignment
	of strings so that two strings would share a common internal data buffer:

<ul>
	<li>Structure holding reference count and the buffer is moved to a nested
		class, <tt>StringReference</tt></li>
	<li>References are managed by <tt>reassign</tt>
		<ul>
			<li>Takes ptr to a <tt>StringReference</tt></li>
			<li>If not NULL, reference to argument value is incremented</li>
			<li>Reference to current value is decremented</li>
			<li>If current reference is 0, object is deleted</li>
			<li>Current object is set to refer to argument value</li>
		</ul>
	</li>
</ul>

</font>

<hr><h2><font color="#009999">18.5 Example: Reference Counting
	(<tt>sharedString.cpp</tt>)</font></h2>
<font size="+1">

<script><!-- 
	iframeWrapCode( "sharedString.cpp", "90%", "80%" )
//--></script>

</font>

<hr><h2><font color="#009999">18.5 Reference Counting</font></h2>
<font size="+1">

<font color="#009999">Summary:</font>

<ul>
	<li>Create separate classes for; reference, shared data</li>
	<li>Reference count in shared data object</li>
	<li>Reference assignment: increment ref count</li>
	<li>Reference destroyed: decrement ref count</li>
	<li>Ref count reaches zero: destroy shared data</li>
</ul>

<font color="#009999">Note:</font>

<ul>
	<li>Beware of self-assignment</li>
	<li>Avoid cycles</li>
</ul>

</font>

<hr><h2><font color="#009999">18.6 Case Study: Matrices</font></h2>
<font size="+1">

Extend the matrix from 17.11:

<ul>
	<li>Represent arbitrary m x n matrix</li>
		<ul>
			<li>Need to allocate from heap</li>
		</ul>
	</li>
	<li>The "big three" done per Section 18.4</li>
	<li>Access methods replace constants for row/column information</li>
	<li>Smaller member functions have been inlined</li>
	<li><font color="#990000">(Changes have been
		<font color="#009999">highlighted</font>)</font></li>
</ul>

</font>

<hr><h2><font color="#009999">18.6 Case Study: Matrices (<tt>matrix2.h</tt>)
	</font></h2>
<font size="+1">

<script><!-- 
	iframeWrapCode( "matrix2.h", "90%", "80%" )
//--></script>

</font>

<hr><h2><font color="#009999">18.6 Case Study: Matrices (<tt>matrix2.cpp</tt>)
	</font></h2>
<font size="+1">

<script><!-- 
	iframeWrapCode( "matrix2.cpp", "90%", "80%" )
//--></script>

</font>

<hr><h2><font color="#009999">18.6 Case Study: Matrices,
	(<tt>matrixtest2.cpp</tt>)</font></h2>
<font size="+1">

<script><!-- 
	iframeWrapCode( "matrixtest2.cpp", "80%", "70%" )
//--></script>

</font>

<hr><h2><font color="#009999">Chapter Summary</font></h2>
<font size="+1">
<hr color="#00ffff" size="6">

<ol>
	<li>Memory is divided into four areas</li>
	<li>Pointers can refer to memory in any of the four areas</li>
	<li>Initialization errors can occur in any of the four area</li>
	<li>Stack-based memory is tied to function entry and exit</li>
	<li>Simple objects can be sliced.  Use references or pointers</li>
	<li>C'tors tie memory allocation with object initialization.  Copy
		c'tors create copies of objects</li>
	<li>D'tors return resources when an object is deallocated.  The "Big 3"</li>
	<li>Reference counts can be used to track shared resources</li>
</ol>

</font>

</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -