<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>绑定 on I'm OWenT</title><link>//owent.net/tags/%E7%BB%91%E5%AE%9A.html</link><description>Recent content in 绑定 on I'm OWenT</description><generator>Hugo</generator><language>zh-cn</language><copyright>&lt;a rel="license" href="https://github.com/owent/blog-hugo/blob/master/LICENSE.md"&gt;&lt;img alt="知识共享许可协议" style="border-width:0" src="https://i.creativecommons.org/l/by-nc-sa/4.0/80x15.png" /&gt;&lt;/a&gt;</copyright><lastBuildDate>Fri, 10 Feb 2012 09:52:37 +0000</lastBuildDate><atom:link href="//owent.net/tags/%E7%BB%91%E5%AE%9A/index.xml" rel="self" type="application/rss+xml"/><item><title>C++ 新特性学习（四） — Bind和Function</title><link>//owent.net/2012/536.html</link><pubDate>Fri, 10 Feb 2012 09:52:37 +0000</pubDate><guid>//owent.net/2012/536.html</guid><description>&lt;!-- toc --&gt;&#13;&#10;&lt;p&gt;绑定函数是我认为C++新标准里第二有用的库了&#10;绑定库的使用环境是：&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;GCC-C++ 4.3 及以上&lt;/li&gt;&#10;&lt;li&gt;Visual Studio 2008 SP1 及以上&lt;/li&gt;&#10;&lt;li&gt;BOOST 1.25及以上（其中function是1.16及以上）&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;先来看一段代码&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-cpp"&gt;#include &amp;lt;iostream&amp;gt;&#13;&#10;#include &amp;lt;cstdio&amp;gt;&#13;&#10;#include &amp;lt;memory&amp;gt;&#13;&#10;#include &amp;lt;functional&amp;gt;&#13;&#10;&#13;&#10;class button&#13;&#10;{&#13;&#10;public:&#13;&#10;&#13;&#10; std::function&amp;lt;void(int)&amp;gt; onClick;&#13;&#10;};&#13;&#10;&#13;&#10;class player&#13;&#10;{&#13;&#10;public:&#13;&#10;&#13;&#10; void play(void* sender, int param) {&#13;&#10; printf(&amp;#34;Play: %d =&amp;gt; %d\n&amp;#34;, (int)sender, param);&#13;&#10; };&#13;&#10; void stop(void* sender, int param) {&#13;&#10; printf(&amp;#34;Play: %d =&amp;gt; %d\n&amp;#34;, (int)sender, param);&#13;&#10; };&#13;&#10;};&#13;&#10;&#13;&#10;button playButton, stopButton;&#13;&#10;player thePlayer;&#13;&#10;&#13;&#10;void connect()&#13;&#10;{&#13;&#10; playButton.onClick = std::bind(&amp;amp;player::play, &amp;amp;thePlayer, &amp;amp;playButton, std::placeholders::_1);&#13;&#10; stopButton.onClick = std::bind(&amp;amp;player::stop, &amp;amp;thePlayer, &amp;amp;stopButton, std::placeholders::_1);&#13;&#10;}&#13;&#10;&#13;&#10;int main () {&#13;&#10; connect();&#13;&#10; playButton.onClick(0);&#13;&#10; return 0;&#13;&#10;}&#13;&#10;// 以上代码参考boost中bind库示例代码，在G&amp;#43;&amp;#43; 4.6.1中测试通过&lt;/code&gt;&lt;/pre&gt;&#13;&#10;&lt;p&gt;木有错，这是C++，并且很方便地实现了委托&#10;这就是传说中的绑定库和增强型的函数对象&#10;接下来一个一个来&lt;/p&gt;</description></item></channel></rss>