﻿<?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++博客-woka</title><link>http://www.cppblog.com/woka/</link><description /><language>zh-cn</language><lastBuildDate>Tue, 14 Apr 2026 23:09:56 GMT</lastBuildDate><pubDate>Tue, 14 Apr 2026 23:09:56 GMT</pubDate><ttl>60</ttl><item><title>MFC视口窗口</title><link>http://www.cppblog.com/woka/archive/2009/11/21/101576.html</link><dc:creator>woka</dc:creator><author>woka</author><pubDate>Sat, 21 Nov 2009 12:02:00 GMT</pubDate><guid>http://www.cppblog.com/woka/archive/2009/11/21/101576.html</guid><wfw:comment>http://www.cppblog.com/woka/comments/101576.html</wfw:comment><comments>http://www.cppblog.com/woka/archive/2009/11/21/101576.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/woka/comments/commentRss/101576.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/woka/services/trackbacks/101576.html</trackback:ping><description><![CDATA[<span  style="color: rgb(102, 102, 102); font-family: Tahoma; font-size: 12px; line-height: 20px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; ">1.SetWindowOrg 是把设备坐标的原点移动到逻辑坐标的(X, Y)处&nbsp;<br style="line-height: normal; "><br style="line-height: normal; ">2.SetViewportOrg 是把逻辑坐标的原点移动到设备坐标的(X, Y)处&nbsp;</span>
<img src ="http://www.cppblog.com/woka/aggbug/101576.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/woka/" target="_blank">woka</a> 2009-11-21 20:02 <a href="http://www.cppblog.com/woka/archive/2009/11/21/101576.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>zz:mfc中使用标准库stl出现xdebug、xlocale错误的解决方法</title><link>http://www.cppblog.com/woka/archive/2009/11/21/101554.html</link><dc:creator>woka</dc:creator><author>woka</author><pubDate>Sat, 21 Nov 2009 08:14:00 GMT</pubDate><guid>http://www.cppblog.com/woka/archive/2009/11/21/101554.html</guid><wfw:comment>http://www.cppblog.com/woka/comments/101554.html</wfw:comment><comments>http://www.cppblog.com/woka/archive/2009/11/21/101554.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/woka/comments/commentRss/101554.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/woka/services/trackbacks/101554.html</trackback:ping><description><![CDATA[<span  style="font-family: Arial; font-size: 12px; line-height: 18px; "><div class="tit" style="font-family: 宋体; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 16px; line-height: 18px; font-weight: bold; overflow-x: hidden; overflow-y: hidden; ">mfc中使用标准库stl出现xdebug、xlocale错误的解决方法</div><table style="table-layout: fixed; width: 901px; "><tbody><tr><td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: 18px; "><div id="blog_text" class="cnt" style="font-family: 宋体; word-wrap: break-word; word-break: normal; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 13px; line-height: 20px; color: rgb(85, 85, 85); overflow-x: hidden; overflow-y: hidden; position: static; "><p style="line-height: normal; ">参考：</p><p style="line-height: normal; "><a target="_blank" href="http://topic.csdn.net/u/20081023/14/0dded841-0e50-4fa1-b84a-1822f412b199.html" style="font-weight: bold; text-decoration: none; line-height: 20px; font-size: 14px; color: rgb(39, 153, 211); font-family: 宋体; ">在2005mfc的单文档框架中使用#include&lt;fstream&gt;编译出错</a></p><p style="line-height: normal; "><a target="_blank" href="http://social.msdn.microsoft.com/Forums/zh-TW/234/thread/e27da79b-ac38-4db8-a1d8-5536070590e9" style="font-weight: bold; text-decoration: none; line-height: 20px; font-size: 13px; color: rgb(39, 153, 211); font-family: 宋体; ">初學者想請問 xlocale 以及 xdebug</a></p><p style="line-height: normal; "><a target="_blank" href="http://www.dotnetmonster.com/Uwe/Forum.aspx/vs-net-general/5935/Debug-errors-in-VC-NET-xdebug" style="font-weight: bold; text-decoration: none; line-height: 20px; font-size: 14px; color: rgb(39, 153, 211); font-family: 宋体; ">Debug errors in VC++ .NET (xdebug)</a></p><p style="line-height: normal; "></p><p style="line-height: normal; ">最近因为编程需要，需要在控制台程序中使用mfc的CString，在建立console工程的时候，选择了mfc选项，</p><p style="line-height: normal; ">结果在include标准库stl的头文件的时候，出现了一大堆的xdebug、xlocale错误，该问题我是第一次碰见，</p><p style="line-height: normal; ">在一番google搜索之后，找到了解决方法，特此做一个收藏</p><div style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; background-color: rgb(249, 247, 237); "><div class="source" jquery1243781476921="2" style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; background-color: rgb(249, 247, 237); color: rgb(0, 0, 0); "><p style="line-height: normal; "><span style="line-height: normal; color: rgb(0, 128, 0); ">// 来源：http://social.msdn.microsoft.com/Forums/zh-TW/234/thread/e27da79b-ac38-4db8-a1d8-5536070590e9</span><br style="line-height: normal; "><br style="line-height: normal; "><span style="line-height: normal; color: rgb(0, 128, 0); ">// 正常的 头部：</span><br style="line-height: normal; "><br style="line-height: normal; "><span style="line-height: normal; color: rgb(0, 0, 255); ">#include "stdafx.h"</span><br style="line-height: normal; "><span style="line-height: normal; color: rgb(0, 0, 255); ">#include "OLE DOC Viewer.h"</span><br style="line-height: normal; "><span style="line-height: normal; color: rgb(0, 0, 255); ">#include "OLE DOC ViewerDlg.h"</span><br style="line-height: normal; "><span style="line-height: normal; color: rgb(0, 0, 255); ">#include ".\ole doc viewerdlg.h"</span><br style="line-height: normal; "><span style="line-height: normal; color: rgb(0, 0, 255); ">#include "selectiondlg.h"</span><br style="line-height: normal; "><br style="line-height: normal; "><span style="line-height: normal; color: rgb(0, 0, 255); ">#include "QQCrypt.h"</span><br style="line-height: normal; "><br style="line-height: normal; "><span style="line-height: normal; color: rgb(0, 0, 255); ">#ifdef _DEBUG</span><br style="line-height: normal; "><span style="line-height: normal; color: rgb(0, 0, 255); ">#define new DEBUG_NEW</span><br style="line-height: normal; "><span style="line-height: normal; color: rgb(0, 0, 255); ">#endif</span></p><p style="line-height: normal; ">// #include &lt;iostream&gt;</p><p style="line-height: normal; "><span style="line-height: normal; color: rgb(0, 128, 0); ">// 但是如果把使用了 STL的文件的#include 放在后面就出问题！！！</span></p><p style="line-height: normal; ">=============================================================</p><p style="line-height: normal; ">=============================================================</p><p style="line-height: normal; "><font size="5" style="line-height: normal; ">//&nbsp;<font face="宋体" style="line-height: normal; ">解决方法</font></font><br style="line-height: normal; "><span style="line-height: normal; color: rgb(0, 0, 255); ">#include "stdafx.h"</span><br style="line-height: normal; "><span style="line-height: normal; color: rgb(0, 0, 255); ">#include "OLE DOC Viewer.h"</span><br style="line-height: normal; "><span style="line-height: normal; color: rgb(0, 0, 255); ">#include "OLE DOC ViewerDlg.h"</span><br style="line-height: normal; "><span style="line-height: normal; color: rgb(0, 0, 255); ">#include ".\ole doc viewerdlg.h"</span><br style="line-height: normal; "><span style="line-height: normal; color: rgb(0, 0, 255); ">#include "selectiondlg.h"</span></p><p style="line-height: normal; "><font color="#ff0000" style="line-height: normal; ">// 在这里包含标准库的相关头文件</font></p><p style="line-height: normal; "><font color="#ff0000" style="line-height: normal; ">#include &lt;iostream&gt;</font></p><p style="line-height: normal; "><font color="#ff0000" style="line-height: normal; ">using namespace std;</font></p><p style="line-height: normal; "><br style="line-height: normal; "><font color="#ff0000" style="line-height: normal; "><span style="line-height: normal; color: rgb(0, 0, 255); ">#ifdef _DEBUG</span><br style="line-height: normal; "><span style="line-height: normal; color: rgb(0, 0, 255); ">#define new DEBUG_NEW</span><br style="line-height: normal; "><span style="line-height: normal; color: rgb(0, 0, 255); ">#endif</span><br style="line-height: normal; "></font><br style="line-height: normal; "><br style="line-height: normal; "><span style="line-height: normal; color: rgb(0, 0, 255); ">#include "QQCrypt.h"</span><br style="line-height: normal; "><br style="line-height: normal; "><span style="line-height: normal; color: rgb(0, 128, 0); ">// 原因是DEBUG中，new被重载了！</span></p></div></div><p style="line-height: normal; "></p><p style="line-height: normal; ">正如上面得代码所说，在debug版下，new被重载（标准库stl内部对new进行了重载，两者互相冲突）</p><p style="line-height: normal; ">必须保证标准库include语句出现在</p><p style="line-height: normal; "><span style="line-height: normal; color: rgb(0, 0, 255); ">#ifdef _DEBUG</span><br style="line-height: normal; "><span style="line-height: normal; color: rgb(0, 0, 255); ">#define new DEBUG_NEW</span><br style="line-height: normal; "><span style="line-height: normal; color: rgb(0, 0, 255); ">#endif</span></p><p style="line-height: normal; ">之前；</p><p style="line-height: normal; ">最简单的方法就是将所有的需要的stl的头文件放在stdafx.h文件中（预编译头文件），这样肯定能够保证stl</p><p style="line-height: normal; ">的头文件出现在上面的debug代码的前面</p><p style="line-height: normal; ">ps：当然我并不推荐这种方法，将所有的头文件都丢到stdafx.h中，这是一种不负责任的编码习惯，</p><p style="line-height: normal; ">&#8220;应该合理的安排头文件之间的依赖关系&#8221;--参见effective c++</p></div></td></tr></tbody></table></span>
<img src ="http://www.cppblog.com/woka/aggbug/101554.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/woka/" target="_blank">woka</a> 2009-11-21 16:14 <a href="http://www.cppblog.com/woka/archive/2009/11/21/101554.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>什么是虚函数</title><link>http://www.cppblog.com/woka/archive/2009/10/22/99238.html</link><dc:creator>woka</dc:creator><author>woka</author><pubDate>Thu, 22 Oct 2009 14:25:00 GMT</pubDate><guid>http://www.cppblog.com/woka/archive/2009/10/22/99238.html</guid><wfw:comment>http://www.cppblog.com/woka/comments/99238.html</wfw:comment><comments>http://www.cppblog.com/woka/archive/2009/10/22/99238.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/woka/comments/commentRss/99238.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/woka/services/trackbacks/99238.html</trackback:ping><description><![CDATA[<span  style="font-family: Arial; font-size: 14px; line-height: 24px; ">多态性是<span style="BACKGROUND-COLOR:yellow">将接口与实现进行分离</span>；用形象的语言来解释就是实现以共同的方法，但因个体差异而采用不同的策略。</span>

<div><font  face="Arial" size="4"><span  style="font-size: 14px; line-height: 24px;">这其实也是软件工程设计的一个十分基本的原理：策略与机制分离，机制决定如何来做，策略决定做什么；多态性中的函数决定了做什么，而不同的类对这一相同的决策又有着不同的行为，这样使的我们可以以更简单的接口实现更加全面复杂灵活的功能。</span></font></div><img src ="http://www.cppblog.com/woka/aggbug/99238.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/woka/" target="_blank">woka</a> 2009-10-22 22:25 <a href="http://www.cppblog.com/woka/archive/2009/10/22/99238.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>zz:extern "C"</title><link>http://www.cppblog.com/woka/archive/2009/10/22/99208.html</link><dc:creator>woka</dc:creator><author>woka</author><pubDate>Thu, 22 Oct 2009 11:19:00 GMT</pubDate><guid>http://www.cppblog.com/woka/archive/2009/10/22/99208.html</guid><wfw:comment>http://www.cppblog.com/woka/comments/99208.html</wfw:comment><comments>http://www.cppblog.com/woka/archive/2009/10/22/99208.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/woka/comments/commentRss/99208.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/woka/services/trackbacks/99208.html</trackback:ping><description><![CDATA[<span  style="font-family: 宋体, serif; font-size: 14px; line-height: 22px; ">时常在cpp的代码之中看到这样的代码:&nbsp;<br><br><code style="width: 519px; background-color: rgb(255, 255, 255); border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: rgb(204, 204, 204); border-right-color: rgb(204, 204, 204); border-bottom-color: rgb(204, 204, 204); border-left-color: rgb(204, 204, 204); padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; display: block; font-size: 11px; font-family: Arial, Helvetica, sans-serif; ">#ifdef __cplusplus&nbsp;<br>extern "C" {&nbsp;<br>#endif&nbsp;<br><br>//一段代码&nbsp;<br><br>#ifdef __cplusplus&nbsp;<br>}&nbsp;<br>#endif</code><br>　　这样的代码到底是什么意思呢？首先，__cplusplus是cpp中的自定义宏，那么定义了这个宏的话表示这是一段cpp的代码，也就是说，上面的代码的含义是:如果这是一段cpp的代码，那么加入extern "C"{和}处理其中的代码。&nbsp;<br><br>　　要明白为何使用extern "C"，还得从cpp中对函数的重载处理开始说起。在c++中，为了支持重载机制，在编译生成的汇编码中，要对函数的名字进行一些处理，加入比如函数的返回类型等等.而在C中，只是简单的函数名字而已，不会加入其他的信息.也就是说:C++和C对产生的函数名字的处理是不一样的.&nbsp;<br><br>　　比如下面的一段简单的函数，我们看看加入和不加入extern "C"产生的汇编代码都有哪些变化:&nbsp;<br><br><code style="width: 519px; background-color: rgb(255, 255, 255); border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: rgb(204, 204, 204); border-right-color: rgb(204, 204, 204); border-bottom-color: rgb(204, 204, 204); border-left-color: rgb(204, 204, 204); padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; display: block; font-size: 11px; font-family: Arial, Helvetica, sans-serif; ">int f(void)&nbsp;<br>{&nbsp;<br>return 1;&nbsp;<br>}</code><br>　　在加入extern "C"的时候产生的汇编代码是:&nbsp;<br><br><code style="width: 519px; background-color: rgb(255, 255, 255); border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: rgb(204, 204, 204); border-right-color: rgb(204, 204, 204); border-bottom-color: rgb(204, 204, 204); border-left-color: rgb(204, 204, 204); padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; display: block; font-size: 11px; font-family: Arial, Helvetica, sans-serif; ">.file "test.cxx"&nbsp;<br>.text&nbsp;<br>.align 2&nbsp;<br>.globl _f&nbsp;<br>.def _f; .scl 2; .type 32; .endef&nbsp;<br>_f:&nbsp;<br>pushl %ebp&nbsp;<br>movl %esp， %ebp&nbsp;<br>movl $1， %eax&nbsp;<br>popl %ebp&nbsp;<br>ret</code><br>　　但是不加入了extern "C"之后&nbsp;<br><br><code style="width: 519px; background-color: rgb(255, 255, 255); border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: rgb(204, 204, 204); border-right-color: rgb(204, 204, 204); border-bottom-color: rgb(204, 204, 204); border-left-color: rgb(204, 204, 204); padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; display: block; font-size: 11px; font-family: Arial, Helvetica, sans-serif; ">.file "test.cxx"&nbsp;<br>.text&nbsp;<br>.align 2&nbsp;<br>.globl __Z1fv&nbsp;<br>.def __Z1fv; .scl 2; .type 32; .endef&nbsp;<br>__Z1fv:&nbsp;<br>pushl %ebp&nbsp;<br>movl %esp， %ebp&nbsp;<br>movl $1， %eax&nbsp;<br>popl %ebp&nbsp;<br>ret</code><br>　　两段汇编代码同样都是使用gcc -S命令产生的，所有的地方都是一样的，唯独是产生的函数名，一个是_f，一个是__Z1fv。&nbsp;<br><br>　　明白了加入与不加入extern "C"之后对函数名称产生的影响，我们继续我们的讨论:为什么需要使用extern "C"呢？C++之父在设计C++之时，考虑到当时已经存在了大量的C代码，为了支持原来的C代码和已经写好C库，需要在C++中尽可能的支持C，而extern "C"就是其中的一个策略。&nbsp;<br><br>　　试想这样的情况:一个库文件已经用C写好了而且运行得很良好，这个时候我们需要使用这个库文件，但是我们需要使用C++来写这个新的代码。如果这个代码使用的是C++的方式链接这个C库文件的话，那么就会出现链接错误.我们来看一段代码:首先，我们使用C的处理方式来写一个函数，也就是说假设这个函数当时是用C写成的:&nbsp;<br><br><code style="width: 519px; background-color: rgb(255, 255, 255); border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: rgb(204, 204, 204); border-right-color: rgb(204, 204, 204); border-bottom-color: rgb(204, 204, 204); border-left-color: rgb(204, 204, 204); padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; display: block; font-size: 11px; font-family: Arial, Helvetica, sans-serif; ">//f1.c&nbsp;<br>extern "C"&nbsp;<br>{&nbsp;<br>void f1()&nbsp;<br>{&nbsp;<br>return;&nbsp;<br>}&nbsp;<br>}</code><br>　　编译命令是:gcc -c f1.c -o f1.o 产生了一个叫f1.o的库文件。再写一段代码调用这个f1函数:&nbsp;<br><br><code style="width: 519px; background-color: rgb(255, 255, 255); border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: rgb(204, 204, 204); border-right-color: rgb(204, 204, 204); border-bottom-color: rgb(204, 204, 204); border-left-color: rgb(204, 204, 204); padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; display: block; font-size: 11px; font-family: Arial, Helvetica, sans-serif; ">// test.cxx&nbsp;<br>//这个extern表示f1函数在别的地方定义，这样可以通过&nbsp;<br>//编译，但是链接的时候还是需要&nbsp;<br>//链接上原来的库文件.&nbsp;<br>extern void f1();&nbsp;<br><br>int main()&nbsp;<br>{&nbsp;<br>f1();&nbsp;<br><br>return 0;&nbsp;<br>}</code><br>　　通过gcc -c test.cxx -o test.o 产生一个叫test.o的文件。然后，我们使用gcc test.o f1.o来链接两个文件，可是出错了，错误的提示是:&nbsp;<br><br><code style="width: 519px; background-color: rgb(255, 255, 255); border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: rgb(204, 204, 204); border-right-color: rgb(204, 204, 204); border-bottom-color: rgb(204, 204, 204); border-left-color: rgb(204, 204, 204); padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; display: block; font-size: 11px; font-family: Arial, Helvetica, sans-serif; ">test.o(.text + 0x1f):test.cxx: undefine reference to 'f1()'</code><br>　　也就是说，在编译test.cxx的时候编译器是使用C++的方式来处理f1()函数的，但是实际上链接的库文件却是用C的方式来处理函数的，所以就会出现链接过不去的错误:因为链接器找不到函数。&nbsp;<br><br>　　因此，为了在C++代码中调用用C写成的库文件，就需要用extern "C"来告诉编译器:这是一个用C写成的库文件，请用C的方式来链接它们。&nbsp;<br><br>　　比如，现在我们有了一个C库文件，它的头文件是f.h，产生的lib文件是f.lib，那么我们如果要在C++中使用这个库文件，我们需要这样写:&nbsp;<br><br><code style="width: 519px; background-color: rgb(255, 255, 255); border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: rgb(204, 204, 204); border-right-color: rgb(204, 204, 204); border-bottom-color: rgb(204, 204, 204); border-left-color: rgb(204, 204, 204); padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; display: block; font-size: 11px; font-family: Arial, Helvetica, sans-serif; ">extern "C"&nbsp;<br>{&nbsp;<br>#include "f.h"&nbsp;<br>}</code><br>　　回到上面的问题，如果要改正链接错误，我们需要这样子改写test.cxx:&nbsp;<br><br><code style="width: 519px; background-color: rgb(255, 255, 255); border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: rgb(204, 204, 204); border-right-color: rgb(204, 204, 204); border-bottom-color: rgb(204, 204, 204); border-left-color: rgb(204, 204, 204); padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; display: block; font-size: 11px; font-family: Arial, Helvetica, sans-serif; ">extern "C"&nbsp;<br>{&nbsp;<br>extern void f1();&nbsp;<br>}&nbsp;<br><br>int main()&nbsp;<br>{&nbsp;<br>f1();&nbsp;<br><br>return 0;&nbsp;<br>}</code><br>　　重新编译并且链接就可以过去了.&nbsp;</span>
<img src ="http://www.cppblog.com/woka/aggbug/99208.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/woka/" target="_blank">woka</a> 2009-10-22 19:19 <a href="http://www.cppblog.com/woka/archive/2009/10/22/99208.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>转载：解析#pragma指令</title><link>http://www.cppblog.com/woka/archive/2009/10/18/98904.html</link><dc:creator>woka</dc:creator><author>woka</author><pubDate>Sun, 18 Oct 2009 14:35:00 GMT</pubDate><guid>http://www.cppblog.com/woka/archive/2009/10/18/98904.html</guid><wfw:comment>http://www.cppblog.com/woka/comments/98904.html</wfw:comment><comments>http://www.cppblog.com/woka/archive/2009/10/18/98904.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/woka/comments/commentRss/98904.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/woka/services/trackbacks/98904.html</trackback:ping><description><![CDATA[<span  style="font-family: Tahoma, Arial, ����; font-size: 12px; line-height: 19px; ">在所有的预处理指令中，#Pragma 指令可能是最复杂的了，它的作用是设定编译器的状态或者是指示编译器完成一些特定的动作。#pragma指令对每个编译器给出了一个方法,在保持与C和C++语言完全兼容的情况下,给出主机或操作系统专有的特征。依据定义,编译指示是机器或操作系统专有的,且对于每个编译器都是不同的。<br>其格式一般为: #Pragma Para<br>其中Para 为参数，下面来看一些常用的参数。<br><br>(1)message 参数。 Message 参数是我最喜欢的一个参数，它能够在编译信息输出窗<br>口中输出相应的信息，这对于源代码信息的控制是非常重要的。其使用方法为：<br>#Pragma message(&#8220;消息文本&#8221;)<br>当编译器遇到这条指令时就在编译输出窗口中将消息文本打印出来。<br>当我们在程序中定义了许多宏来控制源代码版本的时候，我们自己有可能都会忘记有没有正确的设置这些宏，此时我们可以用这条指令在编译的时候就进行检查。假设我们希望判断自己有没有在源代码的什么地方定义了_X86这个宏可以用下面的方法<br>#ifdef _X86<br>#Pragma message(&#8220;_X86 macro activated!&#8221;)<br>#endif<br>当我们定义了_X86这个宏以后，应用程序在编译时就会在编译输出窗口里显示&#8220;_<br>X86 macro activated!&#8221;。我们就不会因为不记得自己定义的一些特定的宏而抓耳挠腮了<br>。<br><br>(2)另一个使用得比较多的pragma参数是code_seg。格式如：<br>#pragma code_seg( ["section-name"[,"section-class"] ] )<br>它能够设置程序中函数代码存放的代码段，当我们开发驱动程序的时候就会使用到它。<br><br>(3)#pragma once (比较常用）<br>只要在头文件的最开始加入这条指令就能够保证头文件被编译一次，这条指令实际上在VC6中就已经有了，但是考虑到兼容性并没有太多的使用它。<br><br>(4)#pragma hdrstop表示预编译头文件到此为止，后面的头文件不进行预编译。BCB可以预编译头文件以加快链接的速度，但如果所有头文件都进行预编译又可能占太多磁盘空间，所以使用这个选项排除一些头文件。&nbsp;<br>有时单元之间有依赖关系，比如单元A依赖单元B，所以单元B要先于单元A编译。你可以用#pragma startup指定编译优先级，如果使用了#pragma package(smart_init) ，BCB就会根据优先级的大小先后编译。&nbsp;<br><br>(5)#pragma resource "*.dfm"表示把*.dfm文件中的资源加入工程。*.dfm中包括窗体<br>外观的定义。&nbsp;<br><br>(6)#pragma warning( disable : 4507 34; once : 4385; error : 164 )<br>等价于：<br>#pragma warning(disable:4507 34) // 不显示4507和34号警告信息<br>#pragma warning(once:4385) // 4385号警告信息仅报告一次<br>#pragma warning(error:164) // 把164号警告信息作为一个错误。<br>同时这个pragma warning 也支持如下格式：<br>#pragma warning( push [ ,n ] )<br>#pragma warning( pop )<br>这里n代表一个警告等级(1---4)。<br>#pragma warning( push )保存所有警告信息的现有的警告状态。<br>#pragma warning( push, n)保存所有警告信息的现有的警告状态，并且把全局警告<br>等级设定为n。&nbsp;<br>#pragma warning( pop )向栈中弹出最后一个警告信息，在入栈和出栈之间所作的<br>一切改动取消。例如：<br>#pragma warning( push )<br>#pragma warning( disable : 4705 )<br>#pragma warning( disable : 4706 )<br>#pragma warning( disable : 4707 )<br>//.......<br>#pragma warning( pop )&nbsp;<br>在这段代码的最后，重新保存所有的警告信息(包括4705，4706和4707)。<br>（7）pragma comment(...)<br>该指令将一个注释记录放入一个对象文件或可执行文件中。<br>常用的lib关键字，可以帮我们连入一个库文件。</span>
<img src ="http://www.cppblog.com/woka/aggbug/98904.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/woka/" target="_blank">woka</a> 2009-10-18 22:35 <a href="http://www.cppblog.com/woka/archive/2009/10/18/98904.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>转载：const指针</title><link>http://www.cppblog.com/woka/archive/2009/10/14/98617.html</link><dc:creator>woka</dc:creator><author>woka</author><pubDate>Wed, 14 Oct 2009 11:22:00 GMT</pubDate><guid>http://www.cppblog.com/woka/archive/2009/10/14/98617.html</guid><wfw:comment>http://www.cppblog.com/woka/comments/98617.html</wfw:comment><comments>http://www.cppblog.com/woka/archive/2009/10/14/98617.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/woka/comments/commentRss/98617.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/woka/services/trackbacks/98617.html</trackback:ping><description><![CDATA[<span  style="font-family: Arial; font-size: 12px; line-height: 18px; "><div class="tit" style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 14px; line-height: 18px; font-weight: bold; overflow-x: hidden; overflow-y: hidden; "><font  color="#666666" size="3"><span  style="font-size: 12px; font-weight: normal;"><font  color="#000000" size="4"><span  style="font-size: 14px;"><strong><br></strong></span></font></span></font></div><table style="table-layout: fixed; width: 960px; "><tbody><tr><td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: 18px; "><div id="blog_text" class="cnt" style="font-family: Arial; word-wrap: break-word; word-break: normal; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 14px; line-height: 20px; color: rgb(51, 51, 51); overflow-x: hidden; overflow-y: hidden; position: static; "><p style="line-height: normal; margin-top: 0cm; margin-right: 0cm; margin-bottom: 0pt; margin-left: 0cm; "><span style="line-height: normal; font-size: 9pt; color: black; ">const</span><span style="line-height: normal; font-size: 9pt; color: black; ">修饰指针和引用的用法<span style="line-height: normal; ">,</span>对于初学<span style="line-height: normal; ">C++</span>的人直是讳莫如深<span style="line-height: normal; ">,</span>不知所云<span style="line-height: normal; ">.</span>一旦你了解了其用法<span style="line-height: normal; ">,</span>一切便不值一哂了<span style="line-height: normal; ">.</span>下面我为读者一一释疑<span style="line-height: normal; ">:&nbsp;<br style="line-height: normal; ">&nbsp;&nbsp;&nbsp;</span>大致说来其可分为三种情况<span style="line-height: normal; ">:&nbsp;const</span>修饰指针<span style="line-height: normal; ">,const</span>修饰引用<span style="line-height: normal; ">,const</span>修饰指针的引用<span style="line-height: normal; ">.&nbsp;<br style="line-height: normal; ">1.const</span>修饰指针<span style="line-height: normal; ">&nbsp;<br style="line-height: normal; ">&nbsp;&nbsp;&nbsp;const</span>修饰指针又可分为三种情况<span style="line-height: normal; ">:&nbsp;<br style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;const</span>修饰指针本身<span style="line-height: normal; ">&nbsp;<br style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;const</span>修饰指针所指的变量<span style="line-height: normal; ">(</span>或对象<span style="line-height: normal; ">)&nbsp;<br style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;const</span>修饰指针本身和指针所指的变量<span style="line-height: normal; ">(</span>或对象<span style="line-height: normal; ">)&nbsp;<br style="line-height: normal; ">(1).const</span>修饰指针本身<span style="line-height: normal; ">&nbsp;<br style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;</span>这种情形下<span style="line-height: normal; ">,</span>指针本身为常量<span style="line-height: normal; ">,</span>不可改变<span style="line-height: normal; ">,</span>任何修改指针本身的行为都是非法的<span style="line-height: normal; ">.</span>例如<span style="line-height: normal; ">:&nbsp;<br style="line-height: normal; ">const&nbsp;int&nbsp;a&nbsp;=&nbsp;1;&nbsp;<br style="line-height: normal; ">const&nbsp;int&nbsp;b&nbsp;=&nbsp;2;&nbsp;<br style="line-height: normal; ">int&nbsp;i&nbsp;=&nbsp;3;&nbsp;<br style="line-height: normal; ">int&nbsp;j&nbsp;=&nbsp;4;&nbsp;<br style="line-height: normal; ">int*&nbsp;const&nbsp;pi&nbsp;=&nbsp;&amp;i;&nbsp;//ok,&nbsp;pi</span>的类型为<span style="line-height: normal; ">int*&nbsp;const&nbsp;,&nbsp;&amp;i</span>的类型为<span style="line-height: normal; ">int*&nbsp;const&nbsp;<br style="line-height: normal; ">int*&nbsp;const&nbsp;pi&nbsp;=&nbsp;&amp;a;&nbsp;//error,&nbsp;pi</span>的类型为<span style="line-height: normal; ">int*&nbsp;const,&nbsp;&amp;a</span>的类型为<span style="line-height: normal; ">const&nbsp;int*&nbsp;const&nbsp;<br style="line-height: normal; ">pi&nbsp;=&nbsp;&amp;j;&nbsp;//error,&nbsp;</span>指针是常量<span style="line-height: normal; ">,</span>不可变<span style="line-height: normal; ">&nbsp;<br style="line-height: normal; ">*pi&nbsp;=&nbsp;a;&nbsp;//ok,&nbsp;*pi</span>并没有限定是常量<span style="line-height: normal; ">&nbsp;,</span>可变<span style="line-height: normal; ">&nbsp;<br style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;</span>由此看出，<span style="line-height: normal; ">pi</span>是常量，常量在初始化和赋值时，类型必须严格一致。也就是<span style="line-height: normal; ">&nbsp;<br style="line-height: normal; ">const</span>修饰指针本身时，<span style="line-height: normal; ">=</span>号两边的变量类型必须严格一致，否则不能匹配。<span style="line-height: normal; ">&nbsp;<br style="line-height: normal; ">(2).const</span>修饰指针指向的变量<span style="line-height: normal; ">(</span>或对象<span style="line-height: normal; ">)&nbsp;<br style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;</span>此种情形下<span style="line-height: normal; ">,</span>通过间接引用指针不可改变变量的值<span style="line-height: normal; ">,</span>假设指针为<span style="line-height: normal; ">p,</span>则<span style="line-height: normal; ">*p</span>不可变<span style="line-height: normal; ">,</span>下面以例子说明<span style="line-height: normal; ">:&nbsp;<br style="line-height: normal; ">const&nbsp;int&nbsp;*pi&nbsp;=&nbsp;&amp;a;&nbsp;<br style="line-height: normal; ">//or&nbsp;int&nbsp;const&nbsp;*pi&nbsp;=&nbsp;&amp;a;&nbsp;<br style="line-height: normal; ">//</span>两者毫无二致<span style="line-height: normal; ">,</span>不过<span style="line-height: normal; ">BS</span>喜欢前者<span style="line-height: normal; ">,</span>这也没什么技术上的优劣之分<span style="line-height: normal; ">,</span>也就是说<span style="line-height: normal; ">const&nbsp;int</span>与<span style="line-height: normal; ">int&nbsp;const</span>可以互换<span style="line-height: normal; ">.</span>建议大家熟<span style="line-height: normal; ">&nbsp;<br style="line-height: normal; ">//</span>悉这两种形式<span style="line-height: normal; ">,</span>为简洁便<span style="line-height: normal; ">,</span>以后统统用前者<span style="line-height: normal; ">.&nbsp;<br style="line-height: normal; ">//ok,&nbsp;const</span>并不修饰指针本身<span style="line-height: normal; ">,pi</span>对赋值类型<span style="line-height: normal; ">&nbsp;<br style="line-height: normal; ">//</span>没要求<span style="line-height: normal; ">&nbsp;,</span>但<span style="line-height: normal; ">pi</span>是<span style="line-height: normal; ">int*</span>型指针，所以所赋的必须是个地址值。<span style="line-height: normal; ">&nbsp;<br style="line-height: normal; ">const&nbsp;int&nbsp;*pi&nbsp;=&nbsp;&amp;i;&nbsp;//ok&nbsp;,pi</span>可赋值常量的地址<span style="line-height: normal; ">,</span>又可赋变量的地址<span style="line-height: normal; ">&nbsp;<br style="line-height: normal; ">const&nbsp;int&nbsp;*pi1&nbsp;=&nbsp;&amp;a;&nbsp;<br style="line-height: normal; ">const&nbsp;int&nbsp;*pi&nbsp;=&nbsp;pi1;&nbsp;//ok&nbsp;<br style="line-height: normal; ">*pi&nbsp;=&nbsp;j;&nbsp;//error,*pi&nbsp;</span>不可变<span style="line-height: normal; ">,</span>不能更改指针的间接引用形式<span style="line-height: normal; ">&nbsp;<br style="line-height: normal; ">pi&nbsp;=&nbsp;&amp;j;&nbsp;//ok,pi</span>可变<span style="line-height: normal; ">&nbsp;<br style="line-height: normal; ">pi&nbsp;=&nbsp;&amp;b;&nbsp;//ok,pi</span>可变<span style="line-height: normal; ">&nbsp;<br style="line-height: normal; ">pi++;&nbsp;//ok&nbsp;<br style="line-height: normal; ">--pi;&nbsp;//ok&nbsp;<br style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>由此可见，<span style="line-height: normal; ">pi</span>是变量，可以赋值常量和变量的值<span style="line-height: normal; ">,</span>正如一个整型变量可赋整型数和整型变量一样<span style="line-height: normal; ">.const</span>修饰的不是指针本身，而是其间接引用，<span style="line-height: normal; ">=</span>号两边的类型不必严格匹配，如：<span style="line-height: normal; ">const&nbsp;int*&nbsp;pi&nbsp;=&nbsp;&amp;a;</span>中，<span style="line-height: normal; ">pi</span>的类型为<span style="line-height: normal; ">int*,</span>而<span style="line-height: normal; ">&amp;a</span>的类型为<span style="line-height: normal; ">const&nbsp;int*&nbsp;const,</span>只要其中含有<span style="line-height: normal; ">int*&nbsp;</span>就可以。又如：<span style="line-height: normal; ">const&nbsp;int&nbsp;*pi&nbsp;=&nbsp;&amp;j;</span>中，<span style="line-height: normal; ">pi</span>的类型为<span style="line-height: normal; ">int*,</span>而<span style="line-height: normal; ">&amp;j</span>的类型为<span style="line-height: normal; ">int*&nbsp;const,</span>它向<span style="line-height: normal; ">pi</span>赋值并无大碍。<span style="line-height: normal; ">&nbsp;<br style="line-height: normal; ">(3)const</span>修饰指针本身和指针所指的变量<span style="line-height: normal; ">(</span>或对象<span style="line-height: normal; ">)&nbsp;<br style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>设有指针<span style="line-height: normal; ">p,</span>此种情形下<span style="line-height: normal; ">,p</span>和<span style="line-height: normal; ">*p</span>都不可变<span style="line-height: normal; ">.</span>举例如下<span style="line-height: normal; ">:&nbsp;<br style="line-height: normal; ">const&nbsp;int*&nbsp;const&nbsp;pi&nbsp;=&nbsp;&amp;a;&nbsp;<br style="line-height: normal; ">//or&nbsp;int&nbsp;const*&nbsp;const&nbsp;pi&nbsp;=&nbsp;&amp;a;&nbsp;<br style="line-height: normal; ">//</span>将<span style="line-height: normal; ">const&nbsp;pi</span>看作一体<span style="line-height: normal; ">,</span>就与<span style="line-height: normal; ">(2)</span>所述相同<span style="line-height: normal; ">,</span>只是要求<span style="line-height: normal; ">pi</span>必须为<span style="line-height: normal; ">const,</span>正如上所说，<span style="line-height: normal; ">=</span>号两边的类型不必严格匹配，但必须含有<span style="line-height: normal; ">int*,&nbsp;&amp;a</span>的类型为<span style="line-height: normal; ">const&nbsp;int*&nbsp;const,</span>含有<span style="line-height: normal; ">int*,&nbsp;</span>所以可以赋值。<span style="line-height: normal; ">&nbsp;<br style="line-height: normal; ">const&nbsp;int*&nbsp;const&nbsp;pi&nbsp;=&nbsp;&amp;i;&nbsp;//ok,&nbsp;&amp;i</span>类型为<span style="line-height: normal; ">int*&nbsp;const,</span>含有<span style="line-height: normal; ">int*,&nbsp;</span>可赋值。<span style="line-height: normal; ">&nbsp;<br style="line-height: normal; ">const&nbsp;int&nbsp;*pi1&nbsp;=&nbsp;&amp;j;&nbsp;<br style="line-height: normal; ">const&nbsp;int&nbsp;*const&nbsp;pi&nbsp;=&nbsp;pi1;&nbsp;//ok,&nbsp;&nbsp;pi1</span>类型为<span style="line-height: normal; ">int*&nbsp;<br style="line-height: normal; ">pi&nbsp;=&nbsp;&amp;b;&nbsp;//error,&nbsp;pi</span>不可变<span style="line-height: normal; ">&nbsp;<br style="line-height: normal; ">pi&nbsp;=&nbsp;&amp;j;&nbsp;//error,&nbsp;pi</span>不可变<span style="line-height: normal; ">&nbsp;<br style="line-height: normal; ">*pi&nbsp;=&nbsp;b;&nbsp;//error,&nbsp;*pi</span>不可变<span style="line-height: normal; ">&nbsp;<br style="line-height: normal; ">*pi&nbsp;=&nbsp;j;&nbsp;//error,&nbsp;*pi</span>不可变<span style="line-height: normal; ">&nbsp;<br style="line-height: normal; ">pi++;&nbsp;//error&nbsp;</span>，<span style="line-height: normal; ">pi</span>不可变<span style="line-height: normal; ">&nbsp;<br style="line-height: normal; ">++i;&nbsp;//ok,&nbsp;=</span>号右边的变量<span style="line-height: normal; ">(</span>或对象<span style="line-height: normal; ">)</span>与所修饰的变量无关<span style="line-height: normal; ">&nbsp;<br style="line-height: normal; ">a--;&nbsp;//error,&nbsp;a</span>为<span style="line-height: normal; ">const&nbsp;<br style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>这种情况，跟以上两种情形有联系。对<span style="line-height: normal; ">const&nbsp;int*&nbsp;const&nbsp;pi&nbsp;=&nbsp;&amp;a;</span>我们可以这样看：<span style="line-height: normal; ">const&nbsp;int*(&nbsp;const&nbsp;pi&nbsp;)=&nbsp;&amp;a;(</span>仅仅是表达需要<span style="line-height: normal; ">)</span>，将<span style="line-height: normal; ">const&nbsp;pi</span>看作一体，就与上述分类<span style="line-height: normal; ">(2)</span>符合。只要含有<span style="line-height: normal; ">int*</span>便可<span style="line-height: normal; ">.&nbsp;<br style="line-height: normal; "><br style="line-height: normal; ">2.const</span>修饰引用<span style="line-height: normal; ">&nbsp;<br style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;</span>这种情况比较简单，没有象修饰指针那样繁复，因为引用和引用对象是一体的，所以引用被<span style="line-height: normal; ">const</span>修饰只有一种类型。<span style="line-height: normal; ">&nbsp;<br style="line-height: normal; ">const</span>修饰引用<span style="line-height: normal; ">,</span>引用本身不可变<span style="line-height: normal; ">,</span>但引用的变量<span style="line-height: normal; ">(</span>或对象<span style="line-height: normal; ">)</span>可以改变<span style="line-height: normal; ">.</span>例如<span style="line-height: normal; ">:&nbsp;<br style="line-height: normal; ">const&nbsp;int&amp;&nbsp;ri&nbsp;=&nbsp;a;&nbsp;//or&nbsp;int&nbsp;const&nbsp;&amp;&nbsp;ri&nbsp;=&nbsp;a;&nbsp;ok,&nbsp;ri&nbsp;</span>本身是常量<span style="line-height: normal; ">,</span>引用不区分类型<span style="line-height: normal; ">&nbsp;<br style="line-height: normal; ">const&nbsp;int&amp;&nbsp;ri&nbsp;=&nbsp;i;&nbsp;//ok,</span>引用不区分类型<span style="line-height: normal; ">&nbsp;<br style="line-height: normal; ">ri++;&nbsp;//error,&nbsp;ri</span>为常量<span style="line-height: normal; ">,</span>不可变<span style="line-height: normal; ">&nbsp;<br style="line-height: normal; ">i++;&nbsp;//ok,=</span>右边的变量与引用无关<span style="line-height: normal; ">&nbsp;<br style="line-height: normal; ">ri=b;&nbsp;//error,&nbsp;ri</span>为常量<span style="line-height: normal; ">&nbsp;<br style="line-height: normal; ">i=j;&nbsp;//ok,=</span>右边的变量与引用无关<span style="line-height: normal; ">&nbsp;<br style="line-height: normal; ">int&nbsp;&amp;&nbsp;const&nbsp;ri&nbsp;=&nbsp;i;&nbsp;//error,</span>不存在这种形式<span style="line-height: normal; ">,</span>没有意义<span style="line-height: normal; ">&nbsp;<br style="line-height: normal; "><br style="line-height: normal; ">3.const</span>修饰指针的引用<span style="line-height: normal; ">&nbsp;<br style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;</span>引用只是个别名<span style="line-height: normal; ">,</span>这里与修饰指针类似<span style="line-height: normal; ">,</span>又分为三种情况<span style="line-height: normal; ">:&nbsp;<br style="line-height: normal; ">(1)&nbsp;<br style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>先给个例子：<span style="line-height: normal; ">&nbsp;<br style="line-height: normal; ">const&nbsp;int&nbsp;*pi&nbsp;=&nbsp;&amp;a;&nbsp;<br style="line-height: normal; ">const&nbsp;int&nbsp;*&amp;ri&nbsp;=&nbsp;pi;&nbsp;<br style="line-height: normal; ">//or&nbsp;int&nbsp;const&nbsp;*&amp;ri&nbsp;=&nbsp;pi;&nbsp;<br style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;</span>引用是引用对象的别名，正因为如此，<span style="line-height: normal; ">ri</span>是<span style="line-height: normal; ">pi</span>的别名，所以<span style="line-height: normal; ">ri</span>的类型必须与<span style="line-height: normal; ">pi</span>完全一致才行。这里<span style="line-height: normal; ">pi</span>的类型为<span style="line-height: normal; ">int*,ri</span>的类型也为<span style="line-height: normal; ">int*</span>，赋值可行。若<span style="line-height: normal; ">const&nbsp;int&nbsp;*&amp;ri&nbsp;=&nbsp;&amp;a;</span>正不正确？分析一下就知晓。<span style="line-height: normal; ">ri</span>类型为<span style="line-height: normal; ">int*,&amp;a</span>的类型则为<span style="line-height: normal; ">const&nbsp;int*&nbsp;const</span>不匹配。<span style="line-height: normal; ">&nbsp;<br style="line-height: normal; ">const&nbsp;int&nbsp;*&amp;ri&nbsp;=&nbsp;&amp;i;&nbsp;//error,</span>类型不匹配，一为<span style="line-height: normal; ">int*,</span>一为<span style="line-height: normal; ">int*&nbsp;const&nbsp;<br style="line-height: normal; ">ri&nbsp;=&nbsp;&amp;a;&nbsp;//ok&nbsp;<br style="line-height: normal; ">ri&nbsp;=&nbsp;&amp;i;&nbsp;//ok&nbsp;<br style="line-height: normal; ">const&nbsp;int&nbsp;*pi1=&amp;a;&nbsp;<br style="line-height: normal; ">const&nbsp;int&nbsp;*pi2=&amp;i;&nbsp;<br style="line-height: normal; ">ri&nbsp;=&nbsp;pi1;&nbsp;//ok&nbsp;<br style="line-height: normal; ">ri&nbsp;=&nbsp;pi2;&nbsp;//ok&nbsp;<br style="line-height: normal; ">*ri&nbsp;=&nbsp;i;&nbsp;//error&nbsp;<br style="line-height: normal; ">*ri&nbsp;=&nbsp;a;&nbsp;//error&nbsp;<br style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>注意这与<span style="line-height: normal; ">1-(2)</span>的区别<span style="line-height: normal; ">.&nbsp;<br style="line-height: normal; ">(2)&nbsp;<br style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>用例子说明<span style="line-height: normal; ">:&nbsp;<br style="line-height: normal; ">int&nbsp;*const&nbsp;&amp;ri&nbsp;=&nbsp;&amp;i;&nbsp;<br style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;</span>去掉<span style="line-height: normal; ">ri</span>左边的<span style="line-height: normal; ">&amp;</span>号<span style="line-height: normal; ">,</span>则为<span style="line-height: normal; ">int&nbsp;*const&nbsp;ri,</span>因为<span style="line-height: normal; ">ri</span>是别名<span style="line-height: normal; ">,</span>故<span style="line-height: normal; ">ri</span>的类型应与赋值的数类型一致<span style="line-height: normal; ">,ri</span>类型为<span style="line-height: normal; ">int&nbsp;*const,&amp;i</span>为<span style="line-height: normal; ">int&nbsp;*const,</span>可以这么做<span style="line-height: normal; ">.&nbsp;<br style="line-height: normal; ">int&nbsp;*const&nbsp;&amp;ri&nbsp;=&nbsp;pi;&nbsp;//error,</span>类型不合<span style="line-height: normal; ">,</span>一为<span style="line-height: normal; ">int&nbsp;*const&nbsp;,</span>一为<span style="line-height: normal; ">int&nbsp;*&nbsp;<br style="line-height: normal; ">int&nbsp;*const&nbsp;&amp;ri&nbsp;=&nbsp;&amp;a;&nbsp;//error,</span>类型不合<span style="line-height: normal; ">,</span>一为<span style="line-height: normal; ">int&nbsp;*const,</span>一为<span style="line-height: normal; ">const&nbsp;int*&nbsp;const&nbsp;<br style="line-height: normal; ">(*ri)++;&nbsp;//ok&nbsp;<br style="line-height: normal; ">i++;&nbsp;//ok&nbsp;<br style="line-height: normal; ">ri&nbsp;=&nbsp;&amp;i;&nbsp;//error&nbsp;<br style="line-height: normal; ">&nbsp;&nbsp;&nbsp;</span>这种情况下<span style="line-height: normal; ">,ri</span>为常量<span style="line-height: normal; ">,</span>不可更改<span style="line-height: normal; ">.&nbsp;<br style="line-height: normal; ">(3)&nbsp;<br style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>用例子说明：<span style="line-height: normal; ">&nbsp;<br style="line-height: normal; ">const&nbsp;int*&nbsp;pi&nbsp;=&nbsp;&amp;j;&nbsp;<br style="line-height: normal; ">const&nbsp;int*&nbsp;const&nbsp;&amp;ri&nbsp;=&nbsp;pi;&nbsp;//or&nbsp;int&nbsp;const&nbsp;*&nbsp;const&nbsp;&amp;ri&nbsp;=&nbsp;pi;ok&nbsp;<br style="line-height: normal; ">const&nbsp;int*&nbsp;const&nbsp;&amp;ri&nbsp;=&nbsp;&amp;i;&nbsp;//ok&nbsp;<br style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ri</span>是<span style="line-height: normal; ">pi</span>的别名，<span style="line-height: normal; ">pi</span>的类型应与<span style="line-height: normal; ">ri</span>一致。拿掉<span style="line-height: normal; ">&amp;</span>，得<span style="line-height: normal; ">const&nbsp;int*&nbsp;const&nbsp;ri&nbsp;</span>，把<span style="line-height: normal; ">const&nbsp;&nbsp;ri</span>看作一体<span style="line-height: normal; ">,</span>很容易得出<span style="line-height: normal; ">ri</span>的类型信息，就象前面<span style="line-height: normal; ">2-(3)</span>所讨论的一样，可以得到赋给<span style="line-height: normal; ">ri</span>的只要含有类型<span style="line-height: normal; ">int*&nbsp;</span>即可。<span style="line-height: normal; ">pi</span>的类型为<span style="line-height: normal; ">int*,&amp;i</span>的类型为<span style="line-height: normal; ">int*&nbsp;const&nbsp;,</span>可以这么做<span style="line-height: normal; ">.&nbsp;<br style="line-height: normal; ">const&nbsp;int&nbsp;*&nbsp;const&nbsp;&amp;ri&nbsp;=&nbsp;&amp;a;&nbsp;//ok&nbsp;<br style="line-height: normal; ">ri++;&nbsp;&nbsp;//error&nbsp;<br style="line-height: normal; ">*ri&nbsp;=&nbsp;6;&nbsp;&nbsp;//error&nbsp;<br style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;</span>言尽于此<span style="line-height: normal; ">,</span>希望对初学者有所助益<span style="line-height: normal; ">!</span></span></p></div></td></tr></tbody></table></span>
<img src ="http://www.cppblog.com/woka/aggbug/98617.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/woka/" target="_blank">woka</a> 2009-10-14 19:22 <a href="http://www.cppblog.com/woka/archive/2009/10/14/98617.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>结构体中变量的对齐</title><link>http://www.cppblog.com/woka/archive/2009/10/12/98448.html</link><dc:creator>woka</dc:creator><author>woka</author><pubDate>Mon, 12 Oct 2009 15:48:00 GMT</pubDate><guid>http://www.cppblog.com/woka/archive/2009/10/12/98448.html</guid><wfw:comment>http://www.cppblog.com/woka/comments/98448.html</wfw:comment><comments>http://www.cppblog.com/woka/archive/2009/10/12/98448.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/woka/comments/commentRss/98448.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/woka/services/trackbacks/98448.html</trackback:ping><description><![CDATA[
<span style="font-family: Arial; font-size: 16px; border-collapse: collapse; white-space: pre; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; ">C/C++的实现中为了提高结构体的访问效率，进行了变量的对齐，而Visual C++中的标准有不一样 struct member alignment</span><div><font face="Arial"><span style="border-collapse: collapse; white-space: pre; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px;">因此如果是读一些结构比较固定的文件，如果用结构体定义文件中部分，如果用结构体，很容易出错，类中的变量不会进行对齐，因此用类更加简单（比如读BMP文件），如果用结构体也可以用伪指令</span></font></div><div><font face="Arial"><span style="border-collapse: collapse; white-space: pre; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px;"><span style="border-collapse: separate; font-family: Simsun; white-space: normal; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; "><span style="COLOR:red">#pragma pack (n)</span></span></span></font></div><div><p class="MsoNormal"><span lang="EN-US"><span style="COLOR:red">#pragma pack ()</span></span></p></div><img src ="http://www.cppblog.com/woka/aggbug/98448.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/woka/" target="_blank">woka</a> 2009-10-12 23:48 <a href="http://www.cppblog.com/woka/archive/2009/10/12/98448.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>WM_CLOSE 和 WM_DESTORY</title><link>http://www.cppblog.com/woka/archive/2009/09/09/95731.html</link><dc:creator>woka</dc:creator><author>woka</author><pubDate>Wed, 09 Sep 2009 12:34:00 GMT</pubDate><guid>http://www.cppblog.com/woka/archive/2009/09/09/95731.html</guid><wfw:comment>http://www.cppblog.com/woka/comments/95731.html</wfw:comment><comments>http://www.cppblog.com/woka/archive/2009/09/09/95731.html#Feedback</comments><slash:comments>4</slash:comments><wfw:commentRss>http://www.cppblog.com/woka/comments/commentRss/95731.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/woka/services/trackbacks/95731.html</trackback:ping><description><![CDATA[MFC程序的死亡相对于初生来说要简单的多，主要是以下几步：&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp; 　　1.使用者通过点击File/Close或程序窗口右上角的叉号发出WM_CLOSE消息。&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp; 　　2.程序没有设置WM_CLOSE处理程序，交给默认处理程序。&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp; 　　3.默认处理函数对于WM_CLOSE的处理方式为调用::DestoryWindow，并因而发出WM_DESTORY消息。&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp; 　　4.默认的WM_DESTORY处理方式为调用::PostQuitMessage，发出WM_QUIT。&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp; 　　5.CWinApp::Run收到WM_QUIT后结束内部消息循环，并调用ExinInstance函数，它是CWinApp的一个虚拟函数，可以由用户重载。&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp; 　　6.最后回到AfxWinMain，执行AfxWinTerm，结束程序。&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp; ---------------------------------------------------&nbsp;&nbsp; <br>&nbsp; 有时你看到有的程序当点右上角的叉的时候没有关闭程序而是最小化了，就是因为它重载了OnClose，把默认的发送Destory消息给删掉了<br><span style="COLOR: red">但是下面这个程序有什么错误，为什么关掉窗口后程序不能正常结束（窗口关闭，进程没有结束），希望高手帮忙。<br></span>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top><span style="COLOR: #000000">#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">windows.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top><br><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top>LRESULT&nbsp;CALLBACK&nbsp;WinProc(HWND,&nbsp;UINT,&nbsp;WPARAM,&nbsp;LPARAM);<br><img id=Codehighlighter1_163_1059_Open_Image onclick="this.style.display='none'; Codehighlighter1_163_1059_Open_Text.style.display='none'; Codehighlighter1_163_1059_Closed_Image.style.display='inline'; Codehighlighter1_163_1059_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_163_1059_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_163_1059_Closed_Text.style.display='none'; Codehighlighter1_163_1059_Open_Image.style.display='inline'; Codehighlighter1_163_1059_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif" align=top></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;WINAPI&nbsp;WinMain(HINSTANCE&nbsp;hInstance,&nbsp;HINSTANCE&nbsp;hPrev,&nbsp;LPSTR&nbsp;lpCmdLine,&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;nShowCmd)</span><span id=Codehighlighter1_163_1059_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_163_1059_Open_Text><span style="COLOR: #000000">{<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;HWND&nbsp;hwnd;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;WNDCLASS&nbsp;wndclass;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;MSG&nbsp;msg;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;lpszClassName[]&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">我的窗口</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top><br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;wndclass.lpszClassName&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;lpszClassName;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;wndclass.lpfnWndProc&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;WinProc;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;wndclass.hInstance&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;hInstance;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;wndclass.style&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;wndclass.cbClsExtra&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;wndclass.cbWndExtra&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;wndclass.hbrBackground&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;(HBRUSH)GetStockObject(WHITE_BRUSH);<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;wndclass.hCursor&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;LoadCursor(NULL,&nbsp;IDC_ARROW);<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;wndclass.hIcon&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;LoadIcon(NULL,&nbsp;IDI_HAND);<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;wndclass.lpszMenuName&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;NULL;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top><br><img id=Codehighlighter1_638_675_Open_Image onclick="this.style.display='none'; Codehighlighter1_638_675_Open_Text.style.display='none'; Codehighlighter1_638_675_Closed_Image.style.display='inline'; Codehighlighter1_638_675_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_638_675_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_638_675_Closed_Text.style.display='none'; Codehighlighter1_638_675_Open_Image.style.display='inline'; Codehighlighter1_638_675_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(</span><span style="COLOR: #000000">!</span><span style="COLOR: #000000">RegisterClass(</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">wndclass))</span><span id=Codehighlighter1_638_675_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_638_675_Open_Text><span style="COLOR: #000000">{<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MessageBeep(</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">);<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;FALSE;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top><br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;lpszTitle[]&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">My&nbsp;Window</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;hwnd&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;CreateWindow(<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lpszClassName,<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lpszTitle,<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WS_OVERLAPPEDWINDOW,<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CW_USEDEFAULT,<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CW_USEDEFAULT,<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CW_USEDEFAULT,<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CW_USEDEFAULT,<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NULL,<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NULL,<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hInstance,<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NULL<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;ShowWindow(hwnd,&nbsp;nShowCmd);<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;UpdateWindow(hwnd);<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;<br><img id=Codehighlighter1_983_1037_Open_Image onclick="this.style.display='none'; Codehighlighter1_983_1037_Open_Text.style.display='none'; Codehighlighter1_983_1037_Closed_Image.style.display='inline'; Codehighlighter1_983_1037_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_983_1037_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_983_1037_Closed_Text.style.display='none'; Codehighlighter1_983_1037_Open_Image.style.display='inline'; Codehighlighter1_983_1037_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(GetMessage(</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">msg,&nbsp;hwnd,&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">))</span><span id=Codehighlighter1_983_1037_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_983_1037_Open_Text><span style="COLOR: #000000">{<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TranslateMessage(</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">msg);<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DispatchMessage(</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">msg);<br><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;msg.wParam;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</span></span><span style="COLOR: #000000"><br><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top><br><img id=Codehighlighter1_1141_1435_Open_Image onclick="this.style.display='none'; Codehighlighter1_1141_1435_Open_Text.style.display='none'; Codehighlighter1_1141_1435_Closed_Image.style.display='inline'; Codehighlighter1_1141_1435_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_1141_1435_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_1141_1435_Closed_Text.style.display='none'; Codehighlighter1_1141_1435_Open_Image.style.display='inline'; Codehighlighter1_1141_1435_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif" align=top>LRESULT&nbsp;CALLBACK&nbsp;WinProc(HWND&nbsp;hwnd,&nbsp;UINT&nbsp;message,&nbsp;WPARAM&nbsp;wParam,&nbsp;LPARAM&nbsp;lParam)</span><span id=Codehighlighter1_1141_1435_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_1141_1435_Open_Text><span style="COLOR: #000000">{<br><img id=Codehighlighter1_1159_1422_Open_Image onclick="this.style.display='none'; Codehighlighter1_1159_1422_Open_Text.style.display='none'; Codehighlighter1_1159_1422_Closed_Image.style.display='inline'; Codehighlighter1_1159_1422_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_1159_1422_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_1159_1422_Closed_Text.style.display='none'; Codehighlighter1_1159_1422_Open_Image.style.display='inline'; Codehighlighter1_1159_1422_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">switch</span><span style="COLOR: #000000">(message)</span><span id=Codehighlighter1_1159_1422_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_1159_1422_Open_Text><span style="COLOR: #000000">{<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">case</span><span style="COLOR: #000000">&nbsp;WM_PAINT:<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HDC&nbsp;hdc;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hdc&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;GetDC(hwnd);<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TextOut(hdc,&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">VC++</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,&nbsp;strlen(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">VC++</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">));<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DeleteDC(hdc);<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">break</span><span style="COLOR: #000000">;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">case</span><span style="COLOR: #000000">&nbsp;WM_DESTROY:<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PostQuitMessage(</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">);<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;&nbsp;</span><span style="COLOR: #008000"><br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">default</span><span style="COLOR: #000000">:<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;DefWindowProc(hwnd,&nbsp;message,&nbsp;wParam,&nbsp;lParam);<br><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</span></span></div>
<img src ="http://www.cppblog.com/woka/aggbug/95731.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/woka/" target="_blank">woka</a> 2009-09-09 20:34 <a href="http://www.cppblog.com/woka/archive/2009/09/09/95731.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>VC中的变态错误</title><link>http://www.cppblog.com/woka/archive/2009/06/14/87614.html</link><dc:creator>woka</dc:creator><author>woka</author><pubDate>Sat, 13 Jun 2009 16:45:00 GMT</pubDate><guid>http://www.cppblog.com/woka/archive/2009/06/14/87614.html</guid><wfw:comment>http://www.cppblog.com/woka/comments/87614.html</wfw:comment><comments>http://www.cppblog.com/woka/archive/2009/06/14/87614.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cppblog.com/woka/comments/commentRss/87614.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/woka/services/trackbacks/87614.html</trackback:ping><description><![CDATA[<p><font style="BACKGROUND-COLOR: #c7edcc">在VC中（无论VC6.0还是VS2005），如果建立的是一个C文件（后缀为.c而不是.cpp）,如以下一段程序：</font></p>
<p><br><span style="COLOR: #0000ff">#include &lt;iostream&gt;<br>using namespace std;<br>void pp(int a){<br>&nbsp;cout &lt;&lt; a &lt;&lt; endl;<br>}</span></p>
<p style="COLOR: #0000ff">void p(int a){<br>&nbsp;pp(a);<br>&nbsp;int b = 0;<span style="COLOR: #ff0000">//应该放在最前面<br></span>&nbsp;pp(b);<br>}</p>
<p style="COLOR: #0000ff">int main(){<br>&nbsp;int a = 0;<br>&nbsp;p(a);<br>&nbsp;return 0;<br>}</p>
<br>保存为 <span style="COLOR: #ff0000">test.c<br></span><span style="COLOR: #000000">error C2143: 语法错误 : 缺少&#8220;{&#8221;(在&#8220;:&#8221;的前面)<br>error C2059: 语法错误 : &#8220;:&#8221;<br>总共一百多个错误<br>原来在VC中编译c程序，在一个大括号括起的范围内，如果变量声明放在了函数调用的后面，那么编译就会出错。而在其他一些标准C/C++编译器中是不会出现这个错误的。</span>
<img src ="http://www.cppblog.com/woka/aggbug/87614.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/woka/" target="_blank">woka</a> 2009-06-14 00:45 <a href="http://www.cppblog.com/woka/archive/2009/06/14/87614.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C++不定参数</title><link>http://www.cppblog.com/woka/archive/2009/05/24/85556.html</link><dc:creator>woka</dc:creator><author>woka</author><pubDate>Sat, 23 May 2009 17:28:00 GMT</pubDate><guid>http://www.cppblog.com/woka/archive/2009/05/24/85556.html</guid><wfw:comment>http://www.cppblog.com/woka/comments/85556.html</wfw:comment><comments>http://www.cppblog.com/woka/archive/2009/05/24/85556.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/woka/comments/commentRss/85556.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/woka/services/trackbacks/85556.html</trackback:ping><description><![CDATA[今天了解到了C++还有不定参数一说：<br><br><br>C/C++语言有一个不同于其它语言的特性，即其支持可变参数，典型的函数如printf、scanf等可以接受数量不定的参数。如： <br><br>printf ( "I love you" ); <br>printf ( "%d", a ); <br>printf ( "%d,%d", a, b ); <br><br>第一、二、三个printf分别接受1、2、3个参数，让我们看看printf函数的原型： <br><br>int printf ( const char *format, ... ); <br><br>从函数原型可以看出，其除了接收一个固定的参数format以外，后面的参数用"&#8230;"表示。在C/C++语言中，"&#8230;"表示可以接受不定数量的参数，理论上来讲，可以是0或0以上的n个参数。 <br><br>本文将对C/C++可变参数表的使用方法及C/C++支持可变参数表的深层机理进行探索。 <br><br>可变参数表的用法 <br><br>1、相关宏 <br><br>标准C/C++包含头文件stdarg.h，该头文件中定义了如下三个宏： <br><br>void va_start ( va_list arg_ptr, prev_param ); /* ANSI version */ <br>type va_arg ( va_list arg_ptr, type ); <br>void va_end ( va_list arg_ptr ); <br><br>在这些宏中，va就是variable argument(可变参数)的意思；arg_ptr是指向可变参数表的指针；prev_param则指可变参数表的前一个固定参数；type为可变参数的类型。va_list也是一个宏，其定义为typedef char * va_list，实质上是一char型指针。char型指针的特点是++、--操作对其作用的结果是增1和减1（因为sizeof(char)为1），与之不同的是int等其它类型指针的++、--操作对其作用的结果是增sizeof(type)或减sizeof(type)，而且sizeof(type)大于1。 <br><br>通过va_start宏我们可以取得可变参数表的首指针，这个宏的定义为： <br><br>#define va_start ( ap, v ) ( ap = (va_list)&amp;v + _INTSIZEOF(v) ) <br><br>显而易见，其含义为将最后那个固定参数的地址加上可变参数对其的偏移后赋值给ap，这样ap就是可变参数表的首地址。其中的_INTSIZEOF宏定义为： <br><br>#define _INTSIZEOF(n) ((sizeof ( n ) + sizeof ( int ) - 1 ) &amp; ~( sizeof( int ) - 1 ) ) <br><br>va_arg宏的意思则指取出当前arg_ptr所指的可变参数并将ap指针指向下一可变参数，其原型为： <br><br>#define va_arg(list, mode) ((mode *)(list =\ <br>(char *) ((((int)list + (__builtin_alignof(mode)&lt;=4?3:7)) &amp;\ <br>(__builtin_alignof(mode)&lt;=4?-4:-8))+sizeof(mode))))[-1] <br><br>对这个宏的具体含义我们将在后面深入讨论。 <br><br>而va_end宏被用来结束可变参数的获取，其定义为： <br><br>#define va_end ( list ) <br><br>可以看出，va_end ( list )实际上被定义为空，没有任何真实对应的代码，用于代码对称，与va_start对应；另外，它还可能发挥代码的"自注释"作用。所谓代码的"自注释"，指的是代码能自己注释自己。 <br><br>下面我们以具体的例子来说明以上三个宏的使用方法。 <br><br>2、一个简单的例子 <br><br>#include &lt;stdarg.h&gt; <br>/* 函数名：max <br>* 功能：返回n个整数中的最大值 <br>* 参数：num：整数的个数 ...：num个输入的整数 <br>* 返回值：求得的最大整数 <br>*/ <br>int max ( int num, ... ) <br>{ <br>int m = -0x7FFFFFFF; /* 32系统中最小的整数 */ <br>va_list ap; <br>va_start ( ap, num ); <br>for ( int i= 0; i&lt; num; i++ ) <br>{ <br>int t = va_arg (ap, int); <br>if ( t &gt; m ) <br>{ <br>m = t; <br>} <br>} <br>va_end (ap); <br>return m; <br>} <br>/* 主函数调用max */ <br>int main ( int argc, char* argv[] ) <br>{ <br>int n = max ( 5, 5, 6 ,3 ,8 ,5); /* 求5个整数中的最大值 */ <br>cout &lt;&lt; n; <br>return 0; <br>} <br><br>函数max中首先定义了可变参数表指针ap，而后通过va_start ( ap, num )取得了参数表首地址（赋给了ap），其后的for循环则用来遍历可变参数表。这种遍历方式与我们在数据结构教材中经常看到的遍历方式是类似的。 <br><br><br>函数max看起来简洁明了，但是实际上printf的实现却远比这复杂。max函数之所以看起来简单，是因为： <br><br>(1) max函数可变参数表的长度是已知的，通过num参数传入； <br><br>(2) max函数可变参数表中参数的类型是已知的，都为int型。 <br><br>而printf函数则没有这么幸运。首先，printf函数可变参数的个数不能轻易的得到，而可变参数的类型也不是固定的，需由格式字符串进行识别（由%f、%d、%s等确定），因此则涉及到可变参数表的更复杂应用。 <br>参考资料：<a href="http://www.diybl.com/course/3_program/c++/cppsl/2008520/117226.html" target=_blank>http://www.diybl.com/course/3_program/c++/cppsl/2008520/117226.html</a> <br><br><br>引用自：<a href="http://zhidao.baidu.com/question/70029005.html">http://zhidao.baidu.com/question/70029005.html</a>
<img src ="http://www.cppblog.com/woka/aggbug/85556.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/woka/" target="_blank">woka</a> 2009-05-24 01:28 <a href="http://www.cppblog.com/woka/archive/2009/05/24/85556.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>