﻿<?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++博客-传说中的菜鸟</title><link>http://www.cppblog.com/shixiyu/</link><description>。。。。。</description><language>zh-cn</language><lastBuildDate>Sat, 04 Apr 2026 20:28:31 GMT</lastBuildDate><pubDate>Sat, 04 Apr 2026 20:28:31 GMT</pubDate><ttl>60</ttl><item><title>C++回调函数用法</title><link>http://www.cppblog.com/shixiyu/archive/2012/08/14/187171.html</link><dc:creator>爱走小路</dc:creator><author>爱走小路</author><pubDate>Tue, 14 Aug 2012 06:56:00 GMT</pubDate><guid>http://www.cppblog.com/shixiyu/archive/2012/08/14/187171.html</guid><wfw:comment>http://www.cppblog.com/shixiyu/comments/187171.html</wfw:comment><comments>http://www.cppblog.com/shixiyu/archive/2012/08/14/187171.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/shixiyu/comments/commentRss/187171.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/shixiyu/services/trackbacks/187171.html</trackback:ping><description><![CDATA[<p style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; "><span style="font-size: small; color: #000000; font-family: 黑体; ">一回调函数</span></p><p style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; "><span style="font-size: small; color: #000000; font-family: 'Times New Roman'; ">我们经常在C++设计时通过使用回调函数可以使有些应用（如定时器事件回调处理、用回调函数记录某操作进度等）变得非常方便和符合逻辑，那么它的内在机制如何呢，怎么定义呢?它和其它函数（比如钩子函数）有何不同呢？</span></p><p style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; "><span style="font-size: small; color: #000000; font-family: 'Times New Roman'; ">使用回调函数实际上就是在调用某个函数（通常是API函数）时，将自己的一个函数（这个函数为回调函数）的地址作为参数传递给那个函数。</span></p><p style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; "><span style="font-size: small; color: #000000; font-family: 'Times New Roman'; ">而 那个函数在需要的时候，利用传递的地址调用回调函数，这时你可以利用这个机会在回调函数中处理消息或完成一定的操作。至于如何定义回调函数，跟具体使用的 API函数有关，一般在帮助中有说明回调函数的参数和返回值等。C++中一般要求在回调函数前加CALLBACK（相当于FAR PASCAL），这主要是说明该函数的调用方式。</span></p><p style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; "><span style="font-size: small; color: #000000; font-family: 'Times New Roman'; ">至于钩子函数，只是回调函数的一个特例。习惯上把与SetWindowsHookEx函数一起使用的回调函数称为钩子函数。也有人把利用VirtualQueryEx安装的函数称为钩子函数，不过这种叫法不太流行。</span></p><p style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; "><span style="font-size: small; color: #000000; font-family: 'Times New Roman'; ">也可以这样，更容易理解：回调函数就好像是一个中断处理函数，系统在符合你设定的条件时自动调用。为此，你需要做三件事：</span></p><p style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; "><span style="font-size: small; color: #000000; font-family: 'Times New Roman'; ">1.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 声明；</span></p><p style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; "><span style="font-size: small; color: #000000; font-family: 'Times New Roman'; ">2.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 定义；</span></p><p style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; "><span style="font-size: small; color: #000000; font-family: 'Times New Roman'; ">3.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #ff0000; ">设置触发条件，就是在你的函数中把你的回调函数名称转化为地址作为一个参数，以便于系统调用。</span></span></p><p style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; "><span style="font-size: small; color: #000000; font-family: 'Times New Roman'; ">声明和定义时应注意：<span style="color: #ff0000; ">回调函数可以作为类的成员函数，但必须声明为静态方法。</span></span></p><p style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; "><span style="font-size: small; font-family: 'Times New Roman'; "><span style="color: #000000; ">二<span style="font-family: 黑体; ">回调函数、消息和事件例程</span></span></span></p><p style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; "><span style="font-size: small; "><br /></span><span style="font-size: small; color: #000000; font-family: 'Times New Roman'; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 调用(calling)机制从汇编时代起已经大量使用：准备一段现成的代码，调用者可以随时跳转至此段代码的起始地址，执行完后再返回跳转时的后续地址。 CPU为此准备了现成的调用指令，调用时可以压栈保护现场，调用结束后从堆栈中弹出现场地址，以便自动返回。借堆栈保护现场真是一项绝妙的发明，它使调用 者和被调者可以互不相识，于是才有了后来的函数和构件。</span></p><p style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; "><span style="font-size: small; color: #000000; font-family: 'Times New Roman'; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 此调用机制并非完美。回调函数就是一例。函数之类本是为调用者准备的美餐，其烹制者应对食客了如指掌，但实情并非如此。例如，写一个快速排序函数供他人调 用，其中必包含比较大小。麻烦来了：此时并不知要比较的是何类数据--整数、浮点数、字符串？于是只好为每类数据制作一个不同的排序函数。更通行的办法是 在函数参数中列一个回调函数地址，并通知调用者：君需自己准备一个比较函数，其中包含两个指针类参数，函数要比较此二指针所指数据之大小，并由函数返回值 说明比较结果。排序函数借此调用者提供的函数来比较大小，借指针传递参数，可以全然不管所比较的数据类型。被调用者回头调用调用者的函数（够咬嘴的），故 称其为回调（callback）。</span></p><p style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; "><span style="font-size: small; color: #000000; font-family: 'Times New Roman'; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 回调函数使程序结构乱了许多。Windows API 函数集中有不少回调函数，尽管有详尽说明，仍使初学者一头雾水。恐怕这也是无奈之举。</span></p><p style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; "><span style="font-size: small; color: #000000; font-family: 'Times New Roman'; ">无论何种事物，能以树形结构单向描述毕竟让人舒服些。如果某家族中孙辈又是某祖辈的祖辈，恐怕无人能理清其中的头绪。但数据处理之复杂往往需要构成网状结构，非简单的客户/服务器关系能穷尽。</span></p><p style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; "><span style="font-size: small; color: #000000; font-family: 'Times New Roman'; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Windows 系统还包含着另一种更为广泛的回调机制，即消息机制。消息本是 Windows 的基本控制手段，乍看与函数调用无关，其实是一种变相的函数调用。发送消息的目的是通知收方运行一段预先准备好的代码，相当于调用一个函数。消息所附带的 WParam 和 LParam 相当于函数的参数，只不过比普通参数更通用一些。应用程序可以主动发送消息，更多情况下是坐等 Windows 发送消息。一旦消息进入所属消息队列，便检感兴趣的那些，跳转去执行相应的消息处理代码。操作系统本是为应用程序服务，由应用程序来调用。而应用程序一旦 启动，却要反过来等待操作系统的调用。这分明也是一种回调，或者说是一种广义回调。其实，应用程序之间也可以形成这种回调。假如进程 B 收到进程 A 发来的消息，启动了一段代码，其中又向进程 A 发送消息，这就形成了回调。这种回调比较隐蔽，弄不好会搞成递归调用，若缺少终止条件，将会循环不已，直至把程序搞垮。若是故意编写成此递归调用，并设好 终止条件，倒是很有意思。但这种程序结构太隐蔽，除非十分必要，还是不用为好。</span></p><p style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; "><span style="font-size: small; color: #000000; font-family: 'Times New Roman'; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 利用消息也可以构成狭义回调。上面所举排序函数一例，可以把回调函数地址换成窗口 handle。如此，当需要比较数据大小时，不是去调用回调函数，而是借 API 函数 SendMessage 向指定窗口发送消息。收到消息方负责比较数据大小，把比较结果通过消息本身的返回值传给消息发送方。所实现的功能与回调函数并无不同。当然，此例中改为消 息纯属画蛇添脚，反倒把程序搞得很慢。但其他情况下并非总是如此，特别是需要异步调用时，发送消息是一种不错的选择。假如回调函数中包含文件处理之类的低 速处理，调用方等不得，需要把同步调用改为异步调用，去启动一个单独的线程，然后马上执行后续代码，其余的事让线程慢慢去做。一个替代办法是借 API 函数 PostMessage 发送一个异步消息，然后立即执行后续代码。这要比自己搞个线程省事许多，而且更安全。</span></p><p style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; "><span style="font-size: small; color: #000000; font-family: 'Times New Roman'; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 如今我们是活在一个 object 时代。只要与编程有关，无论何事都离不开 object。但 object 并未消除回调，反而把它发扬光大，弄得到处都是，只不过大都以事件（event）的身份出现，镶嵌在某个结构之中，显得更正统，更容易被人接受。应用程序 要使用某个构件，总要先弄清构件的属性、方法和事件，然后给构件属性赋值，在适当的时候调用适当的构件方法，还要给事件编写处理例程，以备构件代码来调 用。何谓事件？它不过是一个指向事件例程的地址，与回调函数地址没什么区别。</span></p><p style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; "><span style="font-size: small; color: #000000; font-family: 'Times New Roman'; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 不过，此种回调方式比传统回调函数要高明许多。首先，它把让人不太舒服的回调函数变成一种自然而然的处理例程，使编程者顿觉气顺。再者，地址是一个危险的 东西，用好了可使程序加速，用不好处处是陷阱，程序随时都会崩溃。现代编程方式总是想法把地址隐藏起来（隐藏比较彻底的如 VB 和 Java），其代价是降低了程序效率。事件例程（？）使编程者无需直接操作地址，但并不会使程序减速。<br />（例程似乎是进程的台湾翻译。）</span></p><p style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; "><span style="font-size: small; color: #000000; font-family: 黑体; ">三精妙比喻:回调函数还真有点像您随身带的<span style="font-family: 'Times New Roman'; ">BP<span style="font-family: 黑体; ">机：告诉别人号码，在它有事情时</span>Call</span>您。</span></p><p style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; "><span style="font-size: small; color: #000000; font-family: 'Times New Roman'; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 回调用于层间协作，上层将本层函数安装在下层，这个函数就是回调，而下层在一定条件下触发回调，例如作为一个驱动，是一个底层，他在收到一个数据时，除了 完成本层的处理工作外，还将进行回调，将这个数据交给上层应用层来做进一步处理，这在分层的数据通信中很普遍。其实回调和API非常接近，他们的共性都是 跨层调用的函数。但区别是API是低层提供给高层的调用，一般这个函数对高层都是已知的；而回调正好相反，他是高层提供给底层的调用，对于低层他是未知 的，必须由高层进行安装，这个安装函数其实就是一个低层提供的API，安装后低层不知道这个回调的名字，但它通过一个函数指针来保存这个回调，在需要调用 时，只需引用这个函数指针和相关的参数指针。&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 其实：回调就是该函数写在高层，低层通过一个函数指针保存这个函数，在某个事件的触发下，低层通过该函数指针调用高层那个函数。</span></p><p style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; "><span style="font-size: small; color: #000000; font-family: 'Times New Roman'; ">四调用方式<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 软件模块之间总是存在着一定的接口，从调用方式上，可以把他们分为三类：同步调用、回调和异步调用。同步调用是一种阻塞式调用，调用方要等待对方执行完毕 才返回，它是一种单向调用；回调是一种双向调用模式，也就是说，被调用方在接口被调用时也会调用对方的接口；异步调用是一种类似消息或事件的机制，不过它 的调用方向刚好相反，接口的服务在收到某种讯息或发生某种事件时，会主动通知客户方（即调用客户方的接口）。回调和异步调用的关系非常紧密，通常我们使用 回调来实现异步消息的注册，通过异步调用来实现消息的通知。同步调用是三者当中最简单的，而回调又常常是异步调用的基础。<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 对于不同类型的语言（如结构化语言和对象语言）、平台（Win32、JDK）或构架（CORBA、DCOM、WebService），客户和服务的交互除 了同步方式以外，都需要具备一定的异步通知机制，让服务方（或接口提供方）在某些情况下能够主动通知客户，而回调是实现异步的一个最简捷的途径。</span></p><p style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; "><span style="font-size: small; color: #000000; font-family: 'Times New Roman'; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 对于一般的结构化语言，可以通过回调函数来实现回调。回调函数也是一个函数或过程，不过它是一个由调用方自己实现，供被调用方使用的特殊函数。</span></p><p style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; "><span style="font-size: small; color: #000000; font-family: 'Times New Roman'; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 在面向对象的语言中，回调则是通过接口或抽象类来实现的，我们把实现这种接口的类成为回调类，回调类的对象成为回调对象。对于象C++或Object Pascal这些兼容了过程特性的对象语言，不仅提供了回调对象、回调方法等特性，也能兼容过程语言的回调函数机制。</span></p><p style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; "><span style="font-size: small; color: #000000; font-family: 'Times New Roman'; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Windows平台的消息机制也可以看作是回调的一种应用，我们通过系统提供的接口注册消息处理函数（即回调函数），从而实现接收、处理消息的目的。由于Windows平台的API是用C语言来构建的，我们可以认为它也是回调函数的一个特例。</span></p><p style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; "><span style="font-size: small; color: #000000; font-family: 'Times New Roman'; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 对于分布式组件代理体系CORBA，异步处理有多种方式，如回调、事件服务、通知服务等。事件服务和通知服务是CORBA用来处理异步消息的标准服务，他们主要负责消息的处理、派发、维护等工作。对一些简单的异步处理过程，我们可以通过回调机制来实现。</span></p><p style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; "><span style="font-size: small; color: #000000; font-family: 'Times New Roman'; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 下面我们集中比较具有代表性的语言（C、Object Pascal）和架构（CORBA）来分析回调的实现方式、具体作用等。</span></p><p style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; "><span style="font-size: small; color: #000000; font-family: 'Times New Roman'; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 过程语言中的回调（C）<br /></span><span style="font-size: small; color: #000000; font-family: 'Times New Roman'; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (1 )函数指针<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 回调在C语言中是通过函数指针来实现的,通过将回调函数的地址传给被调函数从而实现回调。因此，要实现回调，必须首先定义函数指针，请看下面的例子：</span></p><p style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; "><span style="font-size: small; color: #000000; font-family: 'Times New Roman'; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; void Func(char *s)；// 函数原型<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; void (*pFunc) (char *);//函数指针</span></p><p style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; "><span style="font-size: small; color: #000000; font-family: 'Times New Roman'; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 可以看出，函数的定义和函数指针的定义非常类似。</span></p><p style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; "><span style="font-size: small; color: #000000; font-family: 'Times New Roman'; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 一般的化，为了简化函数指针类型的变量定义，提高程序的可读性，我们需要把函数指针类型自定义一下。</span></p><p style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; "><span style="font-size: small; color: #000000; font-family: 'Times New Roman'; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; typedef void(*pcb)(char *);</span></p><p style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; "><span style="font-size: small; color: #000000; font-family: 'Times New Roman'; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 回调函数可以象普通函数一样被程序调用，但是只有它被当作参数传递给被调函数时才能称作回调函数。</span></p><p style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; "><span style="font-size: small; color: #000000; font-family: 'Times New Roman'; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 被调函数的例子：</span></p><p style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; "><span style="font-size: small; color: #000000; font-family: 'Times New Roman'; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; void GetCallBack(pcb callback)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /*do something*/<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 用户在调用上面的函数时，需要自己实现一个pcb类型的回调函数：<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; void fCallback(char *s)&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* do something */<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 然后，就可以直接把fCallback当作一个变量传递给GetCallBack,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GetCallBack（fCallback）;</span></p><p style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; "><span style="font-size: small; color: #000000; font-family: 'Times New Roman'; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 如果赋了不同的值给该参数，那么调用者将调用不同地址的函数。赋值可以发生在运行时，这样使你能实现动态绑定。</span></p><p style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; "><span style="font-size: small; color: #000000; font-family: 'Times New Roman'; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (2 )参数传递规则<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 到目前为止，我们只讨论了函数指针及回调而没有去注意ANSI C/C++的编译器规范。许多编译器有几种调用规范。如在Visual C++中，可以在函数类型前加_cdecl，_stdcall或者_pascal来表示其调用规范（默认为_cdecl）。C++ Builder也支持_fastcall调用规范。调用规范影响编译器产生的给定函数名，参数传递的顺序（从右到左或从左到右），堆栈清理责任（调用者或 者被调用者）以及参数传递机制（堆栈，CPU寄存器等）。</span></p><p style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; "><span style="font-size: small; color: #000000; font-family: 'Times New Roman'; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 将调用规范看成是函数类型的一部分是很重要的；不能用不兼容的调用规范将地址赋值给函数指针。例如：</span></p><p style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; "><span style="font-size: small; color: #000000; font-family: 'Times New Roman'; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 被调用函数是以int为参数，以int为返回值<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; __stdcall int callee(int);</span></p><p style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; "><span style="font-size: small; color: #000000; font-family: 'Times New Roman'; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 调用函数以函数指针为参数<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; void caller( __cdecl int(*ptr)(int));</span></p><p style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; "><span style="font-size: small; color: #000000; font-family: 'Times New Roman'; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 在p中企图存储被调用函数地址的非法操作<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; __cdecl int(*p)(int) = callee; // 出错</span></p><p style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; "><span style="font-size: small; color: #000000; font-family: 'Times New Roman'; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 指针p和callee()的类型不兼容，因为它们有不同的调用规范。因此不能将被调用者的地址赋值给指针p，尽管两者有相同的返回值和参数列</span></p><p style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; "><span style="font-size: small; color: #000000; font-family: 'Times New Roman'; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (3 )应用举例<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; C语言的标准库函数中很多地方就采用了回调函数来让用户定制处理过程。如常用的快速排序函数、二分搜索函数等。</span></p><p style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; "><span style="font-size: small; color: #000000; font-family: 'Times New Roman'; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 快速排序函数原型：</span></p><p style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; "><span style="font-size: small; color: #000000; font-family: 'Times New Roman'; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; void qsort(void *base, size_t nelem, size_t width, int (_USERENTRY *fcmp)(const void *, const void *));<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 二分搜索函数原型：<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; void *bsearch(const void *key, const void *base, size_t nelem,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; size_t width, int (_USERENTRY *fcmp)(const void *, const void *));</span></p><p style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; "><span style="font-size: small; color: #000000; font-family: 'Times New Roman'; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 其中fcmp就是一个回调函数的变量。</span></p><p style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; "><span style="font-size: small; color: #000000; font-family: 'Times New Roman'; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 下面给出一个具体的例子：</span></p><p style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; "><span style="font-size: small; color: #000000; font-family: 'Times New Roman'; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #include &lt;stdio.h&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #include &lt;stdlib.h&gt;</span></p><p style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; "><span style="font-size: small; color: #000000; font-family: 'Times New Roman'; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int sort_function( const void *a, const void *b);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int list[5] = { 54, 21, 11, 67, 22 };</span></p><p style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; "><span style="font-size: small; color: #000000; font-family: 'Times New Roman'; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int main(void)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br />int x;</span></p><p style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; "><span style="font-size: small; color: #000000; font-family: 'Times New Roman'; ">qsort((void *)list, 5, sizeof(list[0]), sort_function);<br />for (x = 0; x &lt; 5; x++)<br />printf("%i/n", list[x]);<br />return 0;<br />}</span></p><p style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; "><span style="font-size: small; color: #000000; font-family: 'Times New Roman'; ">int sort_function( const void *a, const void *b)<br />{<br />return *(int*)a-*(int*)b;<br />}</span></p><img src ="http://www.cppblog.com/shixiyu/aggbug/187171.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/shixiyu/" target="_blank">爱走小路</a> 2012-08-14 14:56 <a href="http://www.cppblog.com/shixiyu/archive/2012/08/14/187171.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>VC 回调函数及使用方法</title><link>http://www.cppblog.com/shixiyu/archive/2012/08/14/187166.html</link><dc:creator>爱走小路</dc:creator><author>爱走小路</author><pubDate>Tue, 14 Aug 2012 06:53:00 GMT</pubDate><guid>http://www.cppblog.com/shixiyu/archive/2012/08/14/187166.html</guid><wfw:comment>http://www.cppblog.com/shixiyu/comments/187166.html</wfw:comment><comments>http://www.cppblog.com/shixiyu/archive/2012/08/14/187166.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/shixiyu/comments/commentRss/187166.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/shixiyu/services/trackbacks/187166.html</trackback:ping><description><![CDATA[<div style="margin: 5px 0px; font-size: 20px; line-height: 30px; font-family: 'Microsoft YaHei'; background-color: #ffffff; "><strong><span style="color: red; font-family: Arial; font-size: 14pt; line-height: 26px; text-align: right; ">VC 回调函数及使用方法</span>&nbsp;</strong><span style="font-family: Arial; line-height: 26px; "><br />回调函数说白了就是事件响应程序，Windows的每个消息可以理解为一个事件，事件的响应代码要由用户自己来定义。用户定义了事件响应的代码，但还要Windows知道这段代码的位置（要不然Windows就不知道如何去调用，这也没有用），于是用户需要将回调函数的指针告诉Windows，最典型的例子是在窗口类的结构(WNDCLASS)中给lpfnWndProc分量赋回调函数指针值。</span></div><div id="article_content" style="margin: 20px 0px 0px; line-height: 26px; font-family: Arial; background-color: #ffffff; "><p style="margin: 0px; padding: 0px; ">&nbsp; 回调函数的参数格式是由回调函数的调用者（一般是Windows）来定义的，而回调函数的实现者必须遵循这种格式。Windows程序是以事件驱动模型为基础的，这就必然要用到回调函数这种机制。<br />&nbsp; 要透彻了解回调函数，多看看SDK Samples。而MFC中的消息映射机制已经将窗口消息响应的回调函数隐藏起来了，这也符合C++的编程思想，回调函数终究是一种全局函数，它不能在类中实现，而消息映射机制的目的是使消息响应的代码最终封装在窗口类（CWnd类的子类）中。</p><p style="margin: 0px; padding: 0px; ">如果有时间，不妨看看MESSAGE_MAP宏，消息映射是回调函数，只是这种回调函数的用法不同而已。普通的回调函数是要你提供地址，传进某个函数，由它去调用；而消息映射函数，却是由你定义函数，由MESSAGE_MAP宏去取得地址，并实现它的调用。</p><p style="margin: 0px; padding: 0px; ">回调函数是一个程序员不能显式调用的函数；通过将回调函数的地址传给调用者从而实现调用。要实现回调，必须首先定义函数指针。尽管定义的语法有点不可思议，但如果你熟悉函数声明的一般方法，便会发现函数指针的声明与函数声明非常类似。</p><p style="margin: 0px; padding: 0px; ">typedef void (*f1) ();// 为函数指针声明类型定义<br />void (*p) (); //p是指向某函数的指针<br />void func1()&nbsp;<br />{<br />&nbsp;/* do something */<br />&nbsp;printf("From func1(), Hello World!/n");<br />}</p><p style="margin: 0px; padding: 0px; ">void caller(void(*ptrfunc1)())<br />{<br />&nbsp;ptrfunc1(); /* 调用ptr指向的函数 */&nbsp;<br />}</p><p style="margin: 0px; padding: 0px; ">//typedef bool (*f2) (int *);// 为函数指针声明类型定义<br />//bool (*q) (int *); //p是指向某函数的指针</p><p style="margin: 0px; padding: 0px; ">bool func2(int* t_i)&nbsp;<br />{<br />&nbsp;/* do something */<br />&nbsp;printf("From func2() = %d, Hello World!/n", (*t_i)++);<br />&nbsp;return true;<br />}</p><p style="margin: 0px; padding: 0px; ">void caller2(bool (*ptrfunc2)(int *), int * i)<br />{<br />&nbsp;ptrfunc2(i); /* 调用ptr指向的函数 */&nbsp;<br />}</p><p style="margin: 0px; padding: 0px; "><br />int main(int argc, char* argv[])<br />{<br />&nbsp;printf("From main(), Hello World!/n");<br />&nbsp;printf("/n");</p><p style="margin: 0px; padding: 0px; ">&nbsp;//无参数调用<br />&nbsp;p = func1; /* 传递函数地址地址 */<br />&nbsp;caller(p); /* 传递函数地址到调用者 */</p><p style="margin: 0px; padding: 0px; ">&nbsp;//有参数调用<br />&nbsp;int i = 0;&nbsp;<br />&nbsp;for (int j = 0; j &lt; 10; j++)<br />&nbsp;{<br />&nbsp; caller2(func2, &amp;i); //* 传递函数地址到调用者 */<br />&nbsp;}</p><p style="margin: 0px; padding: 0px; ">&nbsp;//有参数调用第二次</p><p style="margin: 0px; padding: 0px; ">&nbsp; i = 0;<br />&nbsp;//q = func2; /* 传递函数地址地址 */<br />&nbsp;//caller2(q, &amp;i); /* 传递函数地址到调用者 */</p><p style="margin: 0px; padding: 0px; ">&nbsp;printf("/n");<br />&nbsp;printf("From main(), Hello World!/n");</p><p style="margin: 0px; padding: 0px; ">&nbsp;getchar();</p><p style="margin: 0px; padding: 0px; ">&nbsp;return 0;<br />}<br /><br /><br /><p style="color: #333333; ">#include &lt;stdlib.h&gt;<br />#include &lt;stdio.h&gt;<br />int Test1()<br />{<br />&nbsp;&nbsp; int i;<br />&nbsp;&nbsp; for (i=0; i&lt;30; i++)<br />&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp; printf("The %d th charactor is: %c/n", i, (char)('a' + i%26));<br />&nbsp;&nbsp; }<br />&nbsp;&nbsp; return 0;<br />}</p><p style="color: #333333; ">int Test2(int num)<br />{<br />&nbsp;&nbsp; int i;<br />&nbsp;&nbsp; for (i=0; i&lt;num; i++)<br />&nbsp;&nbsp; {&nbsp;<br />&nbsp;&nbsp;&nbsp; printf("The %d th charactor is: %c/n", i, (char)('a' + i%26));<br />&nbsp;&nbsp; }<br />&nbsp;&nbsp; return 0;<br />}</p><p style="color: #333333; ">void Caller1(int (*ptr)())<br />//指向函数的指针作函数参数<br />{<br />&nbsp;&nbsp; (*ptr)();<br />}</p><p style="color: #333333; ">void Caller2(int n, int (*ptr)(int n))<br />//指向函数的指针作函数参数,这里第一个参数是为指向函数的指针服务的，<br />{<br />&nbsp;&nbsp; //不能写成void Caller2(int (*ptr)(int n))，这样的定义语法错误。<br />&nbsp;&nbsp; (*ptr)(n);<br />&nbsp;&nbsp; return;<br />}<br />int main()</p><p style="color: #333333; ">{</p><p style="color: #333333; ">&nbsp;&nbsp; printf("************************/n");</p><p style="color: #333333; ">&nbsp;&nbsp; Caller1(Test1); //相当于调用Test1();</p><p style="color: #333333; ">&nbsp;&nbsp; printf("************************/n");</p><p style="color: #333333; ">&nbsp;&nbsp; Caller2(30, Test2); //相当于调用Test2(30);</p><p style="color: #333333; ">&nbsp;&nbsp; return 0;</p><p style="color: #333333; ">}</p><p style="color: #333333; ">/*<br />int main()</p><p style="color: #333333; ">{</p><p style="color: #333333; ">&nbsp;&nbsp; printf("************************/n");</p><p style="color: #333333; ">&nbsp;&nbsp; Caller1(&amp;Test1); //相当于调用Test1();</p><p style="color: #333333; ">&nbsp;&nbsp; printf("&amp;&amp;&amp;&amp;&amp;&amp;************************/n");</p><p style="color: #333333; ">&nbsp;&nbsp; Caller2(30, &amp;Test2); //相当于调用Test2(30);</p><p style="color: #333333; ">&nbsp;&nbsp; return 0;</p><p style="color: #333333; ">}<br />*/</p></p></div><img src ="http://www.cppblog.com/shixiyu/aggbug/187166.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/shixiyu/" target="_blank">爱走小路</a> 2012-08-14 14:53 <a href="http://www.cppblog.com/shixiyu/archive/2012/08/14/187166.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C/C++中指针和引用之相关问题研究  </title><link>http://www.cppblog.com/shixiyu/archive/2012/08/14/187164.html</link><dc:creator>爱走小路</dc:creator><author>爱走小路</author><pubDate>Tue, 14 Aug 2012 06:48:00 GMT</pubDate><guid>http://www.cppblog.com/shixiyu/archive/2012/08/14/187164.html</guid><wfw:comment>http://www.cppblog.com/shixiyu/comments/187164.html</wfw:comment><comments>http://www.cppblog.com/shixiyu/archive/2012/08/14/187164.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/shixiyu/comments/commentRss/187164.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/shixiyu/services/trackbacks/187164.html</trackback:ping><description><![CDATA[<p style="line-height: 25px; margin: 0px 0px 10px; padding: 0px; font-family: Arial, Helvetica, simsun, u5b8bu4f53; background-color: #ffffff; "><strong>一、基本知识</strong><br />指针和引用的声明方式：<br />声明指针：&nbsp;char* pc;<br />声明引用：&nbsp;char c = 'A'<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; char&amp; rc = c;</p><p style="line-height: 25px; margin: 0px 0px 10px; padding: 0px; font-family: Arial, Helvetica, simsun, u5b8bu4f53; background-color: #ffffff; ">它们的区别：<br />&#9312;从现象上看，指针在运行时可以改变其所指向的值，而引用一旦和某个对象绑定后就不再改变。这句话可以理解为：指针可以被重新赋值以指向另一个不同的对象。但是引用则总是指向在初始化时被指定的对象，以后不能改变，但是指定的对象其内容可以改变。<br />&#9313;从内存分配上看，程序为指针变量分配内存区域，而不为引用分配内存区域，因为引用声明时必须初始化，从而指向一个已经存在的对象。引用不能指向空值。<br />&#9314;从编译上看，程序在编译时分别将指针和引用添加到符号表上，符号表上记录的是变量名及变量所对应地址。指针变量在符号表上对应的地址值为指针变量的地址值，而引用在符号表上对应的地址值为引用对象的地址值。符号表生成后就不会再改，因此指针可以改变指向的对象（指针变量中的值可以改），而引用对象不能改。这是使用指针不安全而使用引用安全的主要原因。从某种意义上来说引用可以被认为是不能改变的指针。<br />&#9315;不存在指向空值的引用这个事实意味着使用引用的代码效率比使用指针的要高。因为在使用引用之前不需要测试它的合法性。相反，指针则应该总是被测试，防止其为空。<br />&#9316;理论上，对于指针的级数没有限制，但是引用只能是一级。如下：<br />&nbsp; int** p1;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 合法。指向指针的指针<br />&nbsp; int*&amp; p2;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 合法。指向指针的引用<br />&nbsp; int&amp;* p3;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 非法。指向引用的指针是非法的<br />&nbsp; int&amp;&amp; p4;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 非法。指向引用的引用是非法的<br />&nbsp; 注意上述读法是从左到右。<br />&nbsp;<br />程序1：<br />#include "stdio.h"<br />int main(void)<br />{<br />&nbsp; // 声明一个char型指针pc，且让它指向空值<br />&nbsp; char* pc = 0;<br />&nbsp; char a = 'a';<br />&nbsp; // 声明一个引用rc，且让它引用变量a<br />&nbsp; char&amp; rc = a;<br />&nbsp; printf("%d, %c\n", pc, rc);<br />&nbsp;<br />&nbsp; char *pc2;<br />&nbsp; // 声明一个指针，但可以不初始化<br />&nbsp; pc2 = pc;<br />&nbsp;<br />&nbsp; // char&amp; rc2;<br />&nbsp; // 上面语句编译时，会产生如下错误：<br />&nbsp; // error C2530: 'rc2' : references must be initialized<br />&nbsp; // 即，应用必须初始化<br />&nbsp; // rc = *pc;<br />&nbsp; // 上面语句编译不会有问题，但运行时，会报如下错误：<br />&nbsp; // "0x00401057"指令引用的"0x00000000"内存。该内存不能为"read"<br />&nbsp; // 说明引用在任何情况下，都不能指向空值<br />&nbsp;<br />&nbsp; return 0;<br />}</p><p style="margin: 0px 0px 10px; padding: 0px; "><br />程序2：<br />#include &lt;iostream&gt;<br />#include &lt;string&gt;<br />using namespace std;<br />int main(void)<br />{<br />&nbsp; string s1("Hello");<br />&nbsp; string s2("World");<br />&nbsp; // printf("%s\n", s1); 不能用printf输出s1，而应该用cout<br />&nbsp;<br />&nbsp; cout &lt;&lt; "s1的地址 = "&lt;&lt; &amp;s1 &lt;&lt; endl;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // &amp;s1 = 0012FF64<br />&nbsp; cout &lt;&lt; "s2的地址 = "&lt;&lt; &amp;s2 &lt;&lt; endl;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // &amp;s2 = 0012FF54<br />&nbsp;<br />&nbsp;&nbsp;<strong>string&amp; rs = s1;</strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// 1. 定义一个引用rs，rs引用s1<br />&nbsp; cout &lt;&lt; "引用rs的地址 ＝ " &lt;&lt; &amp;rs &lt;&lt; endl;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // &amp;rs = 0012FF64<br />&nbsp;<br />&nbsp; string* ps = &amp;s1;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //定义一个指针ps, ps指向s1<br />&nbsp; cout &lt;&lt; "指针ps的地址 = " &lt;&lt; ps &lt;&lt; endl;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // ps = 0012FF64<br />&nbsp;<br />&nbsp; cout &lt;&lt; rs &lt;&lt; ", " &lt;&lt; *ps &lt;&lt; endl;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Hello, Hello<br />&nbsp; // 如果没有#include &lt;string&gt;，上面的语句在编译的时候，会出现如下错误：<br />&nbsp; // error C2679: binary '&lt;&lt;' : no operator defined which takes a right-<br />&nbsp; // hand operand of type 'class std::basic_string&lt;char,struct<br />&nbsp; // std::char_traits&lt;char&gt;,class std::allocator&lt;char&gt; &gt;'<br />&nbsp; // (or there is no acceptable&nbsp; conversion)<br />&nbsp;<br />&nbsp;&nbsp;<strong>rs = s2;</strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 2. rs仍旧引用s1, 但是s1现在的值是"World"<br />&nbsp; ps = &amp;s2;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // ps现在指向s2<br />&nbsp;<br />&nbsp; cout &lt;&lt; "引用rs的地址 ＝ " &lt;&lt; &amp;rs &lt;&lt; endl;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // &amp;rs = 0012FF64 未改变<br />&nbsp; cout &lt;&lt; "引用rs的值 ＝ " &lt;&lt; rs &lt;&lt; endl;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // rs = "World" 已改变<br />&nbsp;<br />&nbsp; cout &lt;&lt; "指针ps的地址 ＝ " &lt;&lt; ps &lt;&lt; endl;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // ps = 0012FF54&nbsp; 已改变<br />&nbsp; cout &lt;&lt; "指针ps所指地址的内容 ＝ " &lt;&lt; *ps &lt;&lt; endl;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // *ps = World&nbsp;&nbsp;&nbsp; 已改变<br />&nbsp;<br />&nbsp;&nbsp;<strong>cout &lt;&lt; "s1的地址 = "&lt;&lt; &amp;s1 &lt;&lt; endl;</strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 3. &amp;s1 = 0012FF64 未改变<br />&nbsp;<strong>&nbsp;cout &lt;&lt; "s1的值 = " &lt;&lt; s1 &lt;&lt; endl;</strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 4. s1 = World&nbsp; 已改变<br />&nbsp;<br />&nbsp; return 0;<br />}<br />&nbsp;<br />可以认为：<br />引用就是变量的别名，在引用初始化的时候就已经确定，以后不能再改变。见程序2的粗体字语句。第1句，声明了rs引用s1，s1的值为&#8221;Hello&#8221;，从这以后，rs实际上就相当于变量s1了，或者从更本质的意义上来说，rs的地址就是初始化时s1的地址了，以后都不会再改变。这应该比较好理解，比如我们在程序中定义了一个变量a，不管我们如何给a赋值，但它的地址是不会改变的；<br />&nbsp;<br />第2句，rs仍旧指向初始化时s1的地址，但此处的赋值就相当于重新给s1赋值，因此我们从第3句和第4句可以看到，s1的地址并没有发生变化，但是其值已经发生了变化。</p><p style="margin: 0px 0px 10px; padding: 0px; "><strong>二、作为参数传递</strong><br />利用引用的这个特性，可以用它作为函数的传出参数。如程序3：<br />#include &lt;iostream&gt;<br />#include &lt;string&gt;<br />using namespace std;<br />int newEvaluation(string&amp; aStr)<br />{<br />&nbsp; string bStr("Hello,");<br />&nbsp; aStr = bStr + aStr;<br />&nbsp;<br />&nbsp; return 0;<br />}<br />&nbsp;<br />int main(void)<br />{<br />&nbsp; string aStr("Patrick!");<br />&nbsp; newEvaluation(aStr);<br />&nbsp; std::cout &lt;&lt; aStr &lt;&lt; endl;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 输出结果："Hello, Patrick!"<br />&nbsp;<br />&nbsp; return 0;<br />}<br />&nbsp;<br />而一般变量，则不能从函数内部传值出来，比如程序4：<br />#include &lt;iostream&gt;<br />#include &lt;string&gt;<br />using namespace std;<br />&nbsp;<br />int newEvaluation(string aStr)<br />{<br />&nbsp; string bStr("Hello,");<br />&nbsp; aStr = bStr + aStr;<br />&nbsp;<br />&nbsp; return 0;<br />}<br />&nbsp;<br />int main(void)<br />{<br />&nbsp; string aStr("Patrick!");<br />&nbsp; newEvaluation(aStr);<br />&nbsp; std::cout &lt;&lt; aStr &lt;&lt; endl;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 输出结果："Patrick!"，aStr的值没有变化<br />&nbsp;<br />&nbsp; return 0;<br />}<br />&nbsp;<br />当然程序3引用传递的方式也可以写成指针传递的方式，如程序5：<br />#include &lt;iostream&gt;<br />#include &lt;string&gt;<br />using namespace std;<br />&nbsp;<br />int newEvaluation(string* const aStr)<br />{<br />&nbsp; string bStr("Hello,");<br />&nbsp; *aStr = bStr + *aStr;<br />&nbsp;<br />&nbsp; return 0;<br />}<br />&nbsp;<br />int main(void)<br />{<br />&nbsp; string aStr("Patrick!");<br />&nbsp; newEvaluation(&amp;aStr);<br />&nbsp; std::cout &lt;&lt; aStr &lt;&lt; endl;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 输出结果："Hello, Patrick!"<br />&nbsp;<br />&nbsp; return 0;<br />}<br />&nbsp;<br />注意程序中的陷井，如程序6：<br />#include &lt;iostream.h&gt;<br />int *pPointer;<br />void SomeFunction()<br />{<br />&nbsp; int nNumber;<br />&nbsp; nNumber = 25;<br />&nbsp; //让指针指向nNumber<br />&nbsp; pPointer = &amp;nNumber;<br />}<br />&nbsp;<br />void main()<br />{<br />&nbsp; SomeFunction(); &nbsp;&nbsp;&nbsp;//为pPointer赋值<br />&nbsp; //为什么这里失败了?为什么没有得到25<br />&nbsp; cout &lt;&lt; "Value of *pPointer: " &lt;&lt; *pPointer &lt;&lt; endl;<br />}<br />&nbsp;<br />这段程序先调用了SomeFunction函数，创建了个叫nNumber的变量，接着让指针pPointer指向了它。可是问题出在哪儿呢？当函数结束后，nNumber被删掉了，因为这一个局部变量。局部变量在定义它的函数执行完后都会被系统自动删掉。也就是说当SomeFunction 函数返回主函数main()时，这个变量已经被删掉，但pPointer还指着变量曾经用过的但现在已不属于这个程序的区域。<br />&nbsp;<br />尽管在SomeFunction中使用所谓的动态分配内存。程序7中也存在陷井：<br />#include &lt;iostream.h&gt;<br />int *pPointer;<br />&nbsp;<br />void SomeFunction()<br />{<br />&nbsp;&nbsp;&nbsp; int intNumber = 25;<br />&nbsp;&nbsp;&nbsp; // 让指针指向一个新的整型<br />&nbsp;&nbsp;&nbsp; pPointer = new int;<br />&nbsp;&nbsp;&nbsp; pPointer = &amp;intNumber;<br />}<br />&nbsp;<br />void main()<br />{<br />&nbsp;&nbsp;&nbsp; SomeFunction();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 为pPointer赋值<br />&nbsp;&nbsp;&nbsp; cout&lt;&lt; "Value of *pPointer: " &lt;&lt; *pPointer &lt;&lt; endl;<br />&nbsp;&nbsp;&nbsp; delete pPointer;<br />}<br />原因也如上面所言，intNumber的作用范围仅限于SomeFunction中，离开了SomeFunction，那么intNumber就不存在了，那么&amp;intNumber即intNumber的地址就变得没有意义了，因此，该地址所指向的值是不确定的。如果改为下面的程序就不会有问题了。</p><p style="margin: 0px 0px 10px; padding: 0px; ">程序8：<br />#include &lt;iostream.h&gt;<br />int *pPointer;<br />&nbsp;<br />void SomeFunction()<br />{<br />&nbsp;&nbsp;&nbsp; int intNumber = 25;<br />&nbsp;&nbsp;&nbsp; // 让指针指向一个新的整型<br />&nbsp;&nbsp;&nbsp; pPointer = new int(intNumber);<br />}<br />&nbsp;<br />void main()<br />{<br />&nbsp;&nbsp;&nbsp; SomeFunction();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 为pPointer赋值<br />&nbsp;&nbsp;&nbsp; cout&lt;&lt; "Value of *pPointer: " &lt;&lt; *pPointer &lt;&lt; endl;<br />&nbsp;&nbsp;&nbsp; delete pPointer;<br />}</p><p style="margin: 0px 0px 10px; padding: 0px; "><strong>三、指针的指针</strong><br />前面说到，指针是没有级数限制的。<br />程序9：<br />#include&lt;stdio.h&gt;<br />#include&lt;stdlib.h&gt;<br />&nbsp;<br />void main(void)<br />{<br />&nbsp;&nbsp;&nbsp; int i, j;<br />&nbsp;&nbsp;&nbsp; int a[10], b[3][4], *p1, *p2, **p3;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp; for(i = 0; i &lt; 10; i++)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; scanf("%d", &amp;a[i]);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;<br />&nbsp;&nbsp;&nbsp; for(i = 0; i &lt; 3; i++)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for(j = 0; j &lt; 4; j++)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; scanf("%d", &amp;b[i][j]);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;<br />&nbsp;&nbsp;&nbsp; p1 = a;<br />&nbsp;&nbsp;&nbsp; p3 = &amp;p1;<br />&nbsp;&nbsp;&nbsp; for(i = 0; i &lt; 10; i++)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("%4d", *(*p3+i));&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp; printf("\n");<br />&nbsp;<br />&nbsp;&nbsp;&nbsp; for(p1 = a; p1 - a &lt; 10; p1++)&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; p3 = &amp;p1;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("%4d", **p3);<br />&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp; printf("\n");<br />&nbsp;<br />&nbsp;&nbsp;&nbsp; for(i = 0; i &lt; 3; i++)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; p2 = b[i];<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; p3 = &amp;p2;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for(j = 0; j &lt; 4; j++)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("%4d",*(*p3+j));<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("\n");<br />&nbsp;&nbsp;&nbsp; }<br />&nbsp;<br />&nbsp;&nbsp;&nbsp; for(i = 0; i &lt; 3; i++)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; p2 = b[i];<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for(p2 = b[i]; p2-b[i] &lt; 4; p2++)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; p3 = &amp;p2;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("%4d", **p3);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("\n");<br />&nbsp;&nbsp;&nbsp; }<br />}</p><p style="margin: 0px 0px 10px; padding: 0px; ">输出的结果：<br />&nbsp;&nbsp;&nbsp; 1&nbsp;&nbsp; 2&nbsp;&nbsp; 3&nbsp;&nbsp; 4&nbsp;&nbsp; 5&nbsp;&nbsp; 6&nbsp;&nbsp; 7&nbsp;&nbsp; 8&nbsp;&nbsp; 9&nbsp;&nbsp; 10<br />&nbsp;&nbsp;&nbsp; 1&nbsp;&nbsp; 2&nbsp;&nbsp; 3&nbsp;&nbsp; 4&nbsp;&nbsp; 5&nbsp;&nbsp; 6&nbsp;&nbsp; 7&nbsp;&nbsp; 8&nbsp;&nbsp; 9&nbsp;&nbsp; 10<br />&nbsp;&nbsp;&nbsp; 11&nbsp; 12&nbsp; 13&nbsp; 14<br />&nbsp;&nbsp;&nbsp; 15&nbsp; 16&nbsp; 17&nbsp; 18<br />&nbsp;&nbsp;&nbsp; 19&nbsp; 20&nbsp; 21&nbsp; 22<br />&nbsp;&nbsp;&nbsp; 11&nbsp; 12&nbsp; 13&nbsp; 14<br />&nbsp;&nbsp;&nbsp; 15&nbsp; 16&nbsp; 17&nbsp; 18<br />&nbsp;&nbsp;&nbsp; 19&nbsp; 20&nbsp; 21&nbsp; 22</p><p style="margin: 0px 0px 10px; padding: 0px; "><strong>四、函数指针和函数引用</strong><br />函数指针是C＋＋最大的优点之一。和使用普通指针相比，高级程序员只要有可能都更愿意使用引用，因为引用更容易处理一些。然而，当处理函数时，函数引用对比函数指针就未必有这个优势了。现有的代码很少使用函数引用。下面将向介绍如何函数指针、如何使用函数引用以及分别在什么情况下使用它们。</p><p style="margin: 0px 0px 10px; padding: 0px; ">&#9312; 函数指针的例子<br />#include &lt;iostream&gt;<br />void print(int i)<br />{<br />&nbsp;&nbsp;&nbsp; std::cout &lt;&lt; i &lt;&lt; std::endl;<br />}<br />&nbsp;<br />void multiply(int&amp; nDest, int nBy)<br />{<br />&nbsp;&nbsp;&nbsp; nDest *= nBy;<br />}<br />&nbsp;<br />void print_something()<br />{<br />&nbsp;&nbsp;&nbsp; std::cout &lt;&lt; "something" &lt;&lt; std::endl;<br />}<br />&nbsp;<br />int sayHello()<br />{<br />&nbsp;&nbsp;&nbsp; std::cout &lt;&lt; "Hello, World!" &lt;&lt; std::endl;<br />&nbsp;&nbsp;&nbsp; return 10;<br />}<br />&nbsp;<br />int main()<br />{&nbsp;<br />&nbsp;&nbsp;&nbsp; void (*pFunction_1)(int);<br />&nbsp;&nbsp;&nbsp; pFunction_1 = &amp;print;<br />&nbsp;&nbsp;&nbsp; pFunction_1(1);<br />&nbsp;&nbsp;&nbsp; // 输出结果为1<br />&nbsp;<br />&nbsp;&nbsp;&nbsp; void (*pFunction_2)(int&amp;, int) = &amp;multiply;<br />&nbsp;&nbsp;&nbsp; int i = 1;<br />&nbsp;&nbsp;&nbsp; pFunction_2(i, 10);<br />&nbsp;&nbsp;&nbsp; std::cout &lt;&lt; "i = " &lt;&lt; i &lt;&lt; std::endl;<br />&nbsp;&nbsp;&nbsp; // 输出结果为10<br />&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp; void (*pFunction_3)();<br />&nbsp;&nbsp;&nbsp; pFunction_3 = &amp;print_something;<br />&nbsp;&nbsp;&nbsp; pFunction_3();<br />&nbsp;&nbsp;&nbsp; // 输出结果为something<br />&nbsp;<br />&nbsp;&nbsp;&nbsp; int (*pFunction_4)();<br />&nbsp;&nbsp;&nbsp; pFunction_4 = &amp;sayHello;<br />&nbsp;&nbsp;&nbsp; int a = pFunction_4();<br />&nbsp;&nbsp;&nbsp; // 输出结果为Hello, World!<br />&nbsp;&nbsp;&nbsp; std::cout &lt;&lt; a &lt;&lt; std::endl;<br />&nbsp;&nbsp;&nbsp; // 输出结果为10<br />&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp; return 0;<br />}<br />&nbsp;<br />&#9313; 函数引用的例子<br />#include &lt;iostream&gt;<br />void print(int i)<br />{<br />&nbsp;&nbsp;&nbsp; std::cout &lt;&lt; i &lt;&lt; std::endl;<br />}<br />&nbsp;<br />void print2(int i)<br />{<br />&nbsp;&nbsp;&nbsp; std::cout &lt;&lt; i &lt;&lt; std::endl;<br />}<br />&nbsp;<br />void multiply(int&amp; nDest, int nBy)<br />{<br />&nbsp;&nbsp;&nbsp; nDest *= nBy;<br />}<br />&nbsp;<br />void print_something()<br />{<br />&nbsp;&nbsp;&nbsp; std::cout &lt;&lt; "something" &lt;&lt; std::endl;<br />}<br />&nbsp;<br />int sayHello()<br />{<br />&nbsp;&nbsp;&nbsp; std::cout &lt;&lt; "Hello, World!" &lt;&lt; std::endl;<br />&nbsp;&nbsp;&nbsp; return 10;<br />}<br />&nbsp;<br />&nbsp;<br />int main()<br />{&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp; // void (&amp;rFunction_1)(int);<br />&nbsp;&nbsp;&nbsp; // 错误：未初始化引用！引用必须初始化<br />&nbsp;<br />&nbsp;&nbsp;&nbsp; void (&amp;rFunction_2)(int) = print;<br />&nbsp;&nbsp;&nbsp; rFunction_2(1);<br />&nbsp;&nbsp;&nbsp; // 输出1<br />&nbsp;<br />&nbsp;&nbsp;&nbsp; rFunction_2 = print2;<br />&nbsp;&nbsp;&nbsp; rFunction_2(2);<br />&nbsp;&nbsp;&nbsp; // 输出2<br />&nbsp;<br />&nbsp;&nbsp;&nbsp; void (&amp;rFunction_3)(int&amp;, int) = multiply;<br />&nbsp;&nbsp;&nbsp; int i = 1;<br />&nbsp;&nbsp;&nbsp; rFunction_3(i, 10);&nbsp;<br />&nbsp;&nbsp;&nbsp; std::cout &lt;&lt; i &lt;&lt; std::endl;<br />&nbsp;&nbsp;&nbsp; // 输出10<br />&nbsp;<br />&nbsp;&nbsp;&nbsp; void (&amp;rFunction_4)() = print_something;<br />&nbsp;&nbsp;&nbsp; rFunction_4();<br />&nbsp;&nbsp;&nbsp; // 输出something<br />&nbsp;<br />&nbsp;&nbsp;&nbsp; int (&amp;rFunction_5)();<br />&nbsp;&nbsp;&nbsp; rFunction_5 = sayHello;<br />&nbsp;&nbsp;&nbsp; int a = rFunction_5();&nbsp;&nbsp; // 输出Hello, World!<br />&nbsp;&nbsp;&nbsp; std::cout &lt;&lt; a &lt;&lt; std::endl;<br />&nbsp;&nbsp;&nbsp; // 输出10<br />&nbsp;<br />&nbsp;&nbsp;&nbsp; return 0;<br />}<br />&nbsp;<br />&#9314; 函数指针和函数引用作为函数参数<br />#include &lt;iostream&gt;<br />&nbsp;<br />void print(int i)<br />{<br />&nbsp;&nbsp;&nbsp; std::cout &lt;&lt; i &lt;&lt; std::endl;<br />}<br />&nbsp;<br />void print2(int i)<br />{<br />&nbsp;&nbsp;&nbsp; std::cout &lt;&lt; i * 2 &lt;&lt; std::endl;<br />}<br />&nbsp;<br />void printSomething()<br />{<br />&nbsp;&nbsp;&nbsp; std::cout &lt;&lt; "Something" &lt;&lt; std::endl;<br />}<br />&nbsp;<br />void sayHello()<br />{<br />&nbsp;&nbsp;&nbsp; std::cout &lt;&lt; "Hello, World!" &lt;&lt; std::endl;<br />}<br />&nbsp;<br />void call_p_func(void (*func)(int))<br />{<br />&nbsp;&nbsp;&nbsp; func(1);<br />&nbsp;&nbsp;&nbsp; func(2);<br />&nbsp;&nbsp;&nbsp; func(3);<br />}<br />&nbsp;<br />void call_r_func(void (&amp;func)(int))<br />{<br />&nbsp;&nbsp;&nbsp; func(1);<br />&nbsp;&nbsp;&nbsp; func(2);<br />&nbsp;&nbsp;&nbsp; func(3);<br />}<br />&nbsp;<br />void call_p_function(void (*func)())<br />{<br />&nbsp;&nbsp;&nbsp; func();<br />}<br />&nbsp;<br />int main()<br />{&nbsp;<br />&nbsp;&nbsp;&nbsp; std::cout &lt;&lt; "函数指针作为参数" &lt;&lt; std::endl;<br />&nbsp;&nbsp;&nbsp; call_p_func(&amp;print);<br />&nbsp;&nbsp;&nbsp; call_p_func(&amp;print2);<br />&nbsp;&nbsp;&nbsp; call_p_function(&amp;printSomething);<br />&nbsp;&nbsp;&nbsp; call_p_function(&amp;sayHello);<br />&nbsp;&nbsp;&nbsp; call_p_function(sayHello);<br />&nbsp;&nbsp;&nbsp; // 上面两句对于某些编译器来说是一样的，但是推荐使用前者的写法，<br />&nbsp;&nbsp;&nbsp; // 这样可以是程序的可读性更好一些<br />&nbsp;<br />&nbsp;&nbsp;&nbsp; std::cout &lt;&lt; "函数引用作为参数" &lt;&lt; std::endl;<br />&nbsp;&nbsp;&nbsp; call_r_func(print);<br />&nbsp;&nbsp;&nbsp; call_r_func(print2);<br />&nbsp;<br />&nbsp;&nbsp;&nbsp; return 0;<br />}<br />&nbsp;<br />总结：<br />函数指针的声明使用方式：<br />&lt;想要指向的函数之返回类型&gt;(*函数指针的名称)&lt;想要指向的函数之参数类型&#8230;&gt;<br />如要想声明一个函数指针指向以下函数：<br />void print(int i)<br />{<br />&nbsp;&nbsp;&nbsp; std::cout &lt;&lt; i &lt;&lt; std::endl;<br />}<br />那么就可以如下操作：<br />void (*pFunction)(int);<br />然后如下用函数的地址给pFunction赋值：<br />pFunction = &amp;print;<br />在然后，pFunction就可以和函数print一样使用了，比如，<br />pFunction(1)；<br />等等。<br />&nbsp;<br />函数引用的声明和使用方式：<br />&lt;欲引用的函数之返回类型&gt;(&amp;函数引用的名称)&lt;欲引用的函数之参数类型&#8230;&gt;=&lt;欲引用的函数的名称&gt;，至所以如此，是引用在声明的时候必须初始化，引用不能指向空值。<br />如要想声明一个函数引用指向以下函数：<br />void print(int i)<br />{<br />&nbsp;&nbsp;&nbsp; std::cout &lt;&lt; i &lt;&lt; std::endl;<br />}<br />那么就可以如下操作：<br />void (&amp;rFunction)(int)=print;<br />在然后，rFunction就可以和函数print一样使用了，比如，<br />rFunction(1)；<br />等等。</p><p style="margin: 0px 0px 10px; padding: 0px; "><strong>五、const修饰指针和引用</strong><br />大致而言，const修饰指针和引用分三种情况，即const修饰指针、const修饰引用和const修饰指针的引用。下面分别讨论之。<br />&nbsp;<br />&#9312; const修饰指针<br />&nbsp;&nbsp; const修饰指针又分为三种情况，即const修饰指针本身、const修饰指针所指的变量（或对象）以及const修饰指针本身和指针所指的变量（或对象）。<br />&nbsp;<br />&nbsp;&nbsp;&nbsp; a. const修饰指针本身<br />&nbsp;&nbsp;&nbsp; 在这种情况下，指针本身是常量，不能改变，任何修改指针本身的行为都是非法的，例如：<br />&nbsp;&nbsp;&nbsp; double pi = 3.1416;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp; double* const PI = &amp;pi;<br />&nbsp;<br />&nbsp;&nbsp;&nbsp; double alpha = 3.14;<br />&nbsp;&nbsp;&nbsp; PI = &amp;alpha;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 错误。因为指针PI是常量，不能再被改变。<br />&nbsp;&nbsp;&nbsp; *PI = alpha;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // OK。虽然指针PI不能被改变，但指针所指的变量或者对象可变。<br />&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp; b. const修饰指针指向的变量（或对象）<br />&nbsp;&nbsp;&nbsp; 在这种情况下，指针本身可以改变，但const所修饰的指针所指向的对象不能被改变，例如：<br />&nbsp;&nbsp;&nbsp; double pi = 3.1416;<br />&nbsp;&nbsp;&nbsp; const double* PI = &amp;pi;<br />&nbsp;<br />&nbsp;&nbsp;&nbsp; double alpha = 3.14;<br />&nbsp;&nbsp;&nbsp; *PI = alpha;&nbsp;&nbsp;&nbsp; // 错误。因为PI所指向的内容是常量，因此*PI不能被改变。<br />&nbsp;&nbsp;&nbsp; PI = &amp;alpha;&nbsp;&nbsp;&nbsp; // OK。虽然指针所指的内容不能被改变，但指针PI本身可改变。从而通过这种方式改变*PI。<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp; c. const修饰指针本身和指针所指向的变量（或对象）<br />&nbsp;&nbsp;&nbsp; 在这种情况下，指针本身和指针指向的变量(或对象)均不能被改变，例如：<br />&nbsp;&nbsp;&nbsp; double pi = 3.146;<br />&nbsp;&nbsp;&nbsp; const double* const PI = &amp;pi;<br />&nbsp;&nbsp;&nbsp; //double const* const PI = &amp;pi;<br />&nbsp;&nbsp;&nbsp; cout &lt;&lt; "PI = " &lt;&lt; PI &lt;&lt; endl;<br />&nbsp;&nbsp;&nbsp; cout &lt;&lt; "*PI = " &lt;&lt; *PI &lt;&lt; endl;<br />&nbsp;<br />&nbsp;&nbsp;&nbsp; double alpha = 3.14;<br />&nbsp;&nbsp;&nbsp; //*PI = alpha;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 错误。因为PI所指向的内容是常量，因此*PI不能被改变。<br />&nbsp;&nbsp;&nbsp; //PI = &amp;alpha;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 错误。因为指针PI是常量，不能再被改变。<br />&nbsp;<br />&#9313; const修饰引用<br />&nbsp;&nbsp; const修饰引用没有指针修饰指针那么复杂，只有一种形式。引用本身不能被改变，但所指向的对象是可以被改变的，见上面&#8220;一、基本知识&#8221;。<br />&nbsp;&nbsp;&nbsp; double pi = 3.1416;<br />&nbsp;&nbsp;&nbsp; //const double&amp; PI = pi;<br />&nbsp;&nbsp;&nbsp; double const&amp; PI = pi;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //和上面一句是等价的<br />&nbsp;&nbsp;&nbsp; //double&amp; const PI = pi;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //有问题。很多编译器会产生warning<br />&nbsp;&nbsp;&nbsp; cout &lt;&lt; PI &lt;&lt; endl;<br />&nbsp;<br />&#9314; const修饰指针引用<br />&nbsp;&nbsp; 我们用例子来说明。<br />&nbsp;&nbsp; double pi = 3.14;<br />&nbsp;&nbsp; const double* pPI = &amp;pi;<br />&nbsp;&nbsp; //const double*&amp; rPI = &amp;pi;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //错误。不能将double* 转换成const double *&amp;<br />&nbsp;&nbsp; const double*&amp; rPI = pPI;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //OK。声明指针引用的正确方法</p><p style="margin: 0px 0px 10px; padding: 0px; ">说明：const double*&amp; rPI = &amp;pi; 为什么会出现错误呢？我们知道，引用是被引用对象的别名，正因为如此，由于rPI是pPI的别名，因此rPI和pPI的类型必须完全一致。从上面的代码段我们可以看到，rPI的类型是const double*，而&amp;pi的类型是double*，因此这句程序是错误的。<br />&nbsp;<br />下面这段代码和 &#9312; 中的b中的情形对应（即内容不可变，指针可变）：<br />double pi = 3.1416;<br />double api = 3.14;<br />const double* pPI = &amp;pi;<br />const double* pAPI = &amp;api;<br />const double*&amp; rPI = pPI;<br />const double*&amp; rAPI = pPI;</p><p style="margin: 0px 0px 10px; padding: 0px; ">*rAPI = api;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 错误。指针所指向的值不能被直接改变<br />rAPI = pAPI;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // OK。指针本身可以被改变</p><p style="margin: 0px 0px 10px; padding: 0px; ">指针引用的用法还有其它的情形，由于罕用，故此不谈及。</p><img src ="http://www.cppblog.com/shixiyu/aggbug/187164.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/shixiyu/" target="_blank">爱走小路</a> 2012-08-14 14:48 <a href="http://www.cppblog.com/shixiyu/archive/2012/08/14/187164.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>走近 STL</title><link>http://www.cppblog.com/shixiyu/archive/2012/08/14/187140.html</link><dc:creator>爱走小路</dc:creator><author>爱走小路</author><pubDate>Tue, 14 Aug 2012 03:05:00 GMT</pubDate><guid>http://www.cppblog.com/shixiyu/archive/2012/08/14/187140.html</guid><wfw:comment>http://www.cppblog.com/shixiyu/comments/187140.html</wfw:comment><comments>http://www.cppblog.com/shixiyu/archive/2012/08/14/187140.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/shixiyu/comments/commentRss/187140.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/shixiyu/services/trackbacks/187140.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 什么是泛型程序设计　　我们可以简单的理解为：使用模板的程序设计就是泛型程序设计。就像我们我们可以简单的理解面向对象程序设计就是使用虚函数的程序设计一样。STL是什么&nbsp;　　作为一个C++程序设计者，STL是一种不可忽视的技术。Sandard Template Library (STL)：标准模板库,更准确的说是&nbsp;C++&nbsp;程序设计语言标准模板库。学习过MFC的人知道，MF...&nbsp;&nbsp;<a href='http://www.cppblog.com/shixiyu/archive/2012/08/14/187140.html'>阅读全文</a><img src ="http://www.cppblog.com/shixiyu/aggbug/187140.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/shixiyu/" target="_blank">爱走小路</a> 2012-08-14 11:05 <a href="http://www.cppblog.com/shixiyu/archive/2012/08/14/187140.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C++中虚函数的作用是什么？它应该怎么用呢？</title><link>http://www.cppblog.com/shixiyu/archive/2012/08/14/187138.html</link><dc:creator>爱走小路</dc:creator><author>爱走小路</author><pubDate>Tue, 14 Aug 2012 03:02:00 GMT</pubDate><guid>http://www.cppblog.com/shixiyu/archive/2012/08/14/187138.html</guid><wfw:comment>http://www.cppblog.com/shixiyu/comments/187138.html</wfw:comment><comments>http://www.cppblog.com/shixiyu/archive/2012/08/14/187138.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/shixiyu/comments/commentRss/187138.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/shixiyu/services/trackbacks/187138.html</trackback:ping><description><![CDATA[<span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">虚函数联系到多态，多态联系到继承。所以本文中都是在继承层次上做文章。没了继承，什么都没得谈。&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">下面是对C++的虚函数这玩意儿的理解。&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">一， 什么是虚函数（如果不知道虚函数为何物，但有急切的想知道，那你就应该从这里开始）&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">简单地说，那些被virtual关键字修饰的成员函数，就是虚函数。虚函数的作用，用专业术语来解释就是实现多态性（Polymorphism），多态性是将接口与实现进行分离；用形象的语言来解释就是实现以共同的方法，但因个体差异而采用不同的策略。下面来看一段简单的代码&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">class A{&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">public:&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">void print(){ cout&lt;&lt;&#8221;This is A&#8221;&lt;&lt;endl;}&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">};&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">class B:public A{&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">public:&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">void print(){ cout&lt;&lt;&#8221;This is B&#8221;&lt;&lt;endl;}&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">};&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">int main(){ //为了在以后便于区分，我这段main()代码叫做main1&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">A a;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">B b;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">a.print();&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">b.print();&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">}&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">通过class A和class B的print()这个接口，可以看出这两个class因个体的差异而采用了不同的策略，输出的结果也是我们预料中的，分别是This is A和This is B。但这是否真正做到了多态性呢？No，多态还有个关键之处就是一切用指向基类的指针或引用来操作对象。那现在就把main()处的代码改一改。&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">int main(){ //main2&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">A a;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">B b;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">A* p1=&amp;a;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">A* p2=&amp;b;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">p1-&gt;print();&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">p2-&gt;print();&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">}&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">运行一下看看结果，哟呵，蓦然回首，结果却是两个This is A。问题来了，p2明明指向的是class B的对象但却是调用的class A的print()函数，这不是我们所期望的结果，那么解决这个问题就需要用到虚函数&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">class A{&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">public:&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">virtual void print(){ cout&lt;&lt;&#8221;This is A&#8221;&lt;&lt;endl;} //现在成了虚函数了&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">};&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">class B:public A{&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">public:&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">void print(){ cout&lt;&lt;&#8221;This is B&#8221;&lt;&lt;endl;} //这里需要在前面加上关键字virtual吗？&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">};&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">毫无疑问，class A的成员函数print()已经成了虚函数，那么class B的print()成了虚函数了吗？回答是Yes，我们只需在把基类的成员函数设为virtual，其派生类的相应的函数也会自动变为虚函数。所以，class B的print()也成了虚函数。那么对于在派生类的相应函数前是否需要用virtual关键字修饰，那就是你自己的问题了。&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">现在重新运行main2的代码，这样输出的结果就是This is A和This is B了。&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">现在来消化一下，我作个简单的总结，指向基类的指针在操作它的多态类对象时，会根据不同的类对象，调用其相应的函数，这个函数就是虚函数。&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">二， 虚函数是如何做到的（如果你没有看过《Inside The C++ Object Model》这本书，但又急切想知道，那你就应该从这里开始）&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">虚函数是如何做到因对象的不同而调用其相应的函数的呢？现在我们就来剖析虚函数。我们先定义两个类&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">class A{ //虚函数示例代码&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">public:&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">virtual void fun(){cout&lt;&lt;1&lt;&lt;endl;}&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">virtual void fun2(){cout&lt;&lt;2&lt;&lt;endl;}&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">};&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">class B:public A{&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">public:&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">void fun(){cout&lt;&lt;3&lt;&lt;endl;}&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">void fun2(){cout&lt;&lt;4&lt;&lt;endl;}&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">};&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">由于这两个类中有虚函数存在，所以编译器就会为他们两个分别插入一段你不知道的数据，并为他们分别创建一个表。那段数据叫做vptr指针，指向那个表。那个表叫做vtbl，每个类都有自己的vtbl，vtbl的作用就是保存自己类中虚函数的地址，我们可以把vtbl形象地看成一个数组，这个数组的每个元素存放的就是虚函数的地址，请看图&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">通过上图，可以看到这两个vtbl分别为class A和class B服务。现在有了这个模型之后，我们来分析下面的代码&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">A *p=new A;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">p-&gt;fun();&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">毫无疑问，调用了A::fun()，但是A::fun()是如何被调用的呢？它像普通函数那样直接跳转到函数的代码处吗？No，其实是这样的，首先是取出vptr的值，这个值就是vtbl的地址，再根据这个值来到vtbl这里，由于调用的函数A::fun()是第一个虚函数，所以取出vtbl第一个slot里的值，这个值就是A::fun()的地址了，最后调用这个函数。现在我们可以看出来了，只要vptr不同，指向的vtbl就不同，而不同的vtbl里装着对应类的虚函数地址，所以这样虚函数就可以完成它的任务。&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">而对于class A和class B来说，他们的vptr指针存放在何处呢？其实这个指针就放在他们各自的实例对象里。由于class A和class B都没有数据成员，所以他们的实例对象里就只有一个vptr指针。通过上面的分析，现在我们来实作一段代码，来描述这个带有虚函数的类的简单模型。&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">#include&lt;iostream&gt;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">using namespace std;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">//将上面&#8220;虚函数示例代码&#8221;添加在这里&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">int main(){&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">void (*fun)(A*);&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">A *p=new B;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">long lVptrAddr;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">memcpy(&amp;lVptrAddr,p,4);&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">memcpy(&amp;fun,reinterpret_cast&lt;long*&gt;(lVptrAddr),4);&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">fun(p);&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">delete p;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">system("pause");&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">}&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">用VC或Dev-C++编译运行一下，看看结果是不是输出3，如果不是，那么太阳明天肯定是从西边出来。现在一步一步开始分析&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">void (*fun)(A*); 这段定义了一个函数指针名字叫做fun，而且有一个A*类型的参数，这个函数指针待会儿用来保存从vtbl里取出的函数地址&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">A* p=new B; 这个我不太了解，算了，不解释这个了&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">long lVptrAddr; 这个long类型的变量待会儿用来保存vptr的值&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">memcpy(&amp;lVptrAddr,p,4); 前面说了，他们的实例对象里只有vptr指针，所以我们就放心大胆地把p所指的4bytes内存里的东西复制到lVptrAddr中，所以复制出来的4bytes内容就是vptr的值，即vtbl的地址&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">现在有了vtbl的地址了，那么我们现在就取出vtbl第一个slot里的内容&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">memcpy(&amp;fun,reinterpret_cast&lt;long*&gt;(lVptrAddr),4); 取出vtbl第一个slot里的内容，并存放在函数指针fun里。需要注意的是lVptrAddr里面是vtbl的地址，但lVptrAddr不是指针，所以我们要把它先转变成指针类型&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">fun(p); 这里就调用了刚才取出的函数地址里的函数，也就是调用了B::fun()这个函数，也许你发现了为什么会有参数p,其实类成员函数调用时，会有个this指针，这个p就是那个this指针，只是在一般的调用中编译器自动帮你处理了而已，而在这里则需要自己处理。&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">delete p;和system("pause"); 这个我不太了解，算了，不解释这个了&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">如果调用B::fun2()怎么办？那就取出vtbl的第二个slot里的值就行了&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">memcpy(&amp;fun,reinterpret_cast&lt;long*&gt;(lVptrAddr+4),4); 为什么是加4呢？因为一个指针的长度是4bytes，所以加4。或者memcpy(&amp;fun,reinterpret_cast&lt;long*&gt;(lVptrAddr)+1,4); 这更符合数组的用法，因为lVptrAddr被转成了long*型别，所以+1就是往后移sizeof(long)的长度&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">三， 以一段代码开始&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">#include&lt;iostream&gt;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">using namespace std;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">class A{ //虚函数示例代码2&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">public:&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">virtual void fun(){ cout&lt;&lt;"A::fun"&lt;&lt;endl;}&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">virtual void fun2(){cout&lt;&lt;"A::fun2"&lt;&lt;endl;}&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">};&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">class B:public A{&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">public:&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">void fun(){ cout&lt;&lt;"B::fun"&lt;&lt;endl;}&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">void fun2(){ cout&lt;&lt;"B::fun2"&lt;&lt;endl;}&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">}; //end//虚函数示例代码2&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">int main(){&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">void (A::*fun)(); //定义一个函数指针&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">A *p=new B;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">fun=&amp;A::fun;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">(p-&gt;*fun)();&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">fun = &amp;A::fun2;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">(p-&gt;*fun)();&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">delete p;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">system("pause");&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">}&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">你能估算出输出结果吗？如果你估算出的结果是A::fun和A::fun2，呵呵，恭喜恭喜，你中圈套了。其实真正的结果是B::fun和B::fun2，如果你想不通就接着往下看。给个提示，&amp;A::fun和&amp;A::fun2是真正获得了虚函数的地址吗？&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">首先我们回到第二部分，通过段实作代码，得到一个&#8220;通用&#8221;的获得虚函数地址的方法&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">#include&lt;iostream&gt;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">using namespace std;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">//将上面&#8220;虚函数示例代码2&#8221;添加在这里&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">void CallVirtualFun(void* pThis,int index=0){&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">void (*funptr)(void*);&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">long lVptrAddr;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">memcpy(&amp;lVptrAddr,pThis,4);&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">memcpy(&amp;funptr,reinterpret_cast&lt;long*&gt;(lVptrAddr)+index,4);&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">funptr(pThis); //调用&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">}&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">int main(){&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">A* p=new B;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">CallVirtualFun(p); //调用虚函数p-&gt;fun()&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">CallVirtualFun(p,1);//调用虚函数p-&gt;fun2()&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">system("pause");&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">}&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">现在我们拥有一个&#8220;通用&#8221;的CallVirtualFun方法。&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">这个通用方法和第三部分开始处的代码有何联系呢？联系很大。由于A::fun()和A::fun2()是虚函数，所以&amp;A::fun和&amp;A::fun2获得的不是函数的地址，而是一段间接获得虚函数地址的一段代码的地址，我们形象地把这段代码看作那段CallVirtualFun。编译器在编译时，会提供类似于CallVirtualFun这样的代码，当你调用虚函数时，其实就是先调用的那段类似CallVirtualFun的代码，通过这段代码，获得虚函数地址后，最后调用虚函数，这样就真正保证了多态性。同时大家都说虚函数的效率低，其原因就是，在调用虚函数之前，还调用了获得虚函数地址的代码。&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; " /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">最后的说明：本文的代码可以用VC6和Dev-C++4.9.8.0通过编译，且运行无问题。其他的编译器小弟不敢保证。其中，里面的类比方法只能看成模型，因为不同的编译器的低层实现是不同的。例如this指针，Dev-C++的gcc就是通过压栈，当作参数传递，而VC的编译器则通过取出地址保存在ecx中。所以这些类比方法不能当作具体实现</span>&nbsp;<img src ="http://www.cppblog.com/shixiyu/aggbug/187138.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/shixiyu/" target="_blank">爱走小路</a> 2012-08-14 11:02 <a href="http://www.cppblog.com/shixiyu/archive/2012/08/14/187138.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>MFC Cstring 类的用法 </title><link>http://www.cppblog.com/shixiyu/archive/2012/08/10/186841.html</link><dc:creator>爱走小路</dc:creator><author>爱走小路</author><pubDate>Fri, 10 Aug 2012 09:48:00 GMT</pubDate><guid>http://www.cppblog.com/shixiyu/archive/2012/08/10/186841.html</guid><wfw:comment>http://www.cppblog.com/shixiyu/comments/186841.html</wfw:comment><comments>http://www.cppblog.com/shixiyu/archive/2012/08/10/186841.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/shixiyu/comments/commentRss/186841.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/shixiyu/services/trackbacks/186841.html</trackback:ping><description><![CDATA[<table style="width: 100%; table-layout: fixed">
<tbody>
<tr>
<td>
<div id="blog_text" class="cnt">CString位于头文件afx.h中。 
<div class="postbody">
<div style="line-height: 26px; text-indent: 2em; height: auto !important" class="real_blog">
<table style="width: 1085px; height: 5442px" class="FCK__ShowTableBorders" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td style="word-wrap: break-word" valign="top">
<div><font size="5"><img src="http://qzone.qq.com/ac/b.gif" width="1" height="1" idx="0" eventslistuid="e2"  alt="" /> </font>
<div align="center"></div><font size="5">CString 的 成员函数<img src="http://qzone.qq.com/ac/b.gif" width="1" height="1" idx="1" eventslistuid="e3"  alt="" /></font> 
<div style="font-size: 16px"><font style="line-height: 1.5em" color="#0000cc" size="3"><strong><wbr>CString的构造函数</strong><wbr></font><wbr><br />CString( );<br />例：CString csStr;<br /><br />CString( const CString&amp; stringSrc );<br />例：CString csStr("ABCDEF中文123456");<br />CString csStr2(csStr);<br /><br />CString( TCHAR ch, int nRepeat = 1 );<br />例：CString csStr('a',5);<br /><font style="line-height: 1.5em" color="#990000" size="3">//csStr="aaaaa"</font><wbr><br /><br />CString( LPCTSTR lpch, int nLength );<br />例：CString csStr("abcdef",3);<br /><font style="line-height: 1.5em" color="#990000" size="3">//csStr="abc"</font><wbr><br /><br />CString( LPCWSTR lpsz );<br />例：wchar_t s[]=L"abcdef";<br />CString csStr(s);<br /><font style="line-height: 1.5em" color="#990000" size="3">//csStr=L"abcdef"</font><wbr><br /><br />CString( const unsigned char* psz );<br />例：const unsigned char s[]="abcdef";<br />const unsigned char* sp=s;<br />CString csStr(sp);<br /><font style="line-height: 1.5em" color="#990000" size="3">//csStr="abcdef"</font><wbr><br /><br />CString( LPCSTR lpsz );<br />例：CString csStr("abcdef");<br /><font style="line-height: 1.5em" color="#990000" size="3">//csStr="abcdef"</font><wbr><br /><br /><font style="line-height: 1.5em" color="#0000cc" size="3"><strong><wbr>int GetLength( ) const;</strong><wbr></font><wbr><br />返回字符串的长度，不包含结尾的空字符。<br />例：csStr="ABCDEF中文123456";<br />printf("%d",csStr.GetLength()); <font style="line-height: 1.5em" color="#990000" size="3">//16</font><wbr><br /><br /><font style="line-height: 1.5em" color="#0000cc" size="3"><strong><wbr>void MakeReverse( );</strong><wbr></font><wbr><br />颠倒字符串的顺序<br />例：csStr="ABCDEF中文123456";<br />csStr.MakeReverse();<br />cout&lt;&lt;csStr; <font style="line-height: 1.5em" color="#990000" size="3">//654321文中FEDCBA</font><wbr><br /><br /><strong><wbr><font style="line-height: 1.5em" color="#0000cc" size="3">void MakeUpper( );</font><wbr></strong><wbr><br />将小写字母转换为大写字母<br />例：csStr="abcdef中文123456";<br />csStr.MakeUpper();<br />cout&lt;&lt;csStr; <font style="line-height: 1.5em" color="#990000" size="3">//ABCDEF中文123456</font><wbr><br /><br /><font style="line-height: 1.5em" color="#0000cc" size="3"><strong><wbr>void MakeLower( );</strong><wbr></font><wbr><br />将大写字母转换为小写字母<br />例：csStr="ABCDEF中文123456";<br />csStr.MakeLower();<br />cout&lt;&lt;csStr; <font style="line-height: 1.5em" color="#990000" size="3">//abcdef中文123456</font><wbr><br /><br /><font style="line-height: 1.5em" color="#0000cc" size="3"><strong><wbr>int Compare( LPCTSTR lpsz ) const;</strong><wbr></font><wbr><br />区分大小写比较两个字符串，相等时返回0，大于时返回1，小于时返回-1<br />例：csStr="abcdef中文123456";<br />csStr2="ABCDEF中文123456";<br />cout&lt;&lt;csStr.CompareNoCase(csStr2); <font style="line-height: 1.5em" color="#990000" size="3">//0</font><wbr><br /><br /><font style="line-height: 1.5em" color="#0000cc" size="3"><strong><wbr>int CompareNoCase( LPCTSTR lpsz ) const;</strong><wbr></font><wbr><br />不区分大小写比较两个字符串，相等时返回0，大于时返回1，小于时返回-1<br />例：csStr="abcdef中文123456";<br />csStr2="ABCDEF中文123456";<br />cout&lt;&lt;csStr.CompareNoCase(csStr2); <font style="line-height: 1.5em" color="#990000" size="3">//-1</font><wbr><br /><br /><font style="line-height: 1.5em" color="#0000cc" size="3"><strong><wbr>int Delete( int nIndex, int nCount = 1 )</strong><wbr></font><wbr><br />删除字符，删除从下标nIndex开始的nCount个字符<br />例：csStr="ABCDEF";<br />csStr.Delete(2,3);<br />cout&lt;&lt;csStr; <font style="line-height: 1.5em" color="#990000" size="3">// ABF<br />//当nIndex过大，超出对像所在内存区域时，函数没有任何操作。<br />//当nIndex为负数时，从第一个字符开始删除。<br />//当nCount过大，导致删除字符超出对像所在内存区域时，会发生无法预料的结果。<br />//当nCount为负数时，函数没有任何操作。</font><wbr><br /><br /><font style="line-height: 1.5em" color="#0000cc" size="3"><strong><wbr>int Insert( int nIndex, TCHAR ch )<br />int Insert( int nIndex, LPCTSTR pstr )</strong><wbr></font><wbr><br />在下标为nIndex的位置，插入字符或字符串。返回插入后对象的长度<br />例：csStr="abc";<br />csStr.Insert(2,'x');<br />cout&lt;&lt;csStr; <font style="line-height: 1.5em" color="#990000" size="3">//abxc<br /></font><wbr>csStr="abc";<br />csStr.Insert(2,"xyz");<br />cout&lt;&lt;csStr; <font style="line-height: 1.5em" color="#990000" size="3">//abxyzc</font><wbr><br /><font style="line-height: 1.5em" color="#990000" size="3">//当nIndex为负数时，插入在对象开头<br />//当nIndex超出对象末尾时，插入在对象末尾</font><wbr><br /><br /><font style="line-height: 1.5em" color="#0000cc" size="3"><strong><wbr>int Remove( TCHAR ch );</strong><wbr></font><wbr><br />移除对象内的指定字符。返回移除的数目<br />例：csStr="aabbaacc";<br />csStr.Remove('a');<br />cout&lt;&lt;csStr; <font style="line-height: 1.5em" color="#990000" size="3">//bbcc</font><wbr><br /><br /><font style="line-height: 1.5em" color="#0000cc" size="3"><strong><wbr>int Replace( TCHAR chOld, TCHAR chNew );<br />int Replace( LPCTSTR lpszOld, LPCTSTR lpszNew );</strong><wbr></font><wbr><br />替换字串<br />例：csStr="abcdef";<br />csStr.Replace('a','x');<br />cout&lt;&lt;csStr; <font style="line-height: 1.5em" color="#990000" size="3">//xbcdef</font><wbr><br />csStr="abcdef";<br />csStr.Replace("abc","xyz");<br />cout&lt;&lt;csStr; <font style="line-height: 1.5em" color="#990000" size="3">//xyzdef</font><wbr><br /><br /><font style="line-height: 1.5em" color="#0000cc" size="3"><strong><wbr>void TrimLeft( );<br />void TrimLeft( TCHAR chTarget );<br />void TrimLeft( LPCTSTR lpszTargets );</strong><wbr></font><wbr><br />从左删除字符，被删的字符与chTarget或lpszTargets匹配，一直删到第一个不匹配的字符为止<br />例：csStr="aaabaacdef";<br />csStr.TrimLeft('a');<br />cout&lt;&lt;csStr; <font style="line-height: 1.5em" color="#990000" size="3">//baacdef<br /></font><wbr>csStr="aaabaacdef";<br />csStr.TrimLeft("ab");<br />cout&lt;&lt;csStr; <font style="line-height: 1.5em" color="#990000" size="3">//cdef</font><wbr><br /><font style="line-height: 1.5em" color="#990000" size="3">//无参数时删除空格</font><wbr><br /><br /><font style="line-height: 1.5em" color="#0000cc" size="3"><strong><wbr>void TrimRight( );<br />void TrimRight( TCHAR chTarget );<br />void TrimRight( LPCTSTR lpszTargets );</strong><wbr></font><wbr><br />从右删除字符，被删的字符与chTarget或lpszTargets匹配，一直删到第一个不匹配的字符为止<br />例：csStr="abcdeaafaaa";<br />csStr.TrimRight('a');<br />cout&lt;&lt;csStr; <font style="line-height: 1.5em" color="#990000" size="3">//abcdeaaf</font><wbr><br />csStr="abcdeaafaaa";<br />csStr.TrimRight("fa");<br />cout&lt;&lt;csStr; <font style="line-height: 1.5em" color="#990000" size="3">//abcde<br />//无参数时删除空格</font><wbr><br /><br /><font style="line-height: 1.5em" color="#0000cc" size="3"><strong><wbr>void Empty( );</strong><wbr></font><wbr><br />清空<br />例：csStr="abcdef";<br />csStr.Empty();<br />printf("%d",csStr.GetLength()); <font style="line-height: 1.5em" color="#990000" size="3">//0</font><wbr><br /><br /><font style="line-height: 1.5em" color="#0000cc" size="3"><strong><wbr>BOOL IsEmpty( ) const;</strong><wbr></font><wbr><br />测试对象是否为空，为空时返回零，不为空时返回非零<br />例：csStr="abc";<br />cout&lt;&lt;csStr.IsEmpty(); <font style="line-height: 1.5em" color="#990000" size="3">//0;<br /></font><wbr>csStr.Empty();<br />cout&lt;&lt;csStr.IsEmpty(); <font style="line-height: 1.5em" color="#990000" size="3">//1;</font><wbr><br /><br /><font style="line-height: 1.5em" color="#0000cc" size="3"><strong><wbr>int Find( TCHAR ch ) const;<br />int Find( LPCTSTR lpszSub ) const;<br />int Find( TCHAR ch, int nStart ) const;<br />int Find( LPCTSTR pstr, int nStart ) const;</strong><wbr></font><wbr><br />查找字串，nStart为开始查找的位置。未找到匹配时返回-1，否则返回字串的开始位置<br />例：csStr="abcdef";<br />cout&lt;&lt;csStr.Find('b'); <font style="line-height: 1.5em" color="#990000" size="3">//1<br /></font><wbr>cout&lt;&lt;csStr.Find("de"); <font style="line-height: 1.5em" color="#990000" size="3">//3</font><wbr><br />cout&lt;&lt;csStr.Find('b',3); <font style="line-height: 1.5em" color="#990000" size="3">//-1</font><wbr><br />cout&lt;&lt;csStr.Find('b',0); <font style="line-height: 1.5em" color="#990000" size="3">//1</font><wbr><br />cout&lt;&lt;csStr.Find("de",4); <font style="line-height: 1.5em" color="#990000" size="3">//-1</font><wbr><br />cout&lt;&lt;csStr.Find("de",0); <font style="line-height: 1.5em" color="#990000" size="3">//3</font><wbr><br /><font style="line-height: 1.5em" color="#990000" size="3">//当nStart超出对象末尾时，返回-1。<br />//当nStart为负数时，返回-1。</font><wbr><br /><br /><font style="line-height: 1.5em" color="#0000cc" size="3"><strong><wbr>int FindOneOf( LPCTSTR lpszCharSet ) const;</strong><wbr></font><wbr><br />查找lpszCharSet中任意一个字符在CString对象中的匹配位置。未找到时返回-1，否则返回字串的开始位置<br />例：csStr="abcdef";<br />cout&lt;&lt;csStr.FindOneOf("cxy"); <font style="line-height: 1.5em" color="#990000" size="3">//2</font><wbr><br /><br /><font style="line-height: 1.5em" color="#0000cc" size="3"><strong><wbr>CString SpanExcluding( LPCTSTR lpszCharSet ) const;</strong><wbr></font><wbr><br />返回对象中与lpszCharSet中任意匹配的第一个字符之前的子串<br />例：csStr="abcdef";<br />cout&lt;&lt;csStr.SpanExcluding("cf"); <font style="line-height: 1.5em" color="#990000" size="3">//ab</font><wbr><br /><br /><font style="line-height: 1.5em" color="#0000cc" size="3"><strong><wbr>CString SpanIncluding( LPCTSTR lpszCharSet ) const;</strong><wbr></font><wbr><br />从对象中查找与lpszCharSe中任意字符不匹配的字符，并返回第一个不匹配字符之前的字串<br />例：csStr="abcdef";<br />cout&lt;&lt;csStr.SpanIncluding("fdcba"); <font style="line-height: 1.5em" color="#990000" size="3">//abcd</font><wbr><br /><br /><font style="line-height: 1.5em" color="#0000cc" size="3"><strong><wbr>int ReverseFind( TCHAR ch ) const;</strong><wbr></font><wbr><br />从后向前查找第一个匹配，找到时返回下标。没找到时返回-1<br />例：csStr="abba";<br />cout&lt;&lt;csStr.ReverseFind('a'); <font style="line-height: 1.5em" color="#990000" size="3">//3</font><wbr><br /><br /><font style="line-height: 1.5em" color="#0000cc" size="3"><strong><wbr>void Format( LPCTSTR lpszFormat, ... );<br />void Format( UINT nFormatID, ... );</strong><wbr></font><wbr><br />格式化对象，与C语言的sprintf函数用法相同<br />例：csStr.Format("%d",13);<br />cout&lt;&lt;csStr; <font style="line-height: 1.5em" color="#990000" size="3">//13</font><wbr><br /><br /><font style="line-height: 1.5em" color="#0000cc" size="3"><strong><wbr>TCHAR GetAt( int nIndex ) const;</strong><wbr></font><wbr><br />返回下标为nIndex的字符，与字符串的[]用法相同<br />例：csStr="abcdef";<br />cout&lt;&lt;csStr.GetAt(2); <font style="line-height: 1.5em" color="#990000" size="3">//c<br />//当nIndex为负数或超出对象末尾时，会发生无法预料的结果。</font><wbr><br /><br /><font style="line-height: 1.5em" color="#0000cc" size="3"><strong><wbr>void SetAt( int nIndex, TCHAR ch );</strong><wbr></font><wbr><br />给下标为nIndex的字符重新赋值<br />例：csStr="abcdef";<br />csStr.SetAt(2,'x');<br />cout&lt;&lt;csStr; <font style="line-height: 1.5em" color="#990000" size="3">//abxdef<br />//当nIndex为负数或超出对象末尾时，会发生无法预料的结果。</font><wbr><br /><br /><font style="line-height: 1.5em" color="#0000cc" size="3"><strong><wbr>CString Left( int nCount ) const;</strong><wbr></font><wbr><br />从左取字串<br />例：csStr="abcdef";<br />cout&lt;&lt;csStr.Left(3); <font style="line-height: 1.5em" color="#990000" size="3">//abc<br />//当nCount等于0时，返回空。<br />//当nCount为负数时，返回空。<br />//当nCount大于对象长度时，返回值与对象相同。</font><wbr><br /><br /><font style="line-height: 1.5em" color="#0000cc" size="3"><strong><wbr>CString Right( int nCount ) const;</strong><wbr></font><wbr><br />从右取字串<br />例：csStr="abcdef";<br />cout&lt;&lt;csStr.Right(3); <font style="line-height: 1.5em" color="#990000" size="3">//def<br />//当nCount等于0时，返回空。<br />//当nCount为负数时，返回空。<br />//当nCount大于对象长度时，返回值与对象相同。</font><wbr><br /><br /><font style="line-height: 1.5em" color="#0000cc" size="3"><strong><wbr>CString Mid( int nFirst ) const;<br />CString Mid( int nFirst, int nCount ) const;</strong><wbr></font><wbr><br />从中间开始取字串<br />例：csStr="abcdef";<br />cout&lt;&lt;csStr.Mid(2); <font style="line-height: 1.5em" color="#990000" size="3">//cdef<br /></font><wbr>csStr="abcdef";<br />cout&lt;&lt;csStr.Mid(2,3); <font style="line-height: 1.5em" color="#990000" size="3">//cde<br />//当nFirst为0和为负数时，从第一个字符开始取。<br />//当nFirst等于对象末尾时，返回空字串。<br />//当nFirst超出对象末尾时，会发生无法预料的结果。<br />//当nCount超出对象末尾时，返回从nFirst开始一直到对象末尾的字串<br />//当nCount为0和为负数时，返回空字串。</font><wbr><br /><br /><font style="line-height: 1.5em" color="#0000cc" size="3"><strong><wbr>LPTSTR GetBuffer( int nMinBufLength );</strong><wbr></font><wbr><br />申请新的空间，并返回指针<br />例：csStr="abcde";<br />LPTSTR pStr=csStr.GetBuffer(10);<br />strcpy(pStr,"12345");<br />csStr.ReleaseBuffer();<br />pStr=NULL;<br />cout&lt;&lt;csStr <font style="line-height: 1.5em" color="#990000" size="3">//12345<br />//使用完GetBuffer后，必须使用ReleaseBuffer以更新对象内部数据，否则会发生无法预料的结果。</font><wbr><br /><br /><font style="line-height: 1.5em" color="#0000cc" size="3"><strong><wbr>void ReleaseBuffer( int nNewLength = -1 );</strong><wbr></font><wbr><br />使用GetBuffer后，必须使用ReleaseBuffer以更新对象内部数据<br />例：csStr="abc";<br />LPTSTR pStr=csStr.GetBuffer(10);<br />strcpy(pStr,"12345");<br />cout&lt;&lt;csStr.GetLength(); <font style="line-height: 1.5em" color="#990000" size="3">//3(错误的用法)</font><wbr><br />csStr.ReleaseBuffer();<br />cout&lt;&lt;csStr.GetLength(); <font style="line-height: 1.5em" color="#990000" size="3">//5(正确)<br /></font><wbr>pStr=NULL;<br /><font style="line-height: 1.5em" color="#990000" size="3">//CString对象的任何方法都应在ReleaseBuffer之后调用</font><wbr><br /><br /><font style="line-height: 1.5em" color="#0000cc" size="3"><strong><wbr>LPTSTR GetBufferSetLength( int nNewLength );</strong><wbr></font><wbr><br />申请新的空间，并返回指针<br />例：csStr="abc";<br />csStr.GetBufferSetLength(20);<br />cout&lt;&lt;csStr; <font style="line-height: 1.5em" color="#990000" size="3">//abc<br /></font><wbr>count&lt;&lt;csStr.GetLength(); <font style="line-height: 1.5em" color="#990000" size="3">//20;<br /></font><wbr>csStr.ReleaseBuffer();<br />count&lt;&lt;csStr.GetLength(); <font style="line-height: 1.5em" color="#990000" size="3">//3;<br />//使用GetBufferSetLength后可以不必使用ReleaseBuffer。<br /></font><wbr></div></div></td></tr></tbody></table></div></div></div></td></tr></tbody></table><img src ="http://www.cppblog.com/shixiyu/aggbug/186841.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/shixiyu/" target="_blank">爱走小路</a> 2012-08-10 17:48 <a href="http://www.cppblog.com/shixiyu/archive/2012/08/10/186841.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C++头文件一览 </title><link>http://www.cppblog.com/shixiyu/archive/2012/08/10/186840.html</link><dc:creator>爱走小路</dc:creator><author>爱走小路</author><pubDate>Fri, 10 Aug 2012 09:47:00 GMT</pubDate><guid>http://www.cppblog.com/shixiyu/archive/2012/08/10/186840.html</guid><wfw:comment>http://www.cppblog.com/shixiyu/comments/186840.html</wfw:comment><comments>http://www.cppblog.com/shixiyu/archive/2012/08/10/186840.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/shixiyu/comments/commentRss/186840.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/shixiyu/services/trackbacks/186840.html</trackback:ping><description><![CDATA[<p>C、传统 C++</p>
<p>#include &lt;assert.h&gt;　　　　设定插入点<br />#include &lt;ctype.h&gt;　　　　字符处理<br />#include &lt;errno.h&gt;　　　　 定义错误码<br />#include &lt;float.h&gt;　　　　浮点数处理<br />#include &lt;fstream.h&gt;　　　文件输入／输出<br />#include &lt;iomanip.h&gt;　　　 参数化输入／输出<br />#include &lt;iostream.h&gt;　　　数据流输入／输出<br />#include &lt;limits.h&gt;　　　　定义各种数据类型最值常量<br />#include &lt;locale.h&gt;　　　　定义本地化函数<br />#include &lt;math.h&gt;　　　　　定义数学函数<br />#include &lt;stdio.h&gt;　　　　定义输入／输出函数<br />#include &lt;stdlib.h&gt;　　　　定义杂项函数及内存分配函数<br />#include &lt;string.h&gt;　　　　字符串处理<br />#include &lt;strstrea.h&gt;　　　基于数组的输入／输出<br />#include &lt;time.h&gt;　　　　　定义关于时间的函数<br />#include &lt;wchar.h&gt;　　　　 宽字符处理及输入／输出<br />#include &lt;wctype.h&gt;　　　　宽字符分类</p>
<p>标准 C++　</p>
<p>#include &lt;algorithm&gt;　　　 通用算法<br />#include &lt;bitset&gt;　　　　　 位集容器<br />#include &lt;cctype&gt;<br />#include &lt;cerrno&gt;<br />#include &lt;clocale&gt;<br />#include &lt;cmath&gt;<br />#include &lt;complex&gt;　　　　 复数类<br />#include &lt;cstdio&gt;<br />#include &lt;cstdlib&gt;<br />#include &lt;cstring&gt;<br />#include &lt;ctime&gt;<br />#include &lt;deque&gt;　　　　　 双端队列容器<br />#include &lt;exception&gt;　　　 异常处理类<br />#include &lt;fstream&gt;<br />#include &lt;functional&gt;　　　 定义运算函数（代替运算符）<br />#include &lt;limits&gt;<br />#include &lt;list&gt;　　　　　　 线性列表容器<br />#include &lt;map&gt;　　　　　　 映射容器<br />#include &lt;iomanip&gt;<br />#include &lt;ios&gt;　　　　　　基本输入／输出支持<br />#include &lt;iosfwd&gt;　　　　输入／输出系统使用的前置声明<br />#include &lt;iostream&gt;<br />#include &lt;istream&gt;　　　　 基本输入流<br />#include &lt;ostream&gt;　　　　 基本输出流<br />#include &lt;queue&gt;　　　　　 队列容器<br />#include &lt;set&gt;　　　　　　 集合容器<br />#include &lt;sstream&gt;　　　　 基于字符串的流<br />#include &lt;stack&gt;　　　　　 堆栈容器　　　　<br />#include &lt;stdexcept&gt;　　　 标准异常类<br />#include &lt;streambuf&gt;　　　底层输入／输出支持<br />#include &lt;string&gt;　　　　　字符串类<br />#include &lt;utility&gt;　　　　 通用模板类<br />#include &lt;vector&gt;　　　　 动态数组容器<br />#include &lt;cwchar&gt;<br />#include &lt;cwctype&gt;</p>
<p>C99 增加</p>
<p>#include &lt;complex.h&gt;　　复数处理<br />#include &lt;fenv.h&gt;　　　　浮点环境<br />#include &lt;inttypes.h&gt;　　整数格式转换<br />#include &lt;stdbool.h&gt;　　 布尔环境<br />#include &lt;stdint.h&gt;　　　整型环境<br />#include &lt;tgmath.h&gt;　　通用类型数学宏<br /></p><img src ="http://www.cppblog.com/shixiyu/aggbug/186840.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/shixiyu/" target="_blank">爱走小路</a> 2012-08-10 17:47 <a href="http://www.cppblog.com/shixiyu/archive/2012/08/10/186840.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>学C++的忠告</title><link>http://www.cppblog.com/shixiyu/archive/2012/08/10/186839.html</link><dc:creator>爱走小路</dc:creator><author>爱走小路</author><pubDate>Fri, 10 Aug 2012 09:46:00 GMT</pubDate><guid>http://www.cppblog.com/shixiyu/archive/2012/08/10/186839.html</guid><wfw:comment>http://www.cppblog.com/shixiyu/comments/186839.html</wfw:comment><comments>http://www.cppblog.com/shixiyu/archive/2012/08/10/186839.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/shixiyu/comments/commentRss/186839.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/shixiyu/services/trackbacks/186839.html</trackback:ping><description><![CDATA[<p>50条忠告：（其中有几条觉得写的不够贴切，所以删了，发了余下的部分）</p>
<p>&nbsp;</p>
<p>1.把C++当成一门新的语言学习； </p>
<p>2.看《Thinking In C++》，不要看《C++变成死相》； </p>
<p>3.看《The C++ Programming Language》和《Inside The C++ Object Model》,不要因为他们很难而我们自己是初学者所以就不看； </p>
<p>4.不要被VC、BCB、BC、MC、TC等词汇所迷惑&#8212;&#8212;他们都是集成开发环境，而我们要学的是一门语言； </p>
<p>5.不要放过任何一个看上去很简单的小编程问题&#8212;&#8212;他们往往并不那么简单，或者可以引伸出很多知识点； </p>
<p>6.会用Visual C++，并不说明你会C++； </p>
<p>7.学class并不难，template、STL、generic programming也不过如此&#8212;&#8212;难的是长期坚持实践和不遗余力的博览群书； </p>
<p>8.如果不是天才的话，想学编程就不要想玩游戏&#8212;&#8212;你以为你做到了，其实你的C++水平并没有和你通关的能力一起变高&#8212;&#8212;其实可以时刻记住：学C++是为了编游戏的； </p>
<p>9.看Visual C++的书，是学不了C++语言的； </p>
<p>16.把时髦的技术挂在嘴边，还不如把过时的技术记在心里； </p>
<p>18.学习编程最好的方法之一就是阅读源代码； </p>
<p>19.在任何时刻都不要认为自己手中的书已经足够了； </p>
<p>20.请阅读《The Standard C++ Bible》(中文版：标准C++宝典)，掌握C++标准； </p>
<p>21.看得懂的书，请仔细看；看不懂的书，请硬着头皮看； </p>
<p>22.别指望看第一遍书就能记住和掌握什么&#8212;&#8212;请看第二遍、第三遍； </p>
<p>23.请看《Effective C++》和《More Effective C++》以及《Exceptional C++》； </p>
<p>24.不要停留在集成开发环境的摇篮上，要学会控制集成开发环境，还要学会用命令行方式处理程序； </p>
<p>25.和别人一起讨论有意义的C++知识点，而不是争吵XX行不行或者YY与ZZ哪个好； </p>
<p>26.请看《程序设计实践》，并严格的按照其要求去做； </p>
<p>27.不要因为C和C++中有一些语法和关键字看上去相同，就认为它们的意义和作用完全一样； </p>
<p>28.C++绝不是所谓的C的&#8220;扩充&#8221;&#8212;&#8212;如果C++一开始就起名叫Z语言，你一定不会把C和Z语言联系得那么紧密； </p>
<p>29.请不要认为学过XX语言再改学C++会有什么问题&#8212;&#8212;你只不过又在学一门全新的语言而已； </p>
<p>30.读完了《Inside The C++ Object Model》以后再来认定自己是不是已经学会了C++； </p>
<p>31.学习编程的秘诀是：编程，编程，再编程； </p>
<p>32.请留意下列书籍：《C++面向对象高效编程（C++ Effective Object-Oriented Software Construction）》《面向对象软件构造(Object-Oriented Software Construction)》《设计模式（Design Patterns）》《The Art of Computer Programming》； </p>
<p>34.请把书上的程序例子亲手输入到电脑上实践，即使配套光盘中有源代码； </p>
<p>35.把在书中看到的有意义的例子扩充； </p>
<p>36.请重视C++中的异常处理技术，并将其切实的运用到自己的程序中； </p>
<p>37.经常回顾自己以前写过的程序，并尝试重写，把自己学到的新知识运用进去； </p>
<p>38.不要漏掉书中任何一个练习题&#8212;&#8212;请全部做完并记录下解题思路； </p>
<p>39.C++语言和C++的集成开发环境要同时学习和掌握； </p>
<p>40.既然决定了学C++,就请坚持学下去，因为学习程序设计语言的目的是掌握程序设计技术，而程序设计技术是跨语言的； </p>
<p>41.就让C++语言的各种平台和开发环境去激烈的竞争吧，我们要以学习C++语言本身为主； </p>
<p>42.当你写C++程序写到一半却发现自己用的方法很拙劣时，请不要马上停手；请尽快将余下的部分粗略的完成以保证这个设计的完整性，然后分析自己的错误并重新设计和编写（参见43）； </p>
<p>43.别心急，设计C++的class确实不容易；自己程序中的class和自己的class设计水平是在不断的编程实践中完善和发展的； </p>
<p>44.决不要因为程序&#8220;很小&#8221;就不遵循某些你不熟练的规则&#8212;&#8212;好习惯是培养出来的，而不是一次记住的； </p>
<p>45.每学到一个C++难点的时候，尝试着对别人讲解这个知识点并让他理解&#8212;&#8212;你能讲清楚才说明你真的理解了； </p>
<p>46.记录下在和别人交流时发现的自己忽视或不理解的知识点； </p>
<p>47.请不断的对自己写的程序提出更高的要求,哪怕你的程序版本号会变成Version 100.XX； </p>
<p>48.保存好你写过的所有的程序&#8212;&#8212;那是你最好的积累之一； </p>
<p>49.请不要做浮躁的人； </p>
<p>50.请热爱C++! </p><img src ="http://www.cppblog.com/shixiyu/aggbug/186839.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/shixiyu/" target="_blank">爱走小路</a> 2012-08-10 17:46 <a href="http://www.cppblog.com/shixiyu/archive/2012/08/10/186839.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C++:STL标准入门汇总</title><link>http://www.cppblog.com/shixiyu/archive/2012/08/10/STL.html</link><dc:creator>爱走小路</dc:creator><author>爱走小路</author><pubDate>Fri, 10 Aug 2012 09:45:00 GMT</pubDate><guid>http://www.cppblog.com/shixiyu/archive/2012/08/10/STL.html</guid><wfw:comment>http://www.cppblog.com/shixiyu/comments/186838.html</wfw:comment><comments>http://www.cppblog.com/shixiyu/archive/2012/08/10/STL.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/shixiyu/comments/commentRss/186838.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/shixiyu/services/trackbacks/186838.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: C++:STL标准入门汇总 学无止境！！！第一部分：（参考百度百科）一、STL简介STL（Standard Template Library，标准模板库)是惠普实验室开发的一系列软件的统称。它是由Alexander Stepanov、Meng Lee和David R Musser在惠普实验室工作时所开发出来的。现在虽说它主要出现在C++中，但在被引入C++之前该技术就已经存...&nbsp;&nbsp;<a href='http://www.cppblog.com/shixiyu/archive/2012/08/10/STL.html'>阅读全文</a><img src ="http://www.cppblog.com/shixiyu/aggbug/186838.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/shixiyu/" target="_blank">爱走小路</a> 2012-08-10 17:45 <a href="http://www.cppblog.com/shixiyu/archive/2012/08/10/STL.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>