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

📄 chapter10.html

📁 《C++编程思想》中文版。。。。。。。。。。。。。
💻 HTML
📖 第 1 页 / 共 5 页
字号:
const</B> variable that allows you to define a constant value inside a class
body.
<A NAME="Index1803"></A><A NAME="Index1804"></A><A NAME="Index1805"></A>It&#8217;s
also possible to create arrays of <B>static</B> objects, both <B>const</B> and
non-<B>const</B>. The syntax is reasonably consistent:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: C10:StaticArray.cpp</font>
<font color=#009900>// Initializing static arrays in classes</font>
<font color=#0000ff>class</font> Values {
  <font color=#009900>// static consts are initialized in-place:</font>
  <font color=#0000ff>static</font> <font color=#0000ff>const</font> <font color=#0000ff>int</font> scSize = 100;
  <font color=#0000ff>static</font> <font color=#0000ff>const</font> <font color=#0000ff>long</font> scLong = 100;
  <font color=#009900>// Automatic counting works with static arrays.</font>
  <font color=#009900>// Arrays, Non-integral and non-const statics </font>
  <font color=#009900>// must be initialized externally:</font>
  <font color=#0000ff>static</font> <font color=#0000ff>const</font> <font color=#0000ff>int</font> scInts[];
  <font color=#0000ff>static</font> <font color=#0000ff>const</font> <font color=#0000ff>long</font> scLongs[];
  <font color=#0000ff>static</font> <font color=#0000ff>const</font> <font color=#0000ff>float</font> scTable[];
  <font color=#0000ff>static</font> <font color=#0000ff>const</font> <font color=#0000ff>char</font> scLetters[];
  <font color=#0000ff>static</font> <font color=#0000ff>int</font> size;
  <font color=#0000ff>static</font> <font color=#0000ff>const</font> <font color=#0000ff>float</font> scFloat;
  <font color=#0000ff>static</font> <font color=#0000ff>float</font> table[];
  <font color=#0000ff>static</font> <font color=#0000ff>char</font> letters[];
};

<font color=#0000ff>int</font> Values::size = 100;
<font color=#0000ff>const</font> <font color=#0000ff>float</font> Values::scFloat = 1.1;

<font color=#0000ff>const</font> <font color=#0000ff>int</font> Values::scInts[] = {
  99, 47, 33, 11, 7
};

<font color=#0000ff>const</font> <font color=#0000ff>long</font> Values::scLongs[] = {
  99, 47, 33, 11, 7
};

<font color=#0000ff>const</font> <font color=#0000ff>float</font> Values::scTable[] = {
  1.1, 2.2, 3.3, 4.4
};

<font color=#0000ff>const</font> <font color=#0000ff>char</font> Values::scLetters[] = {
  'a', 'b', 'c', 'd', 'e',
  'f', 'g', 'h', 'i', 'j'
};

<font color=#0000ff>float</font> Values::table[4] = {
  1.1, 2.2, 3.3, 4.4
};

<font color=#0000ff>char</font> Values::letters[10] = {
  'a', 'b', 'c', 'd', 'e',
  'f', 'g', 'h', 'i', 'j'
};

<font color=#0000ff>int</font> main() { Values v; } <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>

<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">With <B>static const</B>s of integral
types you can provide the definitions inside the class, but for everything else
(including arrays of integral types, even if they are <B>static const</B>)<B>
</B>you must provide a single external definition for the member. These
definitions have internal linkage, so they can be placed in header files. The
syntax for initializing static arrays is the same as for any aggregate,
including automatic counting.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">You can also create <B>static const</B>
objects of class types and arrays of such objects. However, you cannot
initialize them using the &#8220;inline syntax&#8221; allowed for <B>static</B>
<B>const</B>s of integral built-in types:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: C10:StaticObjectArrays.cpp</font>
<font color=#009900>// Static arrays of class objects</font>
<font color=#0000ff>class</font> X {
  <font color=#0000ff>int</font> i;
<font color=#0000ff>public</font>:
  X(<font color=#0000ff>int</font> ii) : i(ii) {}
};

<font color=#0000ff>class</font> Stat {
  <font color=#009900>// This doesn't work, although </font>
  <font color=#009900>// you might want it to:</font>
<font color=#009900>//!  static const X x(100);</font>
  <font color=#009900>// Both const and non-const static class </font>
  <font color=#009900>// objects must be initialized externally:</font>
  <font color=#0000ff>static</font> X x2;
  <font color=#0000ff>static</font> X xTable2[];
  <font color=#0000ff>static</font> <font color=#0000ff>const</font> X x3;
  <font color=#0000ff>static</font> <font color=#0000ff>const</font> X xTable3[];
};

X Stat::x2(100);

X Stat::xTable2[] = {
  X(1), X(2), X(3), X(4)
};

<font color=#0000ff>const</font> X Stat::x3(100);

<font color=#0000ff>const</font> X Stat::xTable3[] = {
  X(1), X(2), X(3), X(4)
};

<font color=#0000ff>int</font> main() { Stat v; } <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>

<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The initialization of both <B>const</B>
and non-<B>const</B> <B>static</B> arrays of class objects must be performed the
same way, following the typical <B>static</B> definition
syntax.</FONT><A NAME="_Toc312373950"></A><A NAME="_Toc472654925"></A><BR></P></DIV>
<A NAME="Heading315"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Nested and local classes</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">You can easily put static data members in
classes that are nested <A NAME="Index1806"></A><A NAME="Index1807"></A>inside
other classes. The definition of such members is an intuitive and obvious
extension &#8211; you simply use another level of scope resolution. However, you
cannot have <B>static</B> data members inside local classes
<A NAME="Index1808"></A><A NAME="Index1809"></A>(a local class is a class
defined inside a function<A NAME="Index1810"></A>). Thus,</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: C10:Local.cpp</font>
<font color=#009900>// Static members &amp; local classes</font>
#include &lt;iostream&gt;
<font color=#0000ff>using</font> <font color=#0000ff>namespace</font> std;

<font color=#009900>// Nested class CAN have static data members:</font>
<font color=#0000ff>class</font> Outer {
  <font color=#0000ff>class</font> Inner {
    <font color=#0000ff>static</font> <font color=#0000ff>int</font> i; <font color=#009900>// OK</font>
  };
};

<font color=#0000ff>int</font> Outer::Inner::i = 47;

<font color=#009900>// Local class cannot have static data members:</font>
<font color=#0000ff>void</font> f() {
  <font color=#0000ff>class</font> Local {
  <font color=#0000ff>public</font>:
<font color=#009900>//! static int i;  // Error</font>
    <font color=#009900>// (How would you define i?)</font>
  } x;
} 

<font color=#0000ff>int</font> main() { Outer x; f(); } <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>

<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">You can see the immediate problem with a
<B>static</B> member in a local class: How do you describe the data member at
file scope in order to define it? In practice, local classes are used very
rarely.</FONT><A NAME="_Toc312373951"></A><A NAME="_Toc472654926"></A><BR></P></DIV>
<A NAME="Heading316"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
static member functions</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">You can also create <B>static</B> member
functions<A NAME="Index1811"></A>
<A NAME="Index1812"></A><A NAME="Index1813"></A><A NAME="Index1814"></A>that,
like <B>static</B> data members, work for the class as a whole rather than for a
particular object of a class. Instead of making a global function that lives in
and &#8220;pollutes&#8221; the global or local namespace, you bring the function
inside the class. When you create a <B>static</B> member function, you are
expressing an association with a particular class.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">You can call a <B>static</B> member
function in the ordinary way, with the dot or the arrow, in association with an
object. However, it&#8217;s more typical to call a <B>static</B> member function
by itself, without any specific object, using the scope-resolution operator,
like
this:<A NAME="Index1815"></A><A NAME="Index1816"></A><A NAME="Index1817"></A></FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: C10:SimpleStaticMemberFunction.cpp </font>
<font color=#0000ff>class</font> X {
<font color=#0000ff>public</font>:
  <font color=#0000ff>static</font> <font color=#0000ff>void</font> f(){};
};

<font color=#0000ff>int</font> main() {
  X::f();
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>

<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">When you see static member functions in a
class, remember that the designer intended that function to be conceptually
associated with the class as a whole.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">A <B>static</B> member function cannot
access ordinary data members, only <B>static</B> data members. It can call only
other <B>static</B> member functions. Normally, the address of the current
object (<B>this<A NAME="Index1818"></A></B>) is quietly passed in when any
member function is called, but a <B>static</B> member has no
<A NAME="Index1819"></A><B>this</B>, which is the reason it cannot access
ordinary members. Thus, you get the tiny increase in speed afforded by a global
function because a <B>static</B> member function doesn&#8217;t have the extra
overhead of passing <B>this</B>. At the same time you get the benefits of having
the function inside the class.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">For data members, <B>static</B> indicates
that only one piece of storage for member data exists for all objects of a
class. This parallels the use of <B>static</B> to define objects <I>inside</I> a
function to mean that only one copy of a local variable is used for all calls of
that function.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Here&#8217;s an example showing
<A NAME="Index1820"></A><B>static</B> data members and <B>static</B> member
functions used together:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: C10:StaticMemberFunctions.cpp</font>
<font color=#0000ff>class</font> X {
  <font color=#0000ff>int</font> i;
  <font color=#0000ff>static</font> <font color=#0000ff>int</font> j;
<font color=#0000ff>public</font>:
  X(<font color=#0000ff>int</font> ii = 0) : i(ii) {
     <font color=#009900>// Non-static member function can access</font>
     <font color=#009900>// static member function or data:</font>
    j = i;
  }
  <font color=#0000ff>int</font> val() <font color=#0000ff>const</font> { <font color=#0000ff>return</font> i; }
  <font color=#0000ff>static</font> <font color=#0000ff>int</font> incr() {
    <font color=#009900>//! i++; // Error: static member function</font>
    <font color=#009900>// cannot access non-static member data</font>
    <font color=#0000ff>return</font> ++j;
  }
  <font color=#0000ff>static</font> <font color=#0000ff>int</font> f() {
    <font color=#009900>//! val(); // Error: static member function</font>
    <font color=#009900>// cannot access non-static member function</font>
    <font color=#0000ff>return</font> incr(); <font color=#009900>// OK -- calls static</font>
  }
};

<font color=#0000ff>int</font> X::j = 0;

<font color=#0000ff>int</font> main() {
  X x;
  X* xp = &amp;x;
  x.f();
  xp-&gt;f();
  X::f(); <font color=#009900>// Only works with static members</font>
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>

<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Because they have no <B>this</B> pointer,
<B>static</B> member functions can neither access non-<B>static</B> data members
nor call non-<B>static</B> member functions.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Notice in <B>main(&#160;)</B> that a
<B>static</B> member can be selected

⌨️ 快捷键说明

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