Revision: 292 Author: totti19851101 Date: Mon Aug 24 20:19:14 2009 Log: [No log message] http://code.google.com/p/boost-doc-zh/source/detail?r=292 Modified: /trunk/doc/html/boost_asio/tutorial/tuttimer4.html =======================================--- /trunk/doc/html/boost_asio/tutorial/tuttimer4.html Wed May 27 03:09:50 2009 +++ /trunk/doc/html/boost_asio/tutorial/tuttimer4.html Mon Aug 24 20:19:14 2009
@@ -1,6 +1,6 @@ <html> <head> -<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> +<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Timer.4 - Using a member function as a handler</title> <link rel="stylesheet" href="../../boostbook.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.74.3"> @@ -24,13 +24,10 @@ </div> <div class="section" lang="en"> <div class="titlepage"><div><div><h3 class="title">-<a name="boost_asio.tutorial.tuttimer4"></a><a class="link" href="tuttimer4.html" title="Timer.4 - Using a member function as a handler"> Timer.4 - Using a member
- function as a handler</a>+<a name="boost_asio.tutorial.tuttimer4"></a><a class="link" href="tuttimer4.html" title="Timer.4 - Using a member function as a handler"> Timer.4 - 成员函数作为回调函数</a>
</h3></div></div></div> <p>- In this tutorial we will see how to use a class member function as a callback - handler. The program should execute identically to the tutorial program from
- tutorial Timer.3.+ 在本例中我们将看到怎样使用类成员函数作为回调句柄。程序完成和 Timer.3完全同样的功能。
</p><pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">iostream</span><span class="special">></span> <span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">asio</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
@@ -38,19 +35,15 @@<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">date_time</span><span class="special">/</span><span class="identifier">posix_time</span><span class="special">/</span><span class="identifier">posix_time</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
</pre> <p>- Instead of defining a free function <code class="computeroutput"><span class="identifier">print</span></code> - as the callback handler, as we did in the earlier tutorial programs, we now - define a class called <code class="computeroutput"><span class="identifier">printer</span></code>. + 我们现在声明一个名为<code class="computeroutput"><span class="identifier">printer</span></code>的类来取代前个例子程序中的声明的作为 回调句柄的自由函数<code class="computeroutput"><span class="identifier">print</span></code>。
</p><pre class="programlisting"><span class="keyword">class</span> <span class="identifier">printer</span>
<span class="special">{</span> <span class="keyword">public</span><span class="special">:</span> </pre> <p>- The constructor of this class will take a reference to the io_service object - and use it when initialising the <code class="computeroutput"><span class="identifier">timer_</span></code> - member. The counter used to shut down the program is now also a member of
- the class.+ 这个类的构造函数使用一个io_service对象的引用来初始化<code class="computeroutput"><span class="identifier">timer_</span></code>
+ 成员变量。用来控制关闭程序的计数器现在也是类的一个成员变量。 </p><pre class="programlisting"> <span class="identifier">printer</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">io_service</span><span class="special">&</span> <span class="identifier">io</span><span class="special">)</span> <span class="special">:</span> <span class="identifier">timer_</span><span class="special">(</span><span class="identifier">io</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">posix_time</span><span class="special">::</span><span class="identifier">seconds</span><span class="special">(</span><span class="number">1</span><span class="special">)),</span>
@@ -58,24 +51,19 @@ <span class="special">{</span> </pre> <p>- The boost::bind() function works just as well with class member functions - as with free functions. Since all non-static class member functions have - an implicit <code class="computeroutput"><span class="keyword">this</span></code> parameter, - we need to bind <code class="computeroutput"><span class="keyword">this</span></code> to the - function. As in tutorial Timer.3, boost::bind() converts our callback handler - (now a member function) into a function object that can be invoked as though - it has the signature <code class="computeroutput"><span class="keyword">void</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">system</span><span class="special">::</span><span class="identifier">error_code</span><span class="special">&)</span></code>. + 正如自由函数一样,boost::bind() 也可以很好地用在类成员函数上。所有 的non-static 成员函量都有一个隐式的this指针参数, + 我们需要把它绑定到函数上。就像在示例Timer.3中,boost::bind() 使我们 的回调函数(现在是一个成员函数) + 转变成一个签名为void(const boost::system::error_code&)、且可被调用 的函数对象。
</p> <p>- You will note that the boost::asio::placeholders::error placeholder is not - specified here, as the <code class="computeroutput"><span class="identifier">print</span></code>
- member function does not accept an error object as a parameter.+ 你可能注意到在这里我们并没有指定boost::asio::placeholders::error 占 位符,这是因为<code class="computeroutput"><span class="identifier">print</span></code>
+ 成员函数并不接受一个error 对象作为参数。 </p><pre class="programlisting"> <span class="identifier">timer_</span><span class="special">.</span><span class="identifier">async_wait</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">bind</span><span class="special">(&</span><span class="identifier">printer</span><span class="special">::</span><span class="identifier">print</span><span class="special">,</span> <span class="keyword">this</span><span class="special">));</span>
<span class="special">}</span> </pre> <p>- In the class destructor we will print out the final value of the counter.
+ 在类的析构函数中我们将打印计数器变量的最终值。 </p><pre class="programlisting"> <span class="special">~</span><span class="identifier">printer</span><span class="special">()</span>
<span class="special">{</span> @@ -83,10 +71,8 @@ <span class="special">}</span> </pre> <p>- The <code class="computeroutput"><span class="identifier">print</span></code> member function - is very similar to the <code class="computeroutput"><span class="identifier">print</span></code> - function from tutorial Timer.3, except that it now operates on the class - data members instead of having the timer and counter passed in as parameters. + 这个<code class="computeroutput"><span class="identifier">print</span></code>成员函数同示例Timer.3中的<code class="computeroutput"><span class="identifier">print</span></code> + 函数很相似,唯一不同的是它现在操作类的数据成员而不是作为参数传递来 的定时器和计数器。
</p><pre class="programlisting"> <span class="keyword">void</span> <span class="identifier">print</span><span class="special">()</span>
<span class="special">{</span> @@ -106,9 +92,8 @@ <span class="special">};</span> </pre> <p>- The <code class="computeroutput"><span class="identifier">main</span></code> function is much - simpler than before, as it now declares a local <code class="computeroutput"><span class="identifier">printer</span></code>
- object before running the io_service as normal.+ <code class="computeroutput"><span class="identifier">main</span></code>函数比前面简单多了。像平常一样运行 io_service对象之前,它现在需要声明一个局部的<code class="computeroutput"><span class="identifier">printer</span></code>
+ 对象。 </p><pre class="programlisting"><span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span>
<span class="special">{</span> @@ -120,18 +105,16 @@ <span class="special">}</span> </pre> <p>- See the <a class="link" href="tuttimer4/src.html" title="Source listing for Timer.4">full source listing</a> + 查看<a class="link" href="tuttimer4/src.html" title="Source listing for Timer.4">本例全部源代码</a>
</p> <p>- Return to the <a class="link" href="../tutorial.html" title="Tutorial">tutorial index</a> + 返回<a class="link" href="../tutorial.html" title="Tutorial">指南 </a>
</p> <p>- Previous: <a class="link" href="tuttimer3.html" title="Timer.3 - Binding arguments to a handler">Timer.3 - Binding
- arguments to a handler</a>+ 前一例: <a class="link" href="tuttimer3.html" title="Timer.3 - Binding arguments to a handler">Timer.3 - 回调函数绑定参数</a>
</p> <p>- Next: <a class="link" href="tuttimer5.html" title="Timer.5 - Synchronising handlers in multithreaded programs">Timer.5 - Synchronising
- handlers in multithreaded programs</a>+ 下一例: <a class="link" href="tuttimer5.html" title="Timer.5 - Synchronising handlers in multithreaded programs">Timer.5 - 多线程同步回调 </a>
</p> </div><table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision"; width="100%"><tr>