page598.html
来自「wqeqwvrw rkjqhwrjwq jkhrjqwhrwq jkhrwq」· HTML 代码 · 共 47 行
HTML
47 行
<HTML>
<HEAD>
<TITLE>Constructors and Destructors</TITLE>
</HEAD>
<BODY bgcolor="#FFFFFF">
<img src="cover75.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/cover75.gif" alt="Logo" align=right>
<b>Data Structures and Algorithms
with Object-Oriented Design Patterns in C++</b><br>
<A NAME="tex2html9295" HREF="page599.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page599.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="next_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/next_motif.gif"></A> <A NAME="tex2html9293" HREF="page596.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page596.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="up_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/up_motif.gif"></A> <A NAME="tex2html9287" HREF="page597.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page597.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="previous_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/previous_motif.gif"></A> <A NAME="tex2html9297" HREF="page9.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page9.html"><IMG WIDTH=65 HEIGHT=24 ALIGN=BOTTOM ALT="contents" SRC="contents_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/contents_motif.gif"></A> <A NAME="tex2html9298" HREF="page620.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page620.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="index_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/index_motif.gif"></A> <BR><HR>
<H2><A NAME="SECTION0018320000000000000000">Constructors and Destructors</A></H2>
<P>
A <em>constructor</em><A NAME=57280> </A> is a member function
that has the same name as its class (and which has no return value).
E.g., there are four constructors declared in Program <A HREF="page597.html#progcomplex1h" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page597.html#progcomplex1h"><IMG ALIGN=BOTTOM ALT="gif" SRC="cross_ref_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/cross_ref_motif.gif"></A>
(lines 6-9).
The purpose of a constructor is to <em>initialize</em> an object.
In fact, C++ makes the following promise:
If a class has a constructor,
all objects of that class will be initialized.
<P>
Consider the following sequence of variable declarations:
<PRE>Complex c; // calls Complex ()
Complex d = 2.0; // calls Complex (double)
Complex i(0, 1); // calls Complex (double, double)
Complex j(i); // calls Complex (Complex const&)</PRE>
Each variable declaration <em>implicitly</em> causes the appropriate
constructor to be invoked.
In particular, the equal sign in the declaration of <tt>d</tt>
denotes <em>initialization</em> not assignment and, therefore,
is accomplished by calling the constructor.
<P>
<BR> <HR>
<UL>
<LI> <A NAME="tex2html9299" HREF="page599.html#SECTION0018321000000000000000" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page599.html#SECTION0018321000000000000000">Default Constructor</A>
<LI> <A NAME="tex2html9300" HREF="page600.html#SECTION0018322000000000000000" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page600.html#SECTION0018322000000000000000">Copy Constructor</A>
<LI> <A NAME="tex2html9301" HREF="page601.html#SECTION0018323000000000000000" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page601.html#SECTION0018323000000000000000">The Copy Constructor, Parameter Passing
and Function Return Values</A>
<LI> <A NAME="tex2html9302" HREF="page602.html#SECTION0018324000000000000000" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page602.html#SECTION0018324000000000000000">Destructors</A>
</UL>
<HR><A NAME="tex2html9295" HREF="page599.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page599.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="next_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/next_motif.gif"></A> <A NAME="tex2html9293" HREF="page596.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page596.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="up_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/up_motif.gif"></A> <A NAME="tex2html9287" HREF="page597.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page597.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="previous_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/previous_motif.gif"></A> <A NAME="tex2html9297" HREF="page9.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page9.html"><IMG WIDTH=65 HEIGHT=24 ALIGN=BOTTOM ALT="contents" SRC="contents_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/contents_motif.gif"></A> <A NAME="tex2html9298" HREF="page620.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page620.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="index_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/index_motif.gif"></A> <P><ADDRESS>
<img src="bruno.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/bruno.gif" alt="Bruno" align=right>
<a href="javascript:if(confirm('http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/copyright.html \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address. \n\nDo you want to open it from the server?'))window.location='http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/copyright.html'" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/copyright.html">Copyright © 1997</a> by <a href="javascript:if(confirm('http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/signature.html \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address. \n\nDo you want to open it from the server?'))window.location='http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/signature.html'" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/signature.html">Bruno R. Preiss, P.Eng.</a> All rights reserved.
</ADDRESS>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?