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

📄 chapter02.html

📁 《C++编程思想》中文版。。。。。。。。。。。。。
💻 HTML
📖 第 1 页 / 共 5 页
字号:
&#8220;object&#8221;<A NAME="Index282"></A> to describe chunks of machine code
is an unfortunate artifact. The word came into use before object-oriented
programming was in general use. &#8220;Object&#8221; is used in the same sense
as &#8220;goal&#8221; when discussing compilation, while in object-oriented
programming it means &#8220;a thing with boundaries.&#8221;</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The <I>linker<A NAME="Index283"></A></I>
combines a list of object modules into an executable program that can be loaded
and run by the operating system. When a function in one object module makes a
reference to a function or variable in another object module, the linker
resolves these references; it makes sure that all the external functions and
data you claimed existed during compilation do exist<A NAME="Index284"></A>. The
linker also adds a special object module to perform start-up
activities.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The linker can search through special
files called <I>libraries</I> in order to resolve all its references. A
library<A NAME="Index285"></A> contains a collection of object modules in a
single file. A library is created and maintained by a program called a
<I>librarian</I>.</FONT><BR></P></DIV>
<A NAME="Heading70"></A><FONT FACE = "Verdana"><H4 ALIGN="LEFT">
Static type checking<BR><A NAME="Index286"></A></H4></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The compiler performs <I>type
checking<A NAME="Index287"></A></I> during the first pass. Type checking tests
for the proper use of arguments in functions and prevents many kinds of
programming errors. Since type checking occurs during compilation instead of
when the program is running, it is called <I>static type checking</I>.
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Some object-oriented languages (notably
Java<A NAME="Index288"></A>) perform some type checking at runtime (<I>dynamic
type checking</I>). If combined with static type checking,
<A NAME="Index289"></A>dynamic type checking is more powerful than static type
checking alone. However, it also adds overhead to program
execution.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">C++ uses static type checking because the
language cannot assume any particular runtime support for bad operations. Static
type checking notifies the programmer about misuses of types during compilation,
and thus maximizes execution speed. As you learn C++, you will see that most of
the language design decisions favor the same kind of high-speed,
production-oriented programming the C language is famous for.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">You can disable static type checking in
C++. You can also do your own dynamic type checking &#8211; you just need to
write the code.
</FONT><A NAME="_Toc462979717"></A><A NAME="_Toc472654721"></A><BR></P></DIV>
<A NAME="Heading71"></A><FONT FACE = "Verdana"><H2 ALIGN="LEFT">
Tools for separate compilation</H2></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Separate compilation is particularly
important <A NAME="Index290"></A>when building large projects. In C and C++, a
program can be created in small, manageable, independently tested pieces. The
most fundamental tool for breaking a program up into pieces is the ability to
create named subroutines or subprograms. In C and C++, a subprogram is called a
<A NAME="Index291"></A><I>function</I>, and functions are the pieces of code
that can be placed in different files, enabling separate compilation. Put
another way, the function is the atomic unit of code, since you cannot have part
of a function in one file and another part in a different file; the entire
function must be placed in a single file (although files can and do contain more
than one function).</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">When you call a function, you typically
pass it some <I>arguments</I>, which are values you&#8217;d like the function to
work with during its execution. When the function is finished, you typically get
back a <A NAME="Index292"></A><A NAME="Index293"></A><I>return value</I>, a
value that the function hands back to you as a result. It&#8217;s also possible
to write functions that take no <A NAME="Index294"></A>arguments and return no
values.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">To create a program with multiple files,
functions in one file must access functions and data in other files. When
compiling a file, the C or C++ compiler must know about the functions and data
in the other files, in particular their names and proper usage. The compiler
ensures that functions and data are used correctly. This process of
&#8220;telling the compiler&#8221; the names of external functions and data and
what they should look like is called <I>declaration<A NAME="Index295"></A></I>.
Once you declare a function or variable, the compiler knows how to check to make
sure it is used
properly.</FONT><A NAME="_Toc312373820"></A><A NAME="_Toc462979718"></A><A NAME="_Toc472654722"></A><BR></P></DIV>
<A NAME="Heading72"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Declarations vs. definitions</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">It&#8217;s important to understand the
difference between <I>declarations<A NAME="Index296"></A></I> and
<I>definitions<A NAME="Index297"></A></I> because these terms will be used
precisely throughout the book. Essentially all C and C++ programs require
declarations. Before you can write your first program, you need to understand
the proper way to write a declaration.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">A <I>declaration</I> introduces a name
&#8211; an identifier &#8211; to the compiler. It tells the compiler &#8220;This
function or this variable exists somewhere, and here is what it should look
like.&#8221; A <I>definition</I>, on the other hand, says: &#8220;Make this
variable here&#8221; or &#8220;Make this function here.&#8221; It allocates
storage for the name. This meaning works whether you&#8217;re talking about a
variable or a function; in either case, at the point of definition the compiler
allocates storage. For a variable, the compiler determines how big that variable
is and causes space to be generated in memory to hold the data for that
variable. For a function, the compiler generates code, which ends up occupying
storage in memory. </FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">You can declare a variable or a function
in many different places, but there must be only one definition in C and C++
(this is sometimes called the ODR: <A NAME="Index298"></A><I>one-definition
rule</I>). When the linker is uniting all the object modules, it will usually
complain if it finds more than one definition for the same function or
variable.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">A definition can also be a declaration.
If the compiler hasn&#8217;t seen the name <B>x</B> before and you define <B>int
x;</B>, the compiler sees the name as a declaration and allocates storage for it
all at once.</FONT><BR></P></DIV>
<A NAME="Heading73"></A><FONT FACE = "Verdana"><H4 ALIGN="LEFT">
Function declaration
syntax<BR><A NAME="Index299"></A><A NAME="Index300"></A><A NAME="Index301"></A></H4></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">A function declaration in C and C++ gives
the function name, the argument types passed to the function, and the return
value of the function. For example, here is a declaration for a function called
<B>func1(&#160;) </B>that takes two integer arguments (integers are denoted in
C/C++ with the keyword <B>int</B>) and returns an integer:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>int</font> func1(<font color=#0000ff>int</font>,<font color=#0000ff>int</font>);</PRE></FONT></BLOCKQUOTE>

<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The first keyword you see is the return
value all by itself: <B>int</B>. The arguments are enclosed in parentheses after
the function name in the order they are used. The semicolon indicates the end of
a statement; in this case, it tells the compiler &#8220;that&#8217;s all &#8211;
there is no function definition here!&#8221; </FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">C and C++ declarations attempt to mimic
the form of the item&#8217;s use. For example, if <B>a</B> is another integer
the above function might be used this way:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE>a = func1(2,3);</PRE></FONT></BLOCKQUOTE>

<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Since <B>func1</B>(&#160;) returns an
integer, the C or C++ compiler will check the use of <B>func1(&#160;)</B> to
make sure that <B>a</B> can accept the return value and that the arguments are
appropriate.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Arguments <A NAME="Index302"></A>in
function declarations may have names. The compiler ignores the names but they
can be helpful as mnemonic devices for the user. For example, we can declare
<B>func1(&#160;)</B> in a different fashion that has the same
meaning:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>int</font> func1(<font color=#0000ff>int</font> length, <font color=#0000ff>int</font> width);</PRE></FONT></BLOCKQUOTE>

<A NAME="Heading74"></A><FONT FACE = "Verdana"><H4 ALIGN="LEFT">
A gotcha</H4></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">There is a significant difference between
C and C++ for functions with empty argument lists. In C, the
declaration:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>int</font> func2();</PRE></FONT></BLOCKQUOTE>

<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">means &#8220;a function with any number
and type of argument.&#8221; This prevents type-checking<A NAME="Index303"></A>,
so in C++ it means &#8220;a function with no arguments.&#8221;</FONT><BR></P></DIV>
<A NAME="Heading75"></A><FONT FACE = "Verdana"><H4 ALIGN="LEFT">
Function definitions<BR><A NAME="Index304"></A><A NAME="Index305"></A></H4></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Function definitions look like function
declarations except that they have bodies<A NAME="Index306"></A>. A body is a
collection of statements enclosed in braces. Braces denote the beginning and
ending of a block of code. To give <B>func1(&#160;)</B> a definition that is an
empty body (a body containing no code), write:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>int</font> func1(<font color=#0000ff>int</font> length, <font color=#0000ff>int</font> width) { }</PRE></FONT></BLOCKQUOTE>

<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Notice that in the function definition,
the braces replace the semicolon. Since braces surround a statement or group of
statements, you don&#8217;t need a semicolon. Notice also that the arguments in
the function definition must have names if you want to use the arguments in the
function body (since they are never used here, they are
optional).</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><A NAME="Index307"></A><A NAME="Index308"></A><A NAME="Index309"></A><BR></P></DIV>
<A NAME="Heading76"></A><FONT FACE = "Verdana"><H4 ALIGN="LEFT">
Variable declaration syntax</H4></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The meaning attributed to the phrase
&#8220;variable declaration&#8221; has historically been confusing and
contradictory, and it&#8217;s important that you understand the correct
definition so you can read code properly. A variable declaration tells the
compiler what a variable looks like. It says, &#8220;I know you haven&#8217;t
seen this name before, but I promise it exists someplace, and it&#8217;s a
variable of X type.&#8221; </FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">In a function declaration, you give a
type (the return value), the function name, the argument list, and a semicolon.
That&#8217;s enough for the compiler to figure out that it&#8217;s a declaration
and what the function should look like. By inference, a variable declaration
might be a type followed by a name. For example:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>int</font> a;</PRE></FONT></BLOCKQUOTE>

<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">could declare the variable <B>a</B> as an
integer, using the logic above. Here&#8217;s the conflict: there is enough
information in the code above for the compiler to create space for an integer
called <B>a</B>, and that&#8217;s what happens. To resolve this dilemma, a
keyword was necessary for C and C++ to say &#8220;This is only a declaration;
it&#8217;s defined elsewhere.&#8221; The keyword is
<B>extern<A NAME="Index310"></A><A NAME="Index311"></A></B>. It can mean the
definition is <B>extern</B>al to the file, or that the definition occurs later
in the file.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Declaring a variable without defining it
means using the <B>extern</B> keyword before a description of the variable, like
this:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>extern</font> <font color=#0000ff>int</font> a;</PRE></FONT></BLOCKQUOTE>

<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>extern</B> can also apply to function
declarations. For <B>func1(&#160;)</B>, it looks like this:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>extern</font> <font color=#0000ff>int</font> func1(<font color=#0000ff>int</font> length, <font color=#0000ff>int</font> width);</PRE></FONT></BLOCKQUOTE>

<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">This statement is equivalent to the
previous <B>func1(&#160;)</B> declarations. Since there is no function body, the
compiler must treat it as a function declaration rather than a function
definition. The <B>extern</B> keyword is thus superfluous and optional for
function declarations. It is probably unfortunate that the designers of C did
not require the use of <B>extern</B> for function declarations; it would have

⌨️ 快捷键说明

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