﻿<?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++博客-酸菜猪蹄的程序人生-文章分类-C++心得</title><link>http://www.cppblog.com/cooleaf/category/740.html</link><description>木下编程屯屯烫烫</description><language>zh-cn</language><lastBuildDate>Fri, 23 May 2008 11:04:44 GMT</lastBuildDate><pubDate>Fri, 23 May 2008 11:04:44 GMT</pubDate><ttl>60</ttl><item><title>C++成员函数指针的使用</title><link>http://www.cppblog.com/cooleaf/articles/2471.html</link><dc:creator>cooelaf</dc:creator><author>cooelaf</author><pubDate>Fri, 06 Jan 2006 09:48:00 GMT</pubDate><guid>http://www.cppblog.com/cooleaf/articles/2471.html</guid><wfw:comment>http://www.cppblog.com/cooleaf/comments/2471.html</wfw:comment><comments>http://www.cppblog.com/cooleaf/articles/2471.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/cooleaf/comments/commentRss/2471.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/cooleaf/services/trackbacks/2471.html</trackback:ping><description><![CDATA[<DIV>下面是例子，如果对C的函数指针有一定体会的话，相信这个例子很容易就看明白了。（其实是一样的。）</DIV>
<DIV>
<HR>
</DIV>
<DIV>#include &lt;iostream&gt;<BR>#include &lt;string&gt;<BR>using namespace std;<BR>class CPerson;<BR>typedef void (CPerson::*PF)(const CPerson&amp;);<BR>class CPerson<BR>{<BR>private:<BR>&nbsp;&nbsp; string name;<BR>public:<BR>&nbsp;&nbsp; CPerson(const string&amp; name);<BR>&nbsp;&nbsp; void Slap(const CPerson&amp; p);<BR>&nbsp;&nbsp; void Kick(const CPerson&amp; p);<BR>};<BR></DIV>
<DIV><BR>CPerson::CPerson(const string&amp; name)<BR>{<BR>&nbsp;&nbsp; this-&gt;name = name;<BR>}</DIV>
<DIV>void CPerson::Slap(const CPerson&amp; p)<BR>{<BR>&nbsp;&nbsp; cout &lt;&lt; this-&gt;name &lt;&lt; " slap " &lt;&lt; p.name &lt;&lt; endl;<BR>}</DIV>
<DIV>void CPerson::Kick(const CPerson&amp; p)<BR>{<BR>&nbsp;&nbsp; cout &lt;&lt; this-&gt;name &lt;&lt; " kick " &lt;&lt; p.name &lt;&lt; endl;<BR>}</DIV>
<DIV>int main()<BR>{<BR>&nbsp;&nbsp; CPerson me("Cooleaf");<BR>&nbsp;&nbsp; CPerson you("dummy");<BR>&nbsp;&nbsp; int i = 0;<BR>&nbsp;&nbsp; PF p[2];<BR>&nbsp;&nbsp; p[0] = CPerson::Slap;<BR>&nbsp;&nbsp; p[1] = CPerson::Kick;<BR>&nbsp;&nbsp; cin &gt;&gt; i;<BR>&nbsp;&nbsp; while (i != -1)<BR>&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (i &gt; 1)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; i = 1;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (i &lt; 0)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; i = 0;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (me.*p[i])(you);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cin &gt;&gt; i;<BR>&nbsp;&nbsp; }<BR>&nbsp;&nbsp; system("pause");<BR>&nbsp;&nbsp; return 0;</DIV>
<DIV>}</DIV><img src ="http://www.cppblog.com/cooleaf/aggbug/2471.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/cooleaf/" target="_blank">cooelaf</a> 2006-01-06 17:48 <a href="http://www.cppblog.com/cooleaf/articles/2471.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>