﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-mildcat1982-文章分类-C++</title><link>http://www.cppblog.com/mildcat1982/category/14194.html</link><description /><language>zh-cn</language><lastBuildDate>Mon, 09 Jul 2012 04:31:31 GMT</lastBuildDate><pubDate>Mon, 09 Jul 2012 04:31:31 GMT</pubDate><ttl>60</ttl><item><title>Item 47: Use traits classes for information about types </title><link>http://www.cppblog.com/mildcat1982/articles/182051.html</link><dc:creator>mildcat</dc:creator><author>mildcat</author><pubDate>Sat, 07 Jul 2012 04:05:00 GMT</pubDate><guid>http://www.cppblog.com/mildcat1982/articles/182051.html</guid><description><![CDATA[<p class="docText">The STL is primarily made up of templates for containers, iterators, and algorithms, but it also has a few utility templates. One of these is called <tt>advance</tt>. <tt>advance</tt> moves a specified iterator a specified distance:</p><pre>template&lt;typename IterT, typename DistT&gt;       // move iter d units

void advance(IterT&amp; iter, DistT d);            // forward; if d &lt; 0,

                                               // move iter backward

</pre><br />
<p class="docText">Conceptually, <tt>advance</tt> just does <tt>iter += d</tt>, but <tt>advance</tt> can't be implemented that way, because only random access iterators support the <tt>+=</tt> operation. Less powerful iterator types have to implement advance by iteratively applying <tt>++</tt> or <tt>-- d</tt> times.</p>
<p class="docText">Um, you don't remember your STL iterator categories? No problem, we'll do a mini-review. There are five categories of iterators, corresponding to the operations they support. <span class="docEmphasis">Input iterators</span> can move only forward, can move only one step at a time, can only read what they point to, and can read what they're pointing to only once. They're modeled on the read pointer into an input file; the C++ library's <tt>istream_iterator</tt>s are representative of this category. <span class="docEmphasis">Output iterators</span> are analogous, but for output: they move only forward, move only one step at a time, can only write what they point to, and can write it only once. They're modeled on the write pointer into an output file; <tt>ostream_iterator</tt>s epitomize this category. These are the two least powerful iterator categories. Because input and output iterators can move only forward and can read or write what they point to at most once, they are suitable only for one-pass algorithms.<a name="ch07index209"></a><a name="ch07index210"></a><a name="ch07index211"></a><a name="ch07index212"></a><a name="ch07index213"></a><a name="ch07index214"></a></p>
<p class="docText">A more powerful iterator category consists of <span class="docEmphasis">forward iterators</span>. Such iterators can do everything input and output iterators can do, plus they can read or write what they point to more than once. This makes them viable for multi-pass algorithms. The STL offers no singly linked list, but some libraries offer one (usually called <tt>slist</tt>), and iterators into such containers are forward iterators. Iterators into TR1's hashed containers (see <a class="docLink" href="mk:@MSITStore:D:\effective-cpp-3\英文第三版-Effective%20C++%20Third%20Edition%2055%20Specific%20Ways%20to%20Improve%20Your%20Programs%20and%20Designs.chm::/0321334876/ch09lev1sec2.html#ch09lev1sec2">Item 54</a>) may also be in the forward category.<a name="ch07index215"></a><a name="ch07index216"></a></p>
<p class="docText"><span class="docEmphasis">Bidirectional iterators</span> add to forward iterators the ability to move backward as well as forward. Iterators for the STL's <tt>list</tt> are in this category, as are iterators for <tt>set</tt>, <tt>multiset</tt>, <tt>map</tt>, and <tt>multimap</tt>.<a name="ch07index217"></a></p>
<p class="docText">The most powerful iterator category is that of <span class="docEmphasis">random access iterators</span>. These kinds of iterators add to bidirectional iterators the ability to perform "iterator arithmetic," i.e., to jump forward or backward an arbitrary distance in constant time. Such arithmetic is analogous to pointer arithmetic, which is not surprising, because random access iterators are modeled on built-in pointers, and built-in pointers can act as random access iterators. Iterators for <tt>vector</tt>, <tt>deque</tt>, and <tt>string</tt> are random access iterators.<a name="ch07index218"></a></p>
<p class="docText">For each of the five iterator categories, C++ has a "tag struct" in the standard library that serves to identify it:</p><a name="page228"></a><pre>struct <span class="docEmphStrong">input_iterator_tag</span> {};<a name="ch07index219"></a><a name="ch07index220"></a>



struct <span class="docEmphStrong">output_iterator_tag</span> {};<a name="ch07index221"></a><a name="ch07index222"></a>



struct <span class="docEmphStrong">forward_iterator_tag</span>: public input_iterator_tag {};<a name="ch07index223"></a><a name="ch07index224"></a>



struct <span class="docEmphStrong">bidirectional_iterator_tag</span>: public forward_iterator_tag {};<a name="ch07index225"></a><a name="ch07index226"></a>



struct <span class="docEmphStrong">random_access_iterator_tag</span>: public bidirectional_iterator_tag {};<a name="ch07index227"></a><a name="ch07index228"></a><a name="ch07index229"></a><a name="ch07index230"></a>

</pre><br />
<p class="docText">The inheritance relationships among these structs are valid is-a relationships (see <a class="docLink" href="mk:@MSITStore:D:\effective-cpp-3\英文第三版-Effective%20C++%20Third%20Edition%2055%20Specific%20Ways%20to%20Improve%20Your%20Programs%20and%20Designs.chm::/0321334876/ch06lev1sec1.html#ch06lev1sec1">Item 32</a>): it's true that all forward iterators are also input iterators, etc. We'll see the utility of this inheritance shortly.</p>
<p class="docText">But back to <tt>advance</tt>. Given the different iterator capabilities, one way to implement <tt>advance</tt> would be to use the lowest-common-denominator strategy of a loop that iteratively increments or decrements the iterator. However, that approach would take linear time. Random access iterators support constant-time iterator arithmetic, and we'd like to take advantage of that ability when it's present.</p>
<p class="docText">What we really want to do is implement <tt>advance</tt> essentially like this:</p><pre>template&lt;typename IterT, typename DistT&gt;<a name="ch07index231"></a><a name="ch07index232"></a>

void advance(IterT&amp; iter, DistT d)

{

  if (<span class="docEmphBoldItalic">iter is a random access iterator</span>) {

     iter += d;                                      // use iterator arithmetic

  }                                                  // for random access iters

  else {

    if (d &gt;= 0) { while (d--) ++iter; }              // use iterative calls to

    else { while (d++) --iter; }                     // ++ or -- for other

  }                                                  // iterator categories

}

</pre><br />
<p class="docText">This requires being able to determine whether <tt>iter</tt> is a random access iterator, which in turn requires knowing whether its type, <tt>IterT</tt>, is a random access iterator type. In other words, we need to get some information about a type. That's what <span class="docEmphasis">traits</span> let you do: they allow you to get information about a type during compilation.</p>
<p class="docText">Traits aren't a keyword or a predefined construct in C++; they're a technique and a convention followed by C++ programmers. One of the demands made on the technique is that it has to work as well for built-in types as it does for user-defined types. For example, if <tt>advance</tt> is called with a pointer (like a <tt>const char*</tt>) and an <tt>int</tt>, <tt>advance</tt> has to work, but that means that the traits technique must apply to built-in types like pointers.</p>
<p class="docText">The fact that traits must work with built-in types means that things like nesting information inside types won't do, because there's no way to nest information inside pointers. The traits information for a type, then, must be external to the type. The standard technique is to put it into a template and one or more specializations of that template. For iterators, the template in the standard library is named <tt>iterator_traits</tt>:<a name="ch07index233"></a></p><pre>template&lt;typename IterT&gt;          // template for information about

struct iterator_traits;           // iterator types

</pre><br />
<p class="docText">As you can see, <tt>iterator_traits</tt> is a struct. By convention, traits are always implemented as structs. Another convention is that the structs used to implement traits are known as &#8212; I am not making this up &#8212; traits <span class="docEmphasis">classes</span>.<a name="ch07index234"></a></p>
<p class="docText">The way <tt>iterator_traits</tt> works is that for each type <tt>IterT</tt>, a typedef named <tt>iterator_category</tt> is declared in the struct <tt>iterator_traits&lt;IterT&gt;</tt>. This typedef identifies the iterator category of <tt>IterT</tt>.</p>
<p class="docText"><tt>iterator_traits</tt> implements this in two parts. First, it imposes the requirement that any user-defined iterator type must contain a nested typedef named <tt>iterator_category</tt> that identifies the appropriate tag struct. <tt>deque</tt>'s iterators are random access, for example, so a class for <tt>deque</tt> iterators would look something like this:<a name="ch07index235"></a></p><pre>template &lt; ... &gt;                    // template params elided

class deque {<a name="ch07index236"></a>

public:

  class iterator {<a name="ch07index237"></a>

  public:

    typedef <span class="docEmphStrong">random_access_iterator_tag</span> iterator_category;

    ...

  }:

  ...

};

</pre><br />
<p class="docText"><tt>list</tt>'s iterators are bidirectional, however, so they'd do things this way:</p><pre>template &lt; ... &gt;

class list {<a name="ch07index238"></a>

public:

  class iterator {<a name="ch07index239"></a>

  public:

    typedef <span class="docEmphStrong">bidirectional_iterator_tag</span> iterator_category;

    ...

  }:

  ...

};

</pre><br />
<p class="docText"><tt>iterator_traits</tt> just parrots back the iterator class's nested typedef:</p><pre>// the iterator_category for type IterT is whatever IterT says it is;

// see Item 42 for info on the use of "typedef typename"

template&lt;typename IterT&gt;

struct iterator_traits {<a name="ch07index240"></a><a name="ch07index241"></a>

  <span class="docEmphStrong">typedef typename IterT::iterator_category iterator_category;</span>

  ...

};

</pre><br />
<p class="docText">This works well for user-defined types, but it doesn't work at all for iterators that are pointers, because there's no such thing as a pointer with a nested typedef. The second part of the <tt>iterator_traits</tt> implementation handles iterators that are pointers.</p>
<p class="docText">To support such iterators, <tt>iterator_traits</tt> offers a <span class="docEmphasis">partial template specialization</span> for pointer types. Pointers act as random access iterators, so that's the category <tt>iterator_traits</tt> specifies for them:<a name="ch07index242"></a><a name="ch07index243"></a></p><pre>template&lt;typename IterT&gt;               // partial template specialization

struct iterator_traits&lt;<span class="docEmphStrong">IterT*</span>&gt;         // for built-in pointer types<a name="ch07index244"></a><a name="ch07index245"></a>



{

  <span class="docEmphStrong">typedef random_access_iterator_tag iterator_category;</span>

  ...

};

</pre><br />
<p class="docText">At this point, you know how to design and implement a traits class:</p>
<ul><li>
<p class="docList">Identify some information about types you'd like to make available (e.g., for iterators, their iterator category).</p></li><li>
<p class="docList">Choose a name to identify that information (e.g., <tt>iterator_category</tt>).</p></li><li>
<p class="docList">Provide a template and set of specializations (e.g., <tt>iterator_traits</tt>) that contain the information for the types you want to support.</p></li></ul>
<p class="docText">Given <tt>iterator_traits</tt> &#8212; actually <tt>std::iterator_traits</tt>, since it's part of C++'s standard library &#8212; we can refine our pseudocode for <tt>advance</tt>:</p><pre>template&lt;typename IterT, typename DistT&gt;<a name="ch07index246"></a>

void advance(IterT&amp; iter, DistT d)

{

  if (<span class="docEmphStrong">typeid</span>(typename std::iterator_traits&lt;IterT&gt;::iterator_category) ==<a name="ch07index247"></a>

     <span class="docEmphStrong">typeid</span>(<span class="docEmphStrong">std::random_access_iterator_tag</span>))

  ...

}

</pre><br />
<p class="docText">Although this looks promising, it's not what we want. For one thing, it will lead to compilation problems, but we'll explore that in <a class="docLink" href="mk:@MSITStore:D:\effective-cpp-3\英文第三版-Effective%20C++%20Third%20Edition%2055%20Specific%20Ways%20to%20Improve%20Your%20Programs%20and%20Designs.chm::/0321334876/ch07lev1sec8.html#ch07lev1sec8">Item 48</a>; right now, there's a more fundamental issue to consider. <tt>IterT</tt>'s type is known during compilation, so <tt>iterator_traits&lt;IterT&gt;::iterator_category</tt> can also be determined during compilation. Yet the <tt>if</tt> statement is evaluated at runtime. Why do something at runtime that we can do during compilation? It wastes time (literally), and it bloats our executable.<a name="ch07index248"></a><a name="ch07index249"></a></p>
<p class="docText">What we really want is a conditional construct (i.e., an <tt>if...else</tt> statement) for types that is evaluated during compilation. As it happens, C++ already has a way to get that behavior. It's called overloading.<a name="ch07index250"></a><a name="ch07index251"></a><a name="ch07index252"></a></p>
<p class="docText">When you overload some function <tt>f</tt>, you specify different parameter types for the different overloads. When you call <tt>f</tt>, compilers pick the best overload, based on the arguments you're passing. Compilers essentially say, "If this overload is the best match for what's being passed, call this <tt>f</tt>; if this other overload is the best match, call it; if this third one is best, call it," etc. See? A compile-time conditional construct for types. To get <tt>advance</tt> to behave the way we want, all we have to do is create two versions of an overloaded function containing the "guts" of <tt>advance</tt>, declaring each to take a different type of <tt>iterator_category</tt> object. I use the name <tt>doAdvance</tt> for these functions:</p><pre>template&lt;typename IterT, typename DistT&gt;              // use this impl for<a name="ch07index253"></a>

void doAdvance(IterT&amp; iter, DistT d,                  // random access

               <span class="docEmphStrong">std::random_access_iterator_tag</span>)       // iterators



{

  iter += d;

}



template&lt;typename IterT, typename DistT&gt;              // use this impl for<a name="ch07index254"></a>

void doAdvance(IterT&amp; iter, DistT d,                  // bidirectional

               <span class="docEmphStrong">std::bidirectional_iterator_tag</span>)       // iterators

{

  if (d &gt;= 0) { while (d--) ++iter; }

  else { while (d++) --iter;         }

}



template&lt;typename IterT, typename DistT&gt;              // use this impl for<a name="ch07index255"></a>

void doAdvance(IterT&amp; iter, DistT d,                  // input iterators

               <span class="docEmphStrong">std::input_iterator_tag</span>)

{

  if (d &lt; 0 ) {

     throw std::out_of_range("Negative distance");    // see below

  }

  while (d--) ++iter;

}

</pre><br />
<p class="docText">Because <tt>forward_iterator_tag</tt> inherits from <tt>input_iterator_tag</tt>, the version of <tt>doAdvance</tt> for <tt>input_iterator_tag</tt> will also handle forward iterators. That's the motivation for inheritance among the various <tt>iterator_tag</tt> structs. (In fact, it's part of the motivation for <span class="docEmphasis">all</span> public inheritance: to be able to write code for base class types that also works for derived class types.)</p>
<p class="docText">The specification for <tt>advance</tt> allows both positive and negative distances for random access and bidirectional iterators, but behavior is undefined if you try to move a forward or input iterator a negative distance. The implementations I checked simply assumed that <tt>d</tt> was non-negative, thus entering a <span class="docEmphasis">very</span> long loop counting "down" to zero if a negative distance was passed in. In the code above, I've shown an exception being thrown instead. Both implementations are valid. That's the curse of undefined behavior: you <span class="docEmphasis">can't predict</span> what will happen.<a name="ch07index256"></a></p>
<p class="docText">Given the various overloads for <tt>doAdvance</tt>, all <tt>advance</tt> needs to do is call them, passing an extra object of the appropriate iterator category type so that the compiler will use overloading resolution to call the proper implementation:</p><pre>template&lt;typename IterT, typename DistT&gt;<a name="ch07index257"></a>

void advance(IterT&amp; iter, DistT d)

{

  <span class="docEmphStrong">doAdvance</span>(                                              // call the version

    iter, d,                                              // of doAdvance

    <span class="docEmphStrong">typename</span>                                              // that is

      <span class="docEmphStrong">std::iterator_traits&lt;IterT&gt;::iterator_category()</span>    // appropriate for

  );                                                      // iter's iterator

}                                                         // category

</pre><br />
<p class="docText">We can now summarize how to use a traits class:</p>
<ul><li>
<p class="docList">Create a set of overloaded "worker" functions or function templates (e.g., <tt>doAdvance</tt>) that differ in a traits parameter. Implement each function in accord with the traits information passed.</p></li><li>
<p class="docList">Create a "master" function or function template (e.g., <tt>advance</tt>) that calls the workers, passing information provided by a traits class.</p></li></ul>
<p class="docText">Traits are widely used in the standard library. There's <tt>iterator_traits</tt>, of course, which, in addition to <tt>iterator_category</tt>, offers four other pieces of information about iterators (the most useful of which is <tt>value_type</tt> &#8212; <a class="docLink" href="mk:@MSITStore:D:\effective-cpp-3\英文第三版-Effective%20C++%20Third%20Edition%2055%20Specific%20Ways%20to%20Improve%20Your%20Programs%20and%20Designs.chm::/0321334876/ch07lev1sec2.html#ch07lev1sec2">Item 42</a> shows an example of its use). There's also <tt>char_traits</tt>, which holds information about character types, and <tt>numeric_limits</tt>, which serves up information about numeric types, e.g., their minimum and maximum representable values, etc. (The name <tt>numeric_limits</tt> is a bit of a surprise, because the more common convention is for traits classes to end with "traits," but <tt>numeric_limits</tt> is what it's called, so <tt>numeric_limits</tt> is the name we use.)<a name="ch07index258"></a><a name="ch07index259"></a></p>
<p class="docText">TR1 (see <a class="docLink" href="mk:@MSITStore:D:\effective-cpp-3\英文第三版-Effective%20C++%20Third%20Edition%2055%20Specific%20Ways%20to%20Improve%20Your%20Programs%20and%20Designs.chm::/0321334876/ch09lev1sec2.html#ch09lev1sec2">Item 54</a>) introduces a slew of new traits classes that give information about types, including <tt>is_fundamental&lt;T&gt;</tt> (whether <tt>T</tt> is a built-in type), <tt>is_array&lt;T&gt;</tt> (whether <tt>T</tt> is an array type), and <tt>is_base_of&lt;T1, T2&gt;</tt> (whether <tt>T1</tt> is the same as or is a base class of <tt>T2</tt>). All told, TR1 adds over 50 traits classes to standard C++.</p><a name="ch07note07"></a>
<div class="docNote">
<p class="docNoteTitle">Things to Remember</p>
<ul><li>
<p class="docList">Traits classes make information about types available during compilation. They're implemented using templates and template specializations.</p></li><li>
<p class="docList">In conjunction with overloading, traits classes make it possible to perform compile-time <tt>if...else</tt> tests on types.<a name="ch07index260"></a><a name="ch07index261"></a><a name="ch07index262"></a></p></li></ul></div><img src ="http://www.cppblog.com/mildcat1982/aggbug/182051.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/mildcat1982/" target="_blank">mildcat</a> 2012-07-07 12:05 <a href="http://www.cppblog.com/mildcat1982/articles/182051.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>基于引用计数抽象基类的String实现4（EC++）</title><link>http://www.cppblog.com/mildcat1982/articles/120374.html</link><dc:creator>mildcat</dc:creator><author>mildcat</author><pubDate>Wed, 14 Jul 2010 12:26:00 GMT</pubDate><guid>http://www.cppblog.com/mildcat1982/articles/120374.html</guid><wfw:comment>http://www.cppblog.com/mildcat1982/comments/120374.html</wfw:comment><comments>http://www.cppblog.com/mildcat1982/articles/120374.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/mildcat1982/comments/commentRss/120374.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/mildcat1982/services/trackbacks/120374.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 文件String.h&nbsp;&nbsp;1#ifndef&nbsp;_STRING_H&nbsp;&nbsp;2#define&nbsp;_STRING_H&nbsp;&nbsp;3&nbsp;&nbsp;4class&nbsp;RefObject&nbsp;&nbsp;5{&nbsp;&nbsp;6public:&nbsp;&nbsp;7&nbsp;&nbsp;&nbsp;&nbsp;R...&nbsp;&nbsp;<a href='http://www.cppblog.com/mildcat1982/articles/120374.html'>阅读全文</a><img src ="http://www.cppblog.com/mildcat1982/aggbug/120374.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/mildcat1982/" target="_blank">mildcat</a> 2010-07-14 20:26 <a href="http://www.cppblog.com/mildcat1982/articles/120374.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>基于引用计数的String实现3（来自EC++） </title><link>http://www.cppblog.com/mildcat1982/articles/120231.html</link><dc:creator>mildcat</dc:creator><author>mildcat</author><pubDate>Tue, 13 Jul 2010 08:51:00 GMT</pubDate><guid>http://www.cppblog.com/mildcat1982/articles/120231.html</guid><wfw:comment>http://www.cppblog.com/mildcat1982/comments/120231.html</wfw:comment><comments>http://www.cppblog.com/mildcat1982/articles/120231.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/mildcat1982/comments/commentRss/120231.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/mildcat1982/services/trackbacks/120231.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: &nbsp; 也许不可能在operator[]内部区分左值还是右值操作，但我们仍然能区别对待读操作和写操作，如果我们将判断读还是写的行为推迟到我们知道operator[]的结果被怎么使用之后的话。我们所需要的是有一个方法将读或写的判断推迟到operator[]返回之后。proxy类可以让我们得到我们所需要的时机，因为我们可以修改operator[]让它返回一个（代理字符的）proxy对象而...&nbsp;&nbsp;<a href='http://www.cppblog.com/mildcat1982/articles/120231.html'>阅读全文</a><img src ="http://www.cppblog.com/mildcat1982/aggbug/120231.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/mildcat1982/" target="_blank">mildcat</a> 2010-07-13 16:51 <a href="http://www.cppblog.com/mildcat1982/articles/120231.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>一个String的测试程序（同样适用于标准模板库中的string）</title><link>http://www.cppblog.com/mildcat1982/articles/120223.html</link><dc:creator>mildcat</dc:creator><author>mildcat</author><pubDate>Tue, 13 Jul 2010 06:45:00 GMT</pubDate><guid>http://www.cppblog.com/mildcat1982/articles/120223.html</guid><wfw:comment>http://www.cppblog.com/mildcat1982/comments/120223.html</wfw:comment><comments>http://www.cppblog.com/mildcat1982/articles/120223.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/mildcat1982/comments/commentRss/120223.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/mildcat1982/services/trackbacks/120223.html</trackback:ping><description><![CDATA[<br>代码如下：<br><br>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><span style="COLOR: #008080">&nbsp;1</span><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #000000">#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;2</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#include&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">String.h</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;3</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br></span><span style="COLOR: #008080">&nbsp;4</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000">&nbsp;std;<br></span><span style="COLOR: #008080">&nbsp;5</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br></span><span style="COLOR: #008080">&nbsp;6</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">std::ostream</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">operator</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;(std::ostream</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">&nbsp;os,&nbsp;String</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">&nbsp;str)<br></span><span style="COLOR: #008080">&nbsp;7</span><span style="COLOR: #000000"><img id=Codehighlighter1_120_154_Open_Image onclick="this.style.display='none'; Codehighlighter1_120_154_Open_Text.style.display='none'; Codehighlighter1_120_154_Closed_Image.style.display='inline'; Codehighlighter1_120_154_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_120_154_Closed_Image onclick="this.style.display='none'; Codehighlighter1_120_154_Closed_Text.style.display='none'; Codehighlighter1_120_154_Open_Image.style.display='inline'; Codehighlighter1_120_154_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_120_154_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_120_154_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">&nbsp;8</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;os&nbsp;</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;str.c_str();<br></span><span style="COLOR: #008080">&nbsp;9</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;os;<br></span><span style="COLOR: #008080">10</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">11</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()<br></span><span style="COLOR: #008080">12</span><span style="COLOR: #000000"><img id=Codehighlighter1_167_1103_Open_Image onclick="this.style.display='none'; Codehighlighter1_167_1103_Open_Text.style.display='none'; Codehighlighter1_167_1103_Closed_Image.style.display='inline'; Codehighlighter1_167_1103_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_167_1103_Closed_Image onclick="this.style.display='none'; Codehighlighter1_167_1103_Closed_Text.style.display='none'; Codehighlighter1_167_1103_Open_Image.style.display='inline'; Codehighlighter1_167_1103_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_167_1103_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_167_1103_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">13</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">sizeof(&nbsp;String&nbsp;)&nbsp;is:</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(&nbsp;String&nbsp;)&nbsp;</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;endl;<br></span><span style="COLOR: #008080">14</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"><br></span><span style="COLOR: #008080">15</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"><br></span><span style="COLOR: #008080">16</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;str1&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">hello</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">17</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;str2(str1);<br></span><span style="COLOR: #008080">18</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;&nbsp;&nbsp;&nbsp;const&nbsp;String&nbsp;str2(str1);</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">19</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">20</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;printf(&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">str1&nbsp;address:%p\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,&nbsp;str1.c_str());<br></span><span style="COLOR: #008080">21</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;printf(&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">str2&nbsp;address:%p\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,&nbsp;str2.c_str());<br></span><span style="COLOR: #008080">22</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br></span><span style="COLOR: #008080">23</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;endl&nbsp;</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">-----after&nbsp;writing&nbsp;str2-----</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;endl&nbsp;</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;endl;<br></span><span style="COLOR: #008080">24</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;<br></span><span style="COLOR: #008080">25</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">str2[0]&nbsp;=&nbsp;'w';</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">26</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;str2[</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #008080">27</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;<br></span><span style="COLOR: #008080">28</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;printf(&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">str1&nbsp;address:%p\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,&nbsp;str1.c_str());<br></span><span style="COLOR: #008080">29</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;printf(&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">str2&nbsp;address:%p\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,&nbsp;str2.c_str());<br></span><span style="COLOR: #008080">30</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">31</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">32</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;str3&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">1234</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">33</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">&nbsp;c&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;str3[</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #008080">34</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;str4(&nbsp;str3&nbsp;);<br></span><span style="COLOR: #008080">35</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;str5(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">12</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">36</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;str5&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;str3;<br></span><span style="COLOR: #008080">37</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;<br></span><span style="COLOR: #008080">38</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;printf(&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">str3&nbsp;address:%p\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,&nbsp;str3.c_str());<br></span><span style="COLOR: #008080">39</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;printf(&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">str4&nbsp;address:%p\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,&nbsp;str4.c_str());<br></span><span style="COLOR: #008080">40</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;printf(&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">str5&nbsp;address:%p\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,&nbsp;str5.c_str());<br></span><span style="COLOR: #008080">41</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">42</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;c&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">H</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">43</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">44</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">str3&nbsp;=&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;str3.c_str()&nbsp;</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;endl;<br></span><span style="COLOR: #008080">45</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">str4&nbsp;=&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;str4.c_str()&nbsp;</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;endl;<br></span><span style="COLOR: #008080">46</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">str5&nbsp;=&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;str5.c_str()&nbsp;</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;endl;<br></span><span style="COLOR: #008080">47</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;<br></span><span style="COLOR: #008080">48</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;system(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">PAUSE</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">49</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;EXIT_SUCCESS;<br></span><span style="COLOR: #008080">50</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">51</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span></div>
<p><br>对于版本2，&nbsp; 在Dev C++ IDE下的输出结果为：<br><br>sizeof( String ) is:4<br>str1 address:004A0F60<br>str2 address:004A0F60</p>
<p>-----after writing str2-----</p>
<p>str1 address:004A0F60<br>str2 address:004A0FF8<br>str3 address:004A1020<br>str4 address:004A1048<br>str5 address:004A1098<br>str3 = H234<br>str4 = 1234<br>str5 = 1234</p>
<img src ="http://www.cppblog.com/mildcat1982/aggbug/120223.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/mildcat1982/" target="_blank">mildcat</a> 2010-07-13 14:45 <a href="http://www.cppblog.com/mildcat1982/articles/120223.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>基于引用计数的String实现2（来自EC++） </title><link>http://www.cppblog.com/mildcat1982/articles/120220.html</link><dc:creator>mildcat</dc:creator><author>mildcat</author><pubDate>Tue, 13 Jul 2010 06:37:00 GMT</pubDate><guid>http://www.cppblog.com/mildcat1982/articles/120220.html</guid><wfw:comment>http://www.cppblog.com/mildcat1982/comments/120220.html</wfw:comment><comments>http://www.cppblog.com/mildcat1982/articles/120220.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/mildcat1982/comments/commentRss/120220.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/mildcat1982/services/trackbacks/120220.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 文件String.h&nbsp;1#ifndef&nbsp;_STRING_H&nbsp;2#define&nbsp;_STRING_H&nbsp;3&nbsp;4#include&nbsp;&lt;ostream&gt;&nbsp;5class&nbsp;String&nbsp;6{&nbsp;7public:&nbsp;8&nbsp;&nbsp;&nbsp;&nbsp;String(con...&nbsp;&nbsp;<a href='http://www.cppblog.com/mildcat1982/articles/120220.html'>阅读全文</a><img src ="http://www.cppblog.com/mildcat1982/aggbug/120220.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/mildcat1982/" target="_blank">mildcat</a> 2010-07-13 14:37 <a href="http://www.cppblog.com/mildcat1982/articles/120220.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>基于引用计数的String实现1（来自EC++）</title><link>http://www.cppblog.com/mildcat1982/articles/119948.html</link><dc:creator>mildcat</dc:creator><author>mildcat</author><pubDate>Sat, 10 Jul 2010 02:32:00 GMT</pubDate><guid>http://www.cppblog.com/mildcat1982/articles/119948.html</guid><wfw:comment>http://www.cppblog.com/mildcat1982/comments/119948.html</wfw:comment><comments>http://www.cppblog.com/mildcat1982/articles/119948.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/mildcat1982/comments/commentRss/119948.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/mildcat1982/services/trackbacks/119948.html</trackback:ping><description><![CDATA[文件String.h<br>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#ifndef&nbsp;_STRING_H<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">#define</span><span style="COLOR: #000000">&nbsp;_STRING_H</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">&nbsp;String<br><img id=Codehighlighter1_50_472_Open_Image onclick="this.style.display='none'; Codehighlighter1_50_472_Open_Text.style.display='none'; Codehighlighter1_50_472_Closed_Image.style.display='inline'; Codehighlighter1_50_472_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_50_472_Closed_Image onclick="this.style.display='none'; Codehighlighter1_50_472_Closed_Text.style.display='none'; Codehighlighter1_50_472_Open_Image.style.display='inline'; Codehighlighter1_50_472_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_50_472_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_50_472_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000">:<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;String(</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;cszStr&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">""</span><span style="COLOR: #000000">);<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;String(</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;String</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">&nbsp;rhs);<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;String</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">operator</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;String</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">&nbsp;rhs);<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #000000">~</span><span style="COLOR: #000000">String();<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"><br><img id=Codehighlighter1_199_219_Open_Image onclick="this.style.display='none'; Codehighlighter1_199_219_Open_Text.style.display='none'; Codehighlighter1_199_219_Closed_Image.style.display='inline'; Codehighlighter1_199_219_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_199_219_Closed_Image onclick="this.style.display='none'; Codehighlighter1_199_219_Closed_Text.style.display='none'; Codehighlighter1_199_219_Open_Image.style.display='inline'; Codehighlighter1_199_219_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;c_str()</span><span style="COLOR: #0000ff">const</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_199_219_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_199_219_Open_Text><span style="COLOR: #000000">{</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;value</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">data;}</span></span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000">:<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">operator</span><span style="COLOR: #000000">[](size_t&nbsp;idx);<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">operator</span><span style="COLOR: #000000">[](size_t&nbsp;idx)</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000">:<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000">&nbsp;StringValue<br><img id=Codehighlighter1_334_439_Open_Image onclick="this.style.display='none'; Codehighlighter1_334_439_Open_Text.style.display='none'; Codehighlighter1_334_439_Closed_Image.style.display='inline'; Codehighlighter1_334_439_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_334_439_Closed_Image onclick="this.style.display='none'; Codehighlighter1_334_439_Closed_Text.style.display='none'; Codehighlighter1_334_439_Open_Image.style.display='inline'; Codehighlighter1_334_439_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_334_439_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_334_439_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;size_t&nbsp;refCount;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;size_t&nbsp;len;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;data;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;StringValue(</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;cszStr);<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #000000">~</span><span style="COLOR: #000000">StringValue();<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #0000ff">private</span><span style="COLOR: #000000">:<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;StringValue</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;value;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">#endif</span></div>
文件String.cpp： <br>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #000000">#include&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">String.h</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">assert.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">String::String(</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;cszStr)<br><img id=Codehighlighter1_96_132_Open_Image onclick="this.style.display='none'; Codehighlighter1_96_132_Open_Text.style.display='none'; Codehighlighter1_96_132_Closed_Image.style.display='inline'; Codehighlighter1_96_132_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_96_132_Closed_Image onclick="this.style.display='none'; Codehighlighter1_96_132_Closed_Text.style.display='none'; Codehighlighter1_96_132_Open_Image.style.display='inline'; Codehighlighter1_96_132_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_96_132_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_96_132_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;value&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000">&nbsp;StringValue(cszStr);<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">String::String(</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;String</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">&nbsp;rhs)<br><img id=Codehighlighter1_169_211_Open_Image onclick="this.style.display='none'; Codehighlighter1_169_211_Open_Text.style.display='none'; Codehighlighter1_169_211_Closed_Image.style.display='inline'; Codehighlighter1_169_211_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_169_211_Closed_Image onclick="this.style.display='none'; Codehighlighter1_169_211_Closed_Text.style.display='none'; Codehighlighter1_169_211_Open_Image.style.display='inline'; Codehighlighter1_169_211_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_169_211_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_169_211_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;value&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;rhs.value;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;value</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">refCount</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">String</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">&nbsp;String::</span><span style="COLOR: #0000ff">operator</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;String</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">&nbsp;rhs)<br><img id=Codehighlighter1_259_437_Open_Image onclick="this.style.display='none'; Codehighlighter1_259_437_Open_Text.style.display='none'; Codehighlighter1_259_437_Closed_Image.style.display='inline'; Codehighlighter1_259_437_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_259_437_Closed_Image onclick="this.style.display='none'; Codehighlighter1_259_437_Closed_Text.style.display='none'; Codehighlighter1_259_437_Open_Image.style.display='inline'; Codehighlighter1_259_437_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_259_437_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_259_437_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(&nbsp;rhs.value&nbsp;</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">this</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">value&nbsp;)</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #0000ff">this</span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #000000">--</span><span style="COLOR: #000000">value</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">refCount;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(&nbsp;value</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">refCount&nbsp;</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">&nbsp;)delete&nbsp;value;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;value</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">data&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;rhs.value</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">data;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">value</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">refCount;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #0000ff">this</span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">String::</span><span style="COLOR: #000000">~</span><span style="COLOR: #000000">String()<br><img id=Codehighlighter1_458_503_Open_Image onclick="this.style.display='none'; Codehighlighter1_458_503_Open_Text.style.display='none'; Codehighlighter1_458_503_Closed_Image.style.display='inline'; Codehighlighter1_458_503_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_458_503_Closed_Image onclick="this.style.display='none'; Codehighlighter1_458_503_Closed_Text.style.display='none'; Codehighlighter1_458_503_Open_Image.style.display='inline'; Codehighlighter1_458_503_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_458_503_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_458_503_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(&nbsp;</span><span style="COLOR: #000000">--</span><span style="COLOR: #000000">value</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">refCount&nbsp;</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">&nbsp;)delete&nbsp;value;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">&nbsp;String::</span><span style="COLOR: #0000ff">operator</span><span style="COLOR: #000000">[](size_t&nbsp;idx)<br><img id=Codehighlighter1_543_811_Open_Image onclick="this.style.display='none'; Codehighlighter1_543_811_Open_Text.style.display='none'; Codehighlighter1_543_811_Closed_Image.style.display='inline'; Codehighlighter1_543_811_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_543_811_Closed_Image onclick="this.style.display='none'; Codehighlighter1_543_811_Closed_Text.style.display='none'; Codehighlighter1_543_811_Open_Image.style.display='inline'; Codehighlighter1_543_811_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_543_811_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_543_811_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;assert(&nbsp;idx&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">&nbsp;value</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">len&nbsp;</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">&nbsp;idx&nbsp;</span><span style="COLOR: #000000">&gt;=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">);<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">if&nbsp;we&nbsp;are&nbsp;sharing&nbsp;value&nbsp;with&nbsp;other&nbsp;String&nbsp;Objects,<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">break&nbsp;off&nbsp;a&nbsp;separate&nbsp;copy&nbsp;of&nbsp;value&nbsp;for&nbsp;ourselves</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(&nbsp;value</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">refCount&nbsp;</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">&nbsp;)<br><img id=Codehighlighter1_719_783_Open_Image onclick="this.style.display='none'; Codehighlighter1_719_783_Open_Text.style.display='none'; Codehighlighter1_719_783_Closed_Image.style.display='inline'; Codehighlighter1_719_783_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_719_783_Closed_Image onclick="this.style.display='none'; Codehighlighter1_719_783_Closed_Text.style.display='none'; Codehighlighter1_719_783_Open_Image.style.display='inline'; Codehighlighter1_719_783_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_719_783_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_719_783_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;value</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">refCount</span><span style="COLOR: #000000">--</span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;value&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000">&nbsp;StringValue(value</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">data);<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;value</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">data[idx];<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">&nbsp;String::</span><span style="COLOR: #0000ff">operator</span><span style="COLOR: #000000">[](size_t&nbsp;idx)</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000"><br><img id=Codehighlighter1_862_930_Open_Image onclick="this.style.display='none'; Codehighlighter1_862_930_Open_Text.style.display='none'; Codehighlighter1_862_930_Closed_Image.style.display='inline'; Codehighlighter1_862_930_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_862_930_Closed_Image onclick="this.style.display='none'; Codehighlighter1_862_930_Closed_Text.style.display='none'; Codehighlighter1_862_930_Open_Image.style.display='inline'; Codehighlighter1_862_930_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_862_930_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_862_930_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;assert(&nbsp;idx&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">&nbsp;value</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">len&nbsp;</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">&nbsp;idx&nbsp;</span><span style="COLOR: #000000">&gt;=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">);<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;value</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">data[idx];<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">String::StringValue::StringValue(</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;cszStr)<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;:refCount(</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">)<br><img id=Codehighlighter1_1000_1075_Open_Image onclick="this.style.display='none'; Codehighlighter1_1000_1075_Open_Text.style.display='none'; Codehighlighter1_1000_1075_Closed_Image.style.display='inline'; Codehighlighter1_1000_1075_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_1000_1075_Closed_Image onclick="this.style.display='none'; Codehighlighter1_1000_1075_Closed_Text.style.display='none'; Codehighlighter1_1000_1075_Open_Image.style.display='inline'; Codehighlighter1_1000_1075_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_1000_1075_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_1000_1075_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;len&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;strlen(cszStr);<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;data&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">[len&nbsp;</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">];<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;strcpy(data,&nbsp;cszStr);<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">String::StringValue::</span><span style="COLOR: #000000">~</span><span style="COLOR: #000000">StringValue()<br><img id=Codehighlighter1_1114_1143_Open_Image onclick="this.style.display='none'; Codehighlighter1_1114_1143_Open_Text.style.display='none'; Codehighlighter1_1114_1143_Closed_Image.style.display='inline'; Codehighlighter1_1114_1143_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_1114_1143_Closed_Image onclick="this.style.display='none'; Codehighlighter1_1114_1143_Closed_Text.style.display='none'; Codehighlighter1_1114_1143_Open_Image.style.display='inline'; Codehighlighter1_1114_1143_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_1114_1143_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_1114_1143_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;delete[]&nbsp;data;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;data&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span></div>
<br>&nbsp;
<p><span>1</span><span>、关于成员函数</span><span>operator[]</span><span>的备注</span></p>
<blockquote style="MARGIN-RIGHT: 0px" dir=ltr>
<p><span>Unfortunately, there is no way for C++ compilers to tell us whether a particular use of operator[] is for a read or a write, so we must be pessimistic and assume that all calls to the non-const operator[] are for writes.</span></p>
<p><span>不幸的是，</span><span>C++</span><span>编译器没有办法告诉我们一个特定的</span><span>operator[]</span><span>是用作读的还是写的，所以我们必须保守地假设</span><span>&#8220;</span><span>所有</span><span>&#8221;</span><span>调用非</span><span>const operator[]</span><span>的行为都是为了写操作。</span></p>
</blockquote>
<p>&nbsp;</p>
<p><span>2</span><span>、这个版本未能解决的问题</span></p>
<blockquote style="MARGIN-RIGHT: 0px" dir=ltr>
<p><span>只有一个挥之不去的问题。看一下这样的代码：</span></p>
<p><span>String s1 = "Hello";</span></p>
<p><span>char *p = &amp;s1[1];</span></p>
<p><span>&nbsp;</span></p>
<p><span>现在看增加一条语句：</span></p>
<p><span>String s2 = s1;</span></p>
<p><span>String</span><span>的拷贝构造函数使得</span><span>s2</span><span>共享</span><span>s1</span><span>的</span><span>StringValue</span><span>对象，</span></p>
<p><span></span></p>
<p><span>下面这样的语句将有不受欢迎的结果：</span></p>
<p><span>*p = 'x';<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>// modifies both s1 and s2!</span></p>
<p><span>String</span><span>的拷贝构造函数没有办法检测这样的问题，因为它不知道指向</span><span>s1</span><span>拥有的</span><span>StringValue</span><span>对象的指针的存在，这样就导致了</span><span>s1</span><span>和</span><span>s2</span><span>同时被修改了。</span></p>
<p><span>并且，这个问题不局限于指针：它同样存在于有人保存了一个</span><span>String</span><span>的非</span><span>const operator[]</span><span>的返回值的引用的情况下。</span></p>
</blockquote><br><br>
<img src ="http://www.cppblog.com/mildcat1982/aggbug/119948.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/mildcat1982/" target="_blank">mildcat</a> 2010-07-10 10:32 <a href="http://www.cppblog.com/mildcat1982/articles/119948.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>EC++中一个限制类产生实例数的方法</title><link>http://www.cppblog.com/mildcat1982/articles/119732.html</link><dc:creator>mildcat</dc:creator><author>mildcat</author><pubDate>Thu, 08 Jul 2010 07:15:00 GMT</pubDate><guid>http://www.cppblog.com/mildcat1982/articles/119732.html</guid><wfw:comment>http://www.cppblog.com/mildcat1982/comments/119732.html</wfw:comment><comments>http://www.cppblog.com/mildcat1982/articles/119732.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/mildcat1982/comments/commentRss/119732.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/mildcat1982/services/trackbacks/119732.html</trackback:ping><description><![CDATA[<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #000000">#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">memory</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000">&nbsp;std;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">template</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">typename&nbsp;BeingCounted</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">&nbsp;Counted<br><img style="DISPLAY: inline" id=Codehighlighter1_107_325_Open_Image onclick="this.style.display='none'; Codehighlighter1_107_325_Open_Text.style.display='none'; Codehighlighter1_107_325_Closed_Image.style.display='inline'; Codehighlighter1_107_325_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_107_325_Closed_Image onclick="this.style.display='none'; Codehighlighter1_107_325_Closed_Text.style.display='none'; Codehighlighter1_107_325_Open_Image.style.display='inline'; Codehighlighter1_107_325_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_107_325_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span style="DISPLAY: inline" id=Codehighlighter1_107_325_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000">:<br><img id=Codehighlighter1_138_139_Open_Image onclick="this.style.display='none'; Codehighlighter1_138_139_Open_Text.style.display='none'; Codehighlighter1_138_139_Closed_Image.style.display='inline'; Codehighlighter1_138_139_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_138_139_Closed_Image onclick="this.style.display='none'; Codehighlighter1_138_139_Closed_Text.style.display='none'; Codehighlighter1_138_139_Open_Image.style.display='inline'; Codehighlighter1_138_139_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">&nbsp;TooManyObjects</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_138_139_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_138_139_Open_Text><span style="COLOR: #000000">{}</span></span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">static</span><span style="COLOR: #000000">&nbsp;size_t&nbsp;getObjectsNum();<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #0000ff">protected</span><span style="COLOR: #000000">:<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;Counted();<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;Counted(Counted</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">);<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #000000">~</span><span style="COLOR: #000000">Counted();<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #0000ff">private</span><span style="COLOR: #000000">:<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;init();<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #0000ff">private</span><span style="COLOR: #000000">:<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">static</span><span style="COLOR: #000000">&nbsp;size_t&nbsp;numObjects;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">static</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;size_t&nbsp;maxObjects;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">template</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">typename&nbsp;BeingCounted</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">size_t&nbsp;Counted</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">BeingCounted</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">::numObjects&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">template</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">typename&nbsp;BeingCounted</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">size_t&nbsp;Counted</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">BeingCounted</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">::getObjectsNum()<br><img id=Codehighlighter1_486_508_Open_Image onclick="this.style.display='none'; Codehighlighter1_486_508_Open_Text.style.display='none'; Codehighlighter1_486_508_Closed_Image.style.display='inline'; Codehighlighter1_486_508_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_486_508_Closed_Image onclick="this.style.display='none'; Codehighlighter1_486_508_Closed_Text.style.display='none'; Codehighlighter1_486_508_Open_Image.style.display='inline'; Codehighlighter1_486_508_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_486_508_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_486_508_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;numObjects;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">template</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">typename&nbsp;BeingCounted</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">Counted</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">BeingCounted</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">::Counted()<br><img id=Codehighlighter1_576_587_Open_Image onclick="this.style.display='none'; Codehighlighter1_576_587_Open_Text.style.display='none'; Codehighlighter1_576_587_Closed_Image.style.display='inline'; Codehighlighter1_576_587_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_576_587_Closed_Image onclick="this.style.display='none'; Codehighlighter1_576_587_Closed_Text.style.display='none'; Codehighlighter1_576_587_Open_Image.style.display='inline'; Codehighlighter1_576_587_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_576_587_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_576_587_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;init();<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">template</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">typename&nbsp;BeingCounted</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">Counted</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">BeingCounted</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">::Counted(Counted</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">)<br><img id=Codehighlighter1_663_674_Open_Image onclick="this.style.display='none'; Codehighlighter1_663_674_Open_Text.style.display='none'; Codehighlighter1_663_674_Closed_Image.style.display='inline'; Codehighlighter1_663_674_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_663_674_Closed_Image onclick="this.style.display='none'; Codehighlighter1_663_674_Closed_Text.style.display='none'; Codehighlighter1_663_674_Open_Image.style.display='inline'; Codehighlighter1_663_674_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_663_674_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_663_674_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;init();<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">template</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">typename&nbsp;BeingCounted</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">Counted</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">BeingCounted</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">::</span><span style="COLOR: #000000">~</span><span style="COLOR: #000000">Counted()<br><img id=Codehighlighter1_743_760_Open_Image onclick="this.style.display='none'; Codehighlighter1_743_760_Open_Text.style.display='none'; Codehighlighter1_743_760_Closed_Image.style.display='inline'; Codehighlighter1_743_760_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_743_760_Closed_Image onclick="this.style.display='none'; Codehighlighter1_743_760_Closed_Text.style.display='none'; Codehighlighter1_743_760_Open_Image.style.display='inline'; Codehighlighter1_743_760_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_743_760_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_743_760_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #000000">--</span><span style="COLOR: #000000">numObjects;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">template</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">typename&nbsp;BeingCounted</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;Counted</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">BeingCounted</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">::init()<br><img id=Codehighlighter1_830_902_Open_Image onclick="this.style.display='none'; Codehighlighter1_830_902_Open_Text.style.display='none'; Codehighlighter1_830_902_Closed_Image.style.display='inline'; Codehighlighter1_830_902_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_830_902_Closed_Image onclick="this.style.display='none'; Codehighlighter1_830_902_Closed_Text.style.display='none'; Codehighlighter1_830_902_Open_Image.style.display='inline'; Codehighlighter1_830_902_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_830_902_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_830_902_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(&nbsp;numObjects&nbsp;</span><span style="COLOR: #000000">&gt;=</span><span style="COLOR: #000000">&nbsp;maxObjects&nbsp;)</span><span style="COLOR: #0000ff">throw</span><span style="COLOR: #000000">&nbsp;TooManyObjects();<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">numObjects;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000"><br><img id=Codehighlighter1_904_951_Open_Image onclick="this.style.display='none'; Codehighlighter1_904_951_Open_Text.style.display='none'; Codehighlighter1_904_951_Closed_Image.style.display='inline'; Codehighlighter1_904_951_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_904_951_Closed_Image onclick="this.style.display='none'; Codehighlighter1_904_951_Closed_Text.style.display='none'; Codehighlighter1_904_951_Open_Image.style.display='inline'; Codehighlighter1_904_951_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_904_951_Closed_Text>/**/</span><span id=Codehighlighter1_904_951_Open_Text><span style="COLOR: #808080">/////////////////////////////////////////////</span><span style="COLOR: #008000">//</span><span style="COLOR: #808080"></span></span><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">&nbsp;Printer:</span><span style="COLOR: #0000ff">private</span><span style="COLOR: #000000">&nbsp;Counted</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">Printer</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br><img id=Codehighlighter1_992_1274_Open_Image onclick="this.style.display='none'; Codehighlighter1_992_1274_Open_Text.style.display='none'; Codehighlighter1_992_1274_Closed_Image.style.display='inline'; Codehighlighter1_992_1274_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_992_1274_Closed_Image onclick="this.style.display='none'; Codehighlighter1_992_1274_Closed_Text.style.display='none'; Codehighlighter1_992_1274_Open_Image.style.display='inline'; Codehighlighter1_992_1274_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_992_1274_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_992_1274_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000">:<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000">&nbsp;Counted</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">Printer</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">::getObjectsNum;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000">&nbsp;Counted</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">Printer</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">::TooManyObjects;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000">:<br><img id=Codehighlighter1_1124_1144_Open_Image onclick="this.style.display='none'; Codehighlighter1_1124_1144_Open_Text.style.display='none'; Codehighlighter1_1124_1144_Closed_Image.style.display='inline'; Codehighlighter1_1124_1144_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_1124_1144_Closed_Image onclick="this.style.display='none'; Codehighlighter1_1124_1144_Closed_Text.style.display='none'; Codehighlighter1_1124_1144_Open_Image.style.display='inline'; Codehighlighter1_1124_1144_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">static</span><span style="COLOR: #000000">&nbsp;Printer</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;make_aPrinter()</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_1124_1144_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_1124_1144_Open_Text><span style="COLOR: #000000">{</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000">&nbsp;Printer;}</span></span><span style="COLOR: #000000"><br><img id=Codehighlighter1_1193_1218_Open_Image onclick="this.style.display='none'; Codehighlighter1_1193_1218_Open_Text.style.display='none'; Codehighlighter1_1193_1218_Closed_Image.style.display='inline'; Codehighlighter1_1193_1218_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_1193_1218_Closed_Image onclick="this.style.display='none'; Codehighlighter1_1193_1218_Closed_Text.style.display='none'; Codehighlighter1_1193_1218_Open_Image.style.display='inline'; Codehighlighter1_1193_1218_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">static</span><span style="COLOR: #000000">&nbsp;Printer</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;make_aCopy(</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;Printer</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">&nbsp;rhs)</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_1193_1218_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_1193_1218_Open_Text><span style="COLOR: #000000">{</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000">&nbsp;Printer(rhs);}</span></span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #0000ff">private</span><span style="COLOR: #000000">:<br><img id=Codehighlighter1_1240_1241_Open_Image onclick="this.style.display='none'; Codehighlighter1_1240_1241_Open_Text.style.display='none'; Codehighlighter1_1240_1241_Closed_Image.style.display='inline'; Codehighlighter1_1240_1241_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_1240_1241_Closed_Image onclick="this.style.display='none'; Codehighlighter1_1240_1241_Closed_Text.style.display='none'; Codehighlighter1_1240_1241_Open_Image.style.display='inline'; Codehighlighter1_1240_1241_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;Printer()</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_1240_1241_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_1240_1241_Open_Text><span style="COLOR: #000000">{}</span></span><span style="COLOR: #000000"><br><img id=Codehighlighter1_1271_1272_Open_Image onclick="this.style.display='none'; Codehighlighter1_1271_1272_Open_Text.style.display='none'; Codehighlighter1_1271_1272_Closed_Image.style.display='inline'; Codehighlighter1_1271_1272_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_1271_1272_Closed_Image onclick="this.style.display='none'; Codehighlighter1_1271_1272_Closed_Text.style.display='none'; Codehighlighter1_1271_1272_Open_Image.style.display='inline'; Codehighlighter1_1271_1272_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;Printer(</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;Printer</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">&nbsp;rhs)</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_1271_1272_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_1271_1272_Open_Text><span style="COLOR: #000000">{}</span></span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">template</span><span style="COLOR: #000000">&lt;&gt;</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;size_t&nbsp;Counted</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">Printer</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">::maxObjects&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">4</span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()<br><img id=Codehighlighter1_1348_1874_Open_Image onclick="this.style.display='none'; Codehighlighter1_1348_1874_Open_Text.style.display='none'; Codehighlighter1_1348_1874_Closed_Image.style.display='inline'; Codehighlighter1_1348_1874_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_1348_1874_Closed_Image onclick="this.style.display='none'; Codehighlighter1_1348_1874_Closed_Text.style.display='none'; Codehighlighter1_1348_1874_Open_Image.style.display='inline'; Codehighlighter1_1348_1874_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_1348_1874_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_1348_1874_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">try</span><span style="COLOR: #000000"><br><img id=Codehighlighter1_1356_1695_Open_Image onclick="this.style.display='none'; Codehighlighter1_1356_1695_Open_Text.style.display='none'; Codehighlighter1_1356_1695_Closed_Image.style.display='inline'; Codehighlighter1_1356_1695_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_1356_1695_Closed_Image onclick="this.style.display='none'; Codehighlighter1_1356_1695_Closed_Text.style.display='none'; Codehighlighter1_1356_1695_Open_Image.style.display='inline'; Codehighlighter1_1356_1695_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_1356_1695_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_1356_1695_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;auto_ptr</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">Printer</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">&nbsp;p1(&nbsp;Printer::make_aPrinter()&nbsp;);<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;auto_ptr</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">Printer</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">&nbsp;p2(&nbsp;Printer::make_aPrinter()&nbsp;);<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;auto_ptr</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">Printer</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">&nbsp;p3(&nbsp;Printer::make_aPrinter()&nbsp;);<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;auto_ptr</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">Printer</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">&nbsp;p5(&nbsp;Printer::make_aPrinter()&nbsp;);<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">current&nbsp;object&nbsp;number:&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;Printer::getObjectsNum()&nbsp;</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;endl;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;auto_ptr</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">Printer</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">&nbsp;p4(&nbsp;Printer::make_aCopy(&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">p1&nbsp;)&nbsp;);<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">catch</span><span style="COLOR: #000000">(&nbsp;Counted</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">Printer</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">::TooManyObjects</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">&nbsp;exp&nbsp;)<br><img id=Codehighlighter1_1746_1789_Open_Image onclick="this.style.display='none'; Codehighlighter1_1746_1789_Open_Text.style.display='none'; Codehighlighter1_1746_1789_Closed_Image.style.display='inline'; Codehighlighter1_1746_1789_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_1746_1789_Closed_Image onclick="this.style.display='none'; Codehighlighter1_1746_1789_Closed_Text.style.display='none'; Codehighlighter1_1746_1789_Open_Image.style.display='inline'; Codehighlighter1_1746_1789_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_1746_1789_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_1746_1789_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">exception&nbsp;happened</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;endl;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">current&nbsp;object&nbsp;number:&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;Printer::getObjectsNum()&nbsp;</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;endl;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span></div>
<img src ="http://www.cppblog.com/mildcat1982/aggbug/119732.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/mildcat1982/" target="_blank">mildcat</a> 2010-07-08 15:15 <a href="http://www.cppblog.com/mildcat1982/articles/119732.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>