﻿<?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++博客-chenglong7997-文章分类-C++ 转载</title><link>http://www.cppblog.com/chenglong7997/category/18928.html</link><description /><language>zh-cn</language><lastBuildDate>Tue, 17 Jul 2012 02:08:03 GMT</lastBuildDate><pubDate>Tue, 17 Jul 2012 02:08:03 GMT</pubDate><ttl>60</ttl><item><title>深入理解C语言(转coolshell)</title><link>http://www.cppblog.com/chenglong7997/articles/183659.html</link><dc:creator>Snape</dc:creator><author>Snape</author><pubDate>Sun, 15 Jul 2012 22:10:00 GMT</pubDate><guid>http://www.cppblog.com/chenglong7997/articles/183659.html</guid><wfw:comment>http://www.cppblog.com/chenglong7997/comments/183659.html</wfw:comment><comments>http://www.cppblog.com/chenglong7997/articles/183659.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/chenglong7997/comments/commentRss/183659.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/chenglong7997/services/trackbacks/183659.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: Dennis Ritchie &nbsp;过世了，他发明了C语言，一个影响深远并彻底改变世界的计算机语言。一门经历40多年的到今天还长盛不衰的语言，今天很多语言都受到C的影响，C++，Java，C#，Perl， PHP， Javascript， 等等。但是，你对C了解吗？相信你看过本站的《C语言的谜题》还有《谁说C语言很简单？》，这里，我再写一篇关于深入理解C语言的文章，一方面是缅怀Dennis，...&nbsp;&nbsp;<a href='http://www.cppblog.com/chenglong7997/articles/183659.html'>阅读全文</a><img src ="http://www.cppblog.com/chenglong7997/aggbug/183659.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/chenglong7997/" target="_blank">Snape</a> 2012-07-16 06:10 <a href="http://www.cppblog.com/chenglong7997/articles/183659.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>程序员的奋斗史（一）——浅谈几种主要编程语言(转)</title><link>http://www.cppblog.com/chenglong7997/articles/170529.html</link><dc:creator>Snape</dc:creator><author>Snape</author><pubDate>Sun, 08 Apr 2012 18:06:00 GMT</pubDate><guid>http://www.cppblog.com/chenglong7997/articles/170529.html</guid><wfw:comment>http://www.cppblog.com/chenglong7997/comments/170529.html</wfw:comment><comments>http://www.cppblog.com/chenglong7997/articles/170529.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/chenglong7997/comments/commentRss/170529.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/chenglong7997/services/trackbacks/170529.html</trackback:ping><description><![CDATA[<div style="margin-top: 5px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; font-family: 'Microsoft YaHei'; font-size: 20px; line-height: 30px; text-align: left; background-color: #ffffff; "><h3><a href="http://blog.csdn.net/wentasy/article/details/7425698" style="color: #000000; text-decoration: none; ">程序员的奋斗史（一）&#8212;&#8212;浅谈几种主要编程语言</a></h3></div><div style="padding-top: 5px; padding-bottom: 5px; color: #999999; font-family: Arial; font-size: 12px; line-height: 24px; text-align: right; background-color: #ffffff; "><span style="margin-top: 0px; margin-right: 5px; margin-bottom: 0px; margin-left: 5px; float: left; ">分类：&nbsp;<a href="http://blog.csdn.net/Wentasy/article/category/902817" style="color: #ca0000; text-decoration: none; ">Others</a></span><span style="margin-top: 0px; margin-right: 5px; margin-bottom: 0px; margin-left: 0px; ">2012-04-04 15:36</span>&nbsp;<span title="阅读次数" style="margin-top: 0px; margin-right: 5px; margin-bottom: 0px; margin-left: 5px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 14px; background-image: url(http://static.blog.csdn.net/images/ico_view.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: 0% 50%; background-repeat: no-repeat no-repeat; ">8321人阅读</span>&nbsp;<span title="评论次数" style="margin-top: 0px; margin-right: 5px; margin-bottom: 0px; margin-left: 5px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 14px; background-image: url(http://static.blog.csdn.net/images/ico_comm.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: 0% 50%; background-repeat: no-repeat no-repeat; "><a href="http://blog.csdn.net/wentasy/article/details/7425698#comments" style="color: #ca0000; text-decoration: none; ">评论</a>(87)</span>&nbsp;<span style="margin-top: 0px; margin-right: 5px; margin-bottom: 0px; margin-left: 5px; "><a href="http://blog.csdn.net/wentasy/article/details/7425698" title="收藏" style="color: #ca0000; text-decoration: none; ">收藏</a></span>&nbsp;<span style="margin-top: 0px; margin-right: 5px; margin-bottom: 0px; margin-left: 5px; "><a href="http://blog.csdn.net/wentasy/article/details/7425698#report" title="举报" style="color: #ca0000; text-decoration: none; ">举报</a></span></div><div id="article_content" style="margin-top: 20px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; "><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="font-size: 12px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-size: 16px; ">&nbsp;&nbsp;经常听到有人在抱怨这个语言哪里哪里不好，那个语言又是如何的优秀。对于这样的牢骚，我只是一笑而过。</span></p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="font-size: 16px; "><span style="font-family: Calibri; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>就我而言，语言只是工具，没有好坏之分。只要你采用相应的语言，完成对应的工作，那你的目标就完成了。我们无需抱怨这种语言的缺点，而是应该抱有接受的态度。任何语言的存在都有它的合理性，如果不合理是不会经受住时间的考验的。世间之物都具有双面性，只是看利多还是弊大。如果一个东西是十全十美的，那我毫不犹豫的告诉你，一种情况是它不存在，另一种情况是你还没发现它的缺点。</span></p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="font-size: 16px; "><span style="font-family: Calibri; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>每种语言都有自己得天独厚的优势，当然缺点同时伴存。<span style="font-family: Calibri; ">C</span>语言是计算机界的常青藤。<span style="font-family: Calibri; ">C</span>语言执行效率高，用于写底层的驱动那是再适合不过。指针是<span style="font-family: Calibri; ">C</span>语言的精华。而<span style="font-family: Calibri; ">C++</span>是很强大的，只要你是个高手，你几乎可以利用<span style="font-family: Calibri; ">C++</span>完成任何事情。当然虚函数是<span style="font-family: Calibri; ">C++</span>的精华。<span style="font-family: Calibri; ">Java</span>取消了指针，这是应该喜还是忧。喜的是，没有<span style="font-family: Calibri; ">C</span>或者<span style="font-family: Calibri; ">C++</span>那种对内存繁琐的操作，而且也降低了很多致命性的错误发生的概率。忧的是，效率大打折扣，采用虚拟机的方式使得<span style="font-family: Calibri; ">Java</span>有跨平台的特性，必然会使得效率降低。另外一个方面，你借别人的东西使用（内存），是不是应该还回去呢？从这一方面考虑，<span style="font-family: Calibri; ">C</span>或者<span style="font-family: Calibri; ">C++</span>的设计思想更有意义。经常关注<span style="font-family: Calibri; ">TIOBE</span>世界编程语言排行榜，<span style="font-family: Calibri; ">Java</span>已经有很久排名第一，我觉得这样的排名只是看看而已，无需过多的信奉。下面看看几种主要语言的优缺点。</span></p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">&nbsp;</p><p align="left" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="color: #ff0000; "><strong><span style="font-size: 18px; "><span style="font-family: Calibri; ">C</span>语言</span></strong></span></p><p align="left" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">&nbsp;</p><p align="left" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="color: #ff0000; "><span style="font-size: 16px; "><span style="font-family: Calibri; ">C</span>语言的优点：</span></span></p><p align="left" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="font-size: 16px; "><span style="font-family: Calibri; ">1.</span>简洁紧凑、灵活方便</span></p><p align="left" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="font-size: 16px; "><span style="font-family: Calibri; ">2.</span>运算符丰富</span></p><p align="left" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="font-size: 16px; "><span style="font-family: Calibri; ">3.</span>数据结构丰富</span></p><p align="left" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="font-size: 16px; "><span style="font-family: Calibri; ">4. C</span>是结构式语言</span></p><p align="left" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="font-size: 16px; "><span style="font-family: Calibri; ">5. C</span>语法限制不太严格，程序设计自由度大</span></p><p align="left" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="font-size: 16px; "><span style="font-family: Calibri; ">6. C</span>语言允许直接访问物理地址，可以直接对硬件进行操作</span></p><p align="left" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="font-size: 16px; "><span style="font-family: Calibri; ">7. C</span>语言程序生成代码质量高，程序执行效率高</span></p><p align="left" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="font-size: 16px; "><span style="font-family: Calibri; ">8. C</span>语言适用范围大，可移植性好</span></p><p align="left" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="font-size: 16px; "><span style="font-family: Calibri; ">9. C</span>语言有一个突出的优点就是适合于多种操作系统<span style="font-family: Calibri; ">,</span>如<span style="font-family: Calibri; ">DOS</span>、<span style="font-family: Calibri; ">UNIX,</span>也适用于多种机型</span></p><p align="left" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="font-size: 16px; "><span style="font-family: Calibri; ">10.</span>有效地将一个较复杂的程序系统设计任务分解成许多易于控制和处理的子任务，便于开发和维护</span></p><p align="left" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">&nbsp;</p><p align="left" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="color: #ff0000; "><span style="font-size: 16px; "><span style="font-family: Calibri; ">C</span>语言的缺点：</span></span></p><p align="left" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="font-size: 16px; "><span style="font-family: Calibri; ">1. C</span>语言的缺点主要是表现在数据的封装性上，这一点使得<span style="font-family: Calibri; ">C</span>在数据的安全性上做的有很大缺陷，这也是<span style="font-family: Calibri; ">C</span>和<span style="font-family: Calibri; ">C++</span>的一大区别。</span></p><p align="left" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="font-size: 16px; "><span style="font-family: Calibri; ">2. C</span>语言的语法限制不太严格，对变量的类型约束不严格，影响程序的安全性，对数组下标越界不作检查等。从应用的角度，<span style="font-family: Calibri; ">C</span>语言比其他高级语言较难掌握。</span></p><p align="left" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="font-size: 16px; "><span style="font-family: Calibri; ">3.</span>可重用性差</span></p><p align="left" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="font-size: 16px; "><span style="font-family: Calibri; ">4.</span>数据安全性差</span></p><p align="left" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="font-size: 16px; "><span style="font-family: Calibri; ">5.</span>难以开发大型软件和图形界面的应用软件</span></p><p align="left" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="font-size: 16px; "><span style="font-family: Calibri; ">6.</span>把数据和处理数据的过程分离为相互独立的实体</span></p><p align="left" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="font-size: 16px; "><span style="font-family: Calibri; ">7.</span>当数据结构改变时，所有相关的处理过程都要进行相应的修改</span></p><p align="left" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="font-size: 16px; "><span style="font-family: Calibri; ">8.</span>每一种相对于老问题的新方法都要带来额外的开销</span></p><p align="left" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="font-size: 16px; "><span style="font-family: Calibri; ">9.</span>图形用户界面的应用程序，很难用过程来描述和实现，开发和维护也都很困难</span></p><p align="left" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">&nbsp;</p><p align="left" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><strong><span style="color: #ff0000; "><span style="font-family: Calibri; font-size: 18px; ">C++</span></span></strong></p><p align="left" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">&nbsp;</p><p align="left" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="color: #ff0000; "><span style="font-size: 16px; "><span style="font-family: Calibri; ">C++</span>优点：</span></span></p><p align="left" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="font-size: 16px; ">1.可扩展性强</span></p><p align="left" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="font-size: 16px; ">2.高效 简洁 快速</span></p><p align="left" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="font-size: 16px; ">3.可移植性</span></p><p align="left" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="font-size: 16px; ">4.面向对象的特性</span></p><p align="left" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="font-size: 16px; ">5.强大而灵活的表达能力和不输于C的效率</span></p><p align="left" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="font-size: 16px; ">6.支持硬件开发<br />7.程序模块间的关系更为简单，程序模块的独立性、数据的安全性就有了良好的保障</span></p><p align="left" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="font-size: 16px; ">8.通过继承与多态性，可以大大提高程序的可重用性，使得软件的开发和维护都更为方便</span></p><p align="left" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">&nbsp;</p><p align="left" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">&nbsp;</p><p align="left" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="font-size: 16px; color: #ff0000; ">C++缺点：</span></p><p align="left" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="font-size: 16px; ">1.比较底层，易用性不是很好</span></p><p align="left" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="font-size: 16px; ">2.多重继承和友元机制</span></p><p align="left" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="font-size: 16px; ">3.标准库涵盖范围不足<br />4.开发周期长</span></p><p align="left" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="font-size: 16px; ">5.非并行</span></p><p align="left" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="font-size: 16px; ">6.掌握有难度</span></p><p align="left" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="font-size: 16px; ">7.粒度封装不够</span></p><p align="left" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">&nbsp;</p><p align="left" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="font-size: 18px; color: #ff0000; "><strong>Java</strong></span></p><p align="left" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">&nbsp;</p><p align="left" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="font-size: 16px; color: #ff0000; ">Java的优点：</span></p><p align="left" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="font-size: 16px; ">1.简单性</span></p><p align="left" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="font-size: 16px; ">2.面向对象性（面向对象的程度可以达到95%）</span></p><p align="left" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="font-size: 16px; ">3.健壮性</span></p><p align="left" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="font-size: 16px; ">4.跨平台性</span></p><p align="left" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="font-size: 16px; ">5.高性能（自动垃圾回收机制）</span></p><p align="left" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="font-size: 16px; ">6.多线程</span></p><p align="left" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="font-size: 16px; ">7.动态性</span></p><p align="left" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="font-size: 16px; ">8.安全性</span></p><p align="left" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">&nbsp;</p><p align="left" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="font-size: 16px; color: #ff0000; ">Java的缺点：</span></p><p align="left" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="font-size: 16px; ">1.效率低（毋庸置疑）</span></p><p align="left" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="font-size: 16px; ">2.跨平台是它最大的优点也是最大的缺点</span></p><p align="left" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="font-size: 16px; ">3.复杂性（做一个项目需要很多的知识，涉及面广）</span></p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">&nbsp;</p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="font-size: 16px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 很多时候，一种语言的优点很可能是它的缺点，缺点又可能是它的优点。这主要看你是从哪方面去考虑。这就印证了那句话，凡事没有必然。</span></p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="font-size: 16px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;不管是哪种语言，就像文章开头所言，只要你采用他达到你想要的，那就成功了。无需过多在意它的缺点，更无需赞扬它的优点。所以网上那些关于哪种语言更好的口水仗，我只能说这样的争斗毫无意义。</span></p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="font-size: 16px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 语言只是工具，没有好坏之分</span></p></div><img src ="http://www.cppblog.com/chenglong7997/aggbug/170529.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/chenglong7997/" target="_blank">Snape</a> 2012-04-09 02:06 <a href="http://www.cppblog.com/chenglong7997/articles/170529.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>类间关系（转）</title><link>http://www.cppblog.com/chenglong7997/articles/169746.html</link><dc:creator>Snape</dc:creator><author>Snape</author><pubDate>Sun, 01 Apr 2012 00:15:00 GMT</pubDate><guid>http://www.cppblog.com/chenglong7997/articles/169746.html</guid><wfw:comment>http://www.cppblog.com/chenglong7997/comments/169746.html</wfw:comment><comments>http://www.cppblog.com/chenglong7997/articles/169746.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/chenglong7997/comments/commentRss/169746.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/chenglong7997/services/trackbacks/169746.html</trackback:ping><description><![CDATA[<div style="font-family: arial; word-wrap: break-word; font-size: 15px; font-weight: bold; position: relative; padding-top: 32px; padding-right: 15px; padding-bottom: 8px; padding-left: 28px; margin-top: 5px; margin-right: 0px; margin-bottom: 15px; margin-left: 0px; background-image: url(http://www.cnblogs.com/Skins/gray/images/bg_post_tilte.gif); background-attachment: scroll; line-height: 18px; border-image: initial; background-repeat: no-repeat no-repeat; "><a id="cb_post_title_url" href="http://www.cnblogs.com/floodpeak/archive/2008/02/27/1083533.html" style="border-bottom-style: dotted; border-bottom-width: 0px; border-bottom-color: #efefef; color: #555555; text-decoration: none; ">类间的关系</a></div><div id="cnblogs_post_body" style="font-family: verdana; word-wrap: break-word; font-size: 15px; line-height: 18px; "><p>网上关于此类的讨论非常多，发现对于该问题的理解各有各的说法，而各个说法中又相去甚远。通过浏览这些讨论以及对《O'Reilly - UML 2.0 In A Nutshell (2007)》的参考，发表一下自己的看法</p><p>类间关系有很多种，在大的类别上可以分为两种：纵向关系、横向关系。</p><p>纵向关系就是继承关系，它的概念非常明确，也成为OO的三个重要特征之一，这里不过多的讨论。</p><p>横向关系较为微妙，按照UML的建议大体上可以分为四种：</p><ol style="margin-left: 2em; "><li style="list-style-type: decimal; margin-left: 10px; ">依赖&nbsp;&nbsp;&nbsp; （Dependency）</li><li style="list-style-type: decimal; margin-left: 10px; ">关联&nbsp;&nbsp;&nbsp; （Association）</li><li style="list-style-type: decimal; margin-left: 10px; ">聚合&nbsp;&nbsp;&nbsp; （Aggregation）</li><li style="list-style-type: decimal; margin-left: 10px; ">组合&nbsp;&nbsp;&nbsp; （Composition）</li></ol><p>它们的强弱关系是没有异议的：依赖 &lt; 关联 &lt; 聚合 &lt; 组合</p><p>然而它们四个之间的差别却又不那么好拿捏，需要好好体会。</p><ol style="margin-left: 2em; "><li style="list-style-type: decimal; margin-left: 10px; "><span style="color: #0000ff; "><strong>依赖</strong></span>：<ul style="list-style-type: disc; margin-left: 45px; "><li style="list-style-type: decimal; margin-left: 10px; list-style: inherit; ">UML表示法：虚线 + 箭头&nbsp;<br /><img height="58" alt="" src="http://images.cnblogs.com/cnblogs_com/floodpeak/1.gif" width="321" border="0" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; " /></li><li style="list-style-type: decimal; margin-left: 10px; list-style: inherit; ">关系：" ... uses a ..."</li><li style="list-style-type: decimal; margin-left: 10px; list-style: inherit; ">此关系最为简单，也最好理解，所谓依赖就是某个对象的功能依赖于另外的某个对象，而被依赖的对象只是作为一种工具在使用，而并不持有对它的引用。</li><li style="list-style-type: decimal; margin-left: 10px; list-style: inherit; ">典型的例子很多，比如：<br />class Human<br />{<br />&nbsp;&nbsp;&nbsp; public void&nbsp;breath()<br />&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Air freshAir&nbsp;= new Air();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; freshAir.releasePower();<br />&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp; public static void main()<br />&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Human me&nbsp;= new Human();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while(true)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;me.breath();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp; }<br />}<br /><br />class Air<br />{<br />&nbsp;&nbsp;&nbsp; public void releasePower()<br />&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //do sth.<br />&nbsp;&nbsp;&nbsp; }<br />}<br />&nbsp;<img height="211" alt="" src="http://images.cnblogs.com/cnblogs_com/floodpeak/5.gif" width="474" border="0" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; left: 569px; top: 888px; " /></li><li style="list-style-type: decimal; margin-left: 10px; list-style: inherit; ">释义：一个人自创生就需要不停的呼吸，而人的呼吸功能之所以能维持生命就在于吸进来的气体发挥了作用，所以说空气只不过是人类的一个工具，而人并不持有对它的引用。</li></ul></li><li style="list-style-type: decimal; margin-left: 10px; "><span style="color: #0000ff; "><strong>关联</strong></span>：<ul style="list-style-type: disc; margin-left: 45px; "><li style="list-style-type: decimal; margin-left: 10px; list-style: inherit; ">UML表示法：实线 + 箭头&nbsp;<br /><img height="58" alt="" src="http://images.cnblogs.com/cnblogs_com/floodpeak/2.gif" width="321" border="0" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; " /></li><li style="list-style-type: decimal; margin-left: 10px; list-style: inherit; ">关系：" ... has a ..."</li><li style="list-style-type: decimal; margin-left: 10px; list-style: inherit; ">所谓关联就是某个对象会长期的持有另一个对象的引用，而二者的关联往往也是相互的。关联的两个对象彼此间没有任何强制性的约束，只要二者同意，可以随时解除关系或是进行关联，它们在生命期问题上没有任何约定。被关联的对象还可以再被别的对象关联，所以关联是可以共享的。</li><li style="list-style-type: decimal; margin-left: 10px; list-style: inherit; ">典型的例子很多，比如：<br />class Human<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;ArrayList friends = new ArrayList();<br />&nbsp;&nbsp;&nbsp; public void&nbsp;makeFriend(Human human)<br />&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; friends.add(human);<br />&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp; public static void main()<br />&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Human me&nbsp;= new Human();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while(true)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;me.makeFriend(mySchool.getStudent());<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp; }<br />}&nbsp;<br /><img height="141" alt="" src="http://images.cnblogs.com/cnblogs_com/floodpeak/6.gif" width="366" border="0" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; left: 561px; top: 1604px; " /></li><li style="list-style-type: decimal; margin-left: 10px; list-style: inherit; ">释义：人从生至死都在不断的交朋友，然而没有理由认为朋友的生死与我的生死有必然的联系，故他们的生命期没有关联，我的朋友又可以是别人的朋友，所以朋友可以共享。</li></ul></li><li style="list-style-type: decimal; margin-left: 10px; "><span style="color: #0000ff; "><strong>聚合</strong></span>：&nbsp;&nbsp;<ul style="list-style-type: disc; margin-left: 45px; "><li style="list-style-type: decimal; margin-left: 10px; list-style: inherit; ">UML表示法：空心菱形 + 实线 + 箭头&nbsp;<br /><img height="58" alt="" src="http://images.cnblogs.com/cnblogs_com/floodpeak/3.gif" width="321" border="0" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; " /></li><li style="list-style-type: decimal; margin-left: 10px; list-style: inherit; ">关系：" ... owns a ..."</li><li style="list-style-type: decimal; margin-left: 10px; list-style: inherit; ">聚合是强版本的关联。它暗含着一种所属关系以及生命期关系。被聚合的对象还可以再被别的对象关联，所以被聚合对象是可以共享的。虽然是共享的，聚合代表的是一种更亲密的关系。</li><li style="list-style-type: decimal; margin-left: 10px; list-style: inherit; ">典型的例子很多，比如：<br />class Human<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;Home myHome;<br />&nbsp;&nbsp;&nbsp; public void&nbsp;goHome()<br />&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//在回家的路上<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; myHome.openDoor();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //看电视<br />&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp; public static void main()<br />&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Human me&nbsp;= new Human();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while(true)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //上学<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //吃饭<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;me.goHome();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp; }<br />}<br />&nbsp;<img height="221" alt="" src="http://images.cnblogs.com/cnblogs_com/floodpeak/7.gif" width="566" border="0" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; left: 477px; top: 2185px; " /></li><li style="list-style-type: decimal; margin-left: 10px; list-style: inherit; ">释义：我的家和我之间具有着一种强烈的所属关系，我的家是可以分享的，而这里的分享又可以有两种。其一是聚合间的分享，这正如你和你媳妇儿都对这个家有着同样的强烈关联；其二是聚合与关联的分享，如果你的朋友来家里吃个便饭，估计你不会给他配一把钥匙。</li></ul></li><li style="list-style-type: decimal; margin-left: 10px; "><span style="color: #0000ff; "><strong>组合</strong></span>：<ul style="list-style-type: disc; margin-left: 45px; "><li style="list-style-type: decimal; margin-left: 10px; list-style: inherit; ">UML表示法：实心菱形 + 实线 + 箭头&nbsp;<br /><img height="58" alt="" src="http://images.cnblogs.com/cnblogs_com/floodpeak/4.gif" width="321" border="0" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; " /></li><li style="list-style-type: decimal; margin-left: 10px; list-style: inherit; ">关系：" ... is a&nbsp;part of &nbsp;..."</li><li style="list-style-type: decimal; margin-left: 10px; list-style: inherit; ">组合是关系当中的最强版本，它直接要求包含对象对被包含对象的拥有以及包含对象与被包含对象生命期的关系。被包含的对象还可以再被别的对象关联，所以被包含对象是可以共享的，然而绝不存在两个包含对象对同一个被包含对象的共享。</li><li style="list-style-type: decimal; margin-left: 10px; list-style: inherit; ">典型的例子很多，比如：<br />class Human<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;Heart myHeart = new Heart();<br />&nbsp;&nbsp;&nbsp;&nbsp;public static void main()<br />&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Human me&nbsp;= new Human();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while(true)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myHeart.beat();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp; }<br />}<br /><img height="215" alt="" src="http://images.cnblogs.com/cnblogs_com/floodpeak/8.gif" width="578" border="0" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; left: 321px; top: 2728px; " /></li><li style="list-style-type: decimal; margin-left: 10px; list-style: inherit; ">释义：组合关系就是整体与部分的关系，部分属于整体，整体不存在，部分一定不存在，然而部分不存在整体是可以存在的，说的更明确一些就是部分必须创生于整体创生之后，而销毁于整体销毁之前。部分在这个生命期内可以被其它对象关联甚至聚合，但有一点必须注意，一旦部分所属于的整体销毁了，那么与之关联的对象中的引用就会成为空引用，这一点可以利用程序来保障。心脏的生命期与人的生命期是一致的，如果换个部分就不那么一定，比如阑尾，很多人在创生后的某个时间对其厌倦便提前销毁了它，可它和人类的关系不可辩驳的属于组合。<br />在UML中存在一种特例，就是允许被包含对象在包含对象销毁前转移给新的对象，这虽然不自然，但它给需要心脏移植的患者带来了福音。</li></ul></li></ol></div><img src ="http://www.cppblog.com/chenglong7997/aggbug/169746.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/chenglong7997/" target="_blank">Snape</a> 2012-04-01 08:15 <a href="http://www.cppblog.com/chenglong7997/articles/169746.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>教你文本聚类（转）</title><link>http://www.cppblog.com/chenglong7997/articles/169741.html</link><dc:creator>Snape</dc:creator><author>Snape</author><pubDate>Sat, 31 Mar 2012 23:57:00 GMT</pubDate><guid>http://www.cppblog.com/chenglong7997/articles/169741.html</guid><wfw:comment>http://www.cppblog.com/chenglong7997/comments/169741.html</wfw:comment><comments>http://www.cppblog.com/chenglong7997/articles/169741.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/chenglong7997/comments/commentRss/169741.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/chenglong7997/services/trackbacks/169741.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 教你文本聚类2009-08-23 18:32&nbsp;189人阅读&nbsp;评论(0)&nbsp;收藏&nbsp;举报摘 要：文本聚类是搜索引擎和语义web的基本技术，这次本蛙和大家一起学习一下简单的文本聚类算法，可能不能直接用于实际应用中，但对于想学搜索技术的初学 者还是有一定入门作用的。这里会用到TF/IDF权重，用余弦夹角计算文本相似度，用方差计算两个数据间欧式距离，用k-means进行...&nbsp;&nbsp;<a href='http://www.cppblog.com/chenglong7997/articles/169741.html'>阅读全文</a><img src ="http://www.cppblog.com/chenglong7997/aggbug/169741.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/chenglong7997/" target="_blank">Snape</a> 2012-04-01 07:57 <a href="http://www.cppblog.com/chenglong7997/articles/169741.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>字符串相似度算法介绍(整理)(转)</title><link>http://www.cppblog.com/chenglong7997/articles/169740.html</link><dc:creator>Snape</dc:creator><author>Snape</author><pubDate>Sat, 31 Mar 2012 23:52:00 GMT</pubDate><guid>http://www.cppblog.com/chenglong7997/articles/169740.html</guid><wfw:comment>http://www.cppblog.com/chenglong7997/comments/169740.html</wfw:comment><comments>http://www.cppblog.com/chenglong7997/articles/169740.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/chenglong7997/comments/commentRss/169740.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/chenglong7997/services/trackbacks/169740.html</trackback:ping><description><![CDATA[<p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">最近在做这方面的应用，把我找到的资料贴出来，有需要的人可以参考参考。<br />1．编辑距离（Levenshtein Distance）<br />编辑距离就是用来计算从原串（s）转换到目标串(t)所需要的最少的插入，删除和替换<br />的数目，在NLP中应用比较广泛，如一些评测方法中就用到了（wer,mWer等），同时也常用来计算你对原文本所作的改动数。编辑距离的算法是首先由俄国科学家Levenshtein提出的，故又叫Levenshtein Distance。<br />Levenshtein Distance算法可以看作动态规划。它的思路就是从两个字符串的左边开始比较,记录已经比较过的子串相似度(实际上叫做距离),然后进一步得到下一个 字符位置时的相似度。 用下面的例子: GUMBO和GAMBOL。当算到矩阵D[3,3]位置时,也就是当比较到GUM和GAM时,要从已经比较过的3对子串GU-GAM, GUM-GA和GU-GA之中选一个差别最小的来当它的值. 所以要从左上到右下构造矩阵。<br />编辑距离的伪算法：<br />整数 Levenshtein距离(字符 str1[1..lenStr1], 字符 str2[1..lenStr2])<br />&nbsp;&nbsp; 宣告 int d[0..lenStr1, 0..lenStr2]<br />&nbsp;&nbsp; 宣告 int i, j, cost<br />&nbsp;<br />&nbsp;&nbsp; 对于 i 等于 由 0 至 lenStr1<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; d[i, 0] := i<br />&nbsp;&nbsp; 对于 j 等于 由 0 至 lenStr2<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; d[0, j] := j<br />&nbsp;&nbsp; 对于 i 等于 由 1 至 lenStr1<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 对于 j 等于 由 1 至 lenStr2<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 若 str1[i] = str2[j] 则 cost := 0<br />&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; 否则 cost := 1<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; d[i, j] := 最小值(<br />&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; d[i-1, j&nbsp; ] + 1,&nbsp;&nbsp;&nbsp;&nbsp; // 删除<br />&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; d[i&nbsp; , j-1] + 1,&nbsp;&nbsp;&nbsp;&nbsp; // 插入<br />&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; d[i-1, j-1] + cost&nbsp;&nbsp; // 替换<br />&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; )<br />返回 d[lenStr1, lenStr2]</p><p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">double Minimum(double a, double b, double c)&nbsp;<br />{&nbsp;<br />&nbsp;double mi;&nbsp;<br />&nbsp;<br />&nbsp;mi = a;&nbsp;<br />&nbsp;if (b &lt; mi) {&nbsp;<br />&nbsp;&nbsp;mi = b;&nbsp;<br />&nbsp;}&nbsp;<br />&nbsp;if (c &lt; mi) {&nbsp;<br />&nbsp;&nbsp;mi = c;&nbsp;<br />&nbsp;}&nbsp;<br />&nbsp;return mi;&nbsp;<br />}</p><p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; "><br />int* GetCellPointer(int *pOrigin, int col, int row, int nCols)&nbsp;<br />{&nbsp;<br />&nbsp;return pOrigin + col + (row * (nCols + 1));&nbsp;<br />}</p><p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">int GetAt(int *pOrigin, int col, int row, int nCols)&nbsp;<br />{&nbsp;<br />&nbsp;int *pCell;&nbsp;<br />&nbsp;<br />&nbsp;pCell = GetCellPointer (pOrigin, col, row, nCols);&nbsp;<br />&nbsp;return *pCell;&nbsp;<br />}</p><p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">void PutAt(int *pOrigin, int col, int row, int nCols, double x)&nbsp;<br />{&nbsp;<br />&nbsp;int *pCell;&nbsp;<br />&nbsp;pCell = GetCellPointer (pOrigin, col, row, nCols);&nbsp;<br />&nbsp;*pCell = x;&nbsp;<br />}</p><p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">//编辑距离<br />LD(const char *s, const char *t)<br />{<br />&nbsp;int *d; // pointer to matrix<br />&nbsp;int n; // length of s<br />&nbsp;int m; // length of t<br />&nbsp;int i; // iterates through s<br />&nbsp;int j; // iterates through t<br />&nbsp;char s_i1; // ith character of s<br />&nbsp;char s_i2; // ith character of s<br />&nbsp;char t_j1; // jth character of t<br />&nbsp;char t_j2; // jth character of t<br />&nbsp;int *cost; // cost代价矩阵<br />&nbsp;int result; // result<br />&nbsp;int cell; // contents of target cell<br />&nbsp;int above; // contents of cell immediately above<br />&nbsp;int left; // contents of cell immediately to left<br />&nbsp;int diag; // contents of cell immediately above and to left<br />&nbsp;int sz; // number of cells in matrix</p><p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">&nbsp;// Step 1</p><p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">&nbsp;n = strlen (s);<br />&nbsp;m = strlen (t);<br />&nbsp;if (n == 0)&nbsp;<br />&nbsp;{<br />&nbsp; return m;<br />&nbsp;}<br />&nbsp;if (m == 0)&nbsp;<br />&nbsp;{<br />&nbsp; return n;<br />&nbsp;}<br />&nbsp;sz = (n+1) * (m+1) * sizeof (int);<br />&nbsp;d = (int *) malloc (sz);<br />&nbsp;cost = (int *) malloc (sz);</p><p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">&nbsp;// Step 2</p><p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">&nbsp;for (i = 0; i &lt;= n; i++)&nbsp;<br />&nbsp;{<br />&nbsp; PutAt (d, i, 0, n, i);<br />&nbsp;}</p><p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">&nbsp;for (j = 0; j &lt;= m; j++)<br />&nbsp;{<br />&nbsp; PutAt (d, 0, j, n, j);<br />&nbsp;}<br />&nbsp;for (int g=0;g&lt;=m;g++)//把代价距离矩阵全部初始化为同一个值，以后可根据此值判断相应的方格是否被赋过值<br />&nbsp;{<br />&nbsp; for(int h=0;h&lt;=n;h++)<br />&nbsp; {<br />&nbsp;&nbsp; PutAt(cost,h,g,n,2);<br />&nbsp; }<br />&nbsp;}<br />&nbsp;//&nbsp;<a title="Powered by Text-Enhance" id="_GPLITA_0" href="http://blog.csdn.net/lkf0217/article/details/4466950#" in_rurl="http://www.textsrv.com/click?v=VVM6MTQyNTI6NjgwOnN0ZXA6ZTFlODc2MTUwNjU3ZTBiYTA1NjQ3MWM2YWE4YzQ5MzE6ei0xMDQ3LTE0MjEyOmJsb2cuY3Nkbi5uZXQ%3D" style="color: #336699; ">Step</a>&nbsp;3</p><p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">&nbsp;for (i = 1; i &lt;= n; i++)&nbsp;<br />&nbsp;{</p><p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">&nbsp; s_i1 = s[i-1];<br />&nbsp; s_i2 = s[i];<br />&nbsp; bool sbd=false;<br />&nbsp; bool tbd=false;<br />&nbsp; if(s_i1&gt;=' '&amp;&amp;s_i1&lt;<a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#61;&#37;&#50;&#55;&#64;&#37;&#50;&#55;&#37;&#55;&#67;&#37;&#55;&#67;&#115;&#95;&#105;&#49;&#37;&#51;&#69;&#61;&#37;&#50;&#55;&#65;&#37;&#50;&#55;&#38;&#97;&#109;&#112;&#59;&#38;&#97;&#109;&#112;&#59;&#115;&#95;&#105;&#49;&#37;&#51;&#67;&#61;&#37;&#50;&#55;&#37;&#55;&#69;&#37;&#50;&#55;" style="color: #336699; text-decoration: none; ">='@'||s_i1&gt;='A'&amp;&amp;s_i1&lt;='~'</a>&nbsp;)<br />&nbsp; {//s为标点符号或其他非中文符号和数字<br />&nbsp; sbd=true;<br />&nbsp; }<br />&nbsp; // Step 4</p><p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">&nbsp; for (j = 1; j &lt;= m; j++)&nbsp;<br />&nbsp; {</p><p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">&nbsp;&nbsp; tbd=false;<br />&nbsp;&nbsp; t_j1 = t[j-1];<br />&nbsp;&nbsp; t_j2 = t[j];<br />&nbsp;&nbsp; // Step 5<br />&nbsp;&nbsp; if(t_j1&gt;=' '&amp;&amp;t_j1&lt;<a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#61;&#37;&#50;&#55;&#64;&#37;&#50;&#55;&#37;&#55;&#67;&#37;&#55;&#67;&#116;&#95;&#106;&#49;&#37;&#51;&#69;&#61;&#37;&#50;&#55;&#65;&#37;&#50;&#55;&#38;&#97;&#109;&#112;&#59;&#38;&#97;&#109;&#112;&#59;&#116;&#95;&#106;&#49;&#37;&#51;&#67;&#61;&#37;&#50;&#55;&#37;&#55;&#69;&#37;&#50;&#55;" style="color: #336699; text-decoration: none; ">='@'||t_j1&gt;='A'&amp;&amp;t_j1&lt;='~'</a>&nbsp;)<br />&nbsp;&nbsp; {//t也为标点符号<br />&nbsp;&nbsp;&nbsp; tbd=true;<br />&nbsp;&nbsp; }<br />&nbsp;&nbsp; if(!sbd)<br />&nbsp;&nbsp; {//s为汉字<br />&nbsp;&nbsp;&nbsp; if(!tbd)<br />&nbsp;&nbsp;&nbsp; {//t也为汉字<br />&nbsp;&nbsp;&nbsp;&nbsp; if (s_i1 == t_j1&amp;&amp;s_i2 == t_j2)&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bool tt=false;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int temp=GetAt(cost,i,j,n);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(temp==2)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PutAt(cost,i,j,n,0);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; tt=true;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(tt)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {//因为st全市汉字，所以把代价矩阵他相邻的未赋过值的三个格赋值<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int temp1=GetAt(cost,i+1,j,n);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(temp1==2)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PutAt(cost,i+1,j,n,0);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int temp2=GetAt(cost,i,j+1,n);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(temp2==2)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PutAt(cost,i,j+1,n,0);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int temp3=GetAt(cost,i+1,j+1,n);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(temp3==2)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PutAt(cost,i+1,j+1,n,0);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp; else&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bool tt=false;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int temp=GetAt(cost,i,j,n);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(temp==2)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PutAt(cost,i,j,n,1);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; tt=true;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(tt)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int temp1=GetAt(cost,i+1,j,n);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(temp1==2)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PutAt(cost,i+1,j,n,1);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int temp2=GetAt(cost,i,j+1,n);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(temp2==2)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PutAt(cost,i,j+1,n,1);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int temp3=GetAt(cost,i+1,j+1,n);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(temp3==2)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PutAt(cost,i+1,j+1,n,1);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp; else<br />&nbsp;&nbsp;&nbsp; {//t为符号<br />&nbsp;&nbsp;&nbsp;&nbsp; bool tt=false;<br />&nbsp;&nbsp;&nbsp;&nbsp; int temp=GetAt(cost,i,j,n);<br />&nbsp;&nbsp;&nbsp;&nbsp; if(temp==2)<br />&nbsp;&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PutAt(cost,i,j,n,1);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; tt=true;<br />&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp; if(tt)<br />&nbsp;&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int temp1=GetAt(cost,i+1,j,n);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(temp1==2)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PutAt(cost,i+1,j,n,1);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp; }</p><p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">&nbsp;&nbsp; }<br />&nbsp;&nbsp; else<br />&nbsp;&nbsp; {//s为符号<br />&nbsp;&nbsp;&nbsp; if(!tbd)<br />&nbsp;&nbsp;&nbsp; {//t为汉字&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp; bool tt=false;<br />&nbsp;&nbsp;&nbsp;&nbsp; int temp=GetAt(cost,i,j,n);<br />&nbsp;&nbsp;&nbsp;&nbsp; if(temp==2)<br />&nbsp;&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PutAt(cost,i,j,n,1);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; tt=true;<br />&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp; if(tt)<br />&nbsp;&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int temp1=GetAt(cost,i,j+1,n);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(temp1==2)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PutAt(cost,i,j+1,n,1);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp; else<br />&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp; if(s_i1==t_j1)<br />&nbsp;&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int temp=GetAt(cost,i,j,n);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(temp==2)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PutAt(cost,i,j,n,0);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp; else<br />&nbsp;&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int temp=GetAt(cost,i,j,n);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(temp==2)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PutAt(cost,i,j,n,1);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp; }</p><p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">&nbsp;&nbsp; }</p><p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">&nbsp;&nbsp;&nbsp; // Step 6</p><p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">&nbsp;&nbsp; above = GetAt (d,i-1,j, n);<br />&nbsp;&nbsp; left = GetAt (d,i, j-1, n);<br />&nbsp;&nbsp; diag = GetAt (d, i-1,j-1, n);<br />&nbsp;&nbsp; int curcost=GetAt(cost,i,j,n);<br />&nbsp;&nbsp; cell = Minimum (above + 1, left + 1, diag + curcost);<br />&nbsp;&nbsp; PutAt (d, i, j, n, cell);<br />&nbsp; }<br />&nbsp;}</p><p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">&nbsp; // Step 7</p><p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">&nbsp; result = GetAt (d, n, m, n);<br />&nbsp; free (d);<br />&nbsp; return result;</p><p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">}</p><p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">2．最长公共子串 (LCS)<br />LCS问题就是求两个字符串最长公共子串的问题。解法就是用一个矩阵来记录两个字符<br />串中所有位置的两个字符之间的匹配情况，若是匹配则为1，否则为0。然后求出对角线最长的1序列，其对应的位置就是最长匹配子串的位置.<br />下面是字符串21232523311324和字符串312123223445的匹配矩阵，前者为X方向的，<br />后者为Y方向的。不难找到，红色部分是最长的匹配子串。通过查找位置我们得到最长的匹配子串为：21232<br />&nbsp;&nbsp;&nbsp; 0 0 0 1 0 0 0 1 1 0 0 1 0 0 0<br />　　0 1 0 0 0 0 0 0 0 1 1 0 0 0 0<br />　　1 0 1 0 1 0 1 0 0 0 0 0 1 0 0<br />　　0 1 0 0 0 0 0 0 0 1 1 0 0 0 0<br />　　1 0 1 0 1 0 1 0 0 0 0 0 1 0 0<br />　　0 0 0 1 0 0 0 1 1 0 0 1 0 0 0<br />　　1 0 1 0 1 0 1 0 0 0 0 0 1 0 0<br />　　1 0 1 0 1 0 1 0 0 0 0 0 1 0 0<br />　　0 0 0 1 0 0 0 1 1 0 0 1 0 0 0<br />　　0 0 0 0 0 0 0 0 0 0 0 0 0 1 0<br />　　0 0 0 0 0 0 0 0 0 0 0 0 0 1 0<br />　　0 0 0 0 0 1 0 0 0 0 0 0 0 0 0&nbsp;<br />　　0 0 0 0 0 0 0 0 0 0 0 0 0 0 0<br />但是在0和1的矩阵中找最长的1对角线序列又要花去一定的时间。通过改进矩阵的生成方式和设置标记变量，可以省去这部分时间。下面是新的矩阵生成方式：<br />&nbsp;&nbsp;&nbsp; 0 0 0 1 0 0 0 1 1 0 0 1 0 0 0<br />　　0 1 0 0 0 0 0 0 0 2 1 0 0 0 0<br />　　1 0 2 0 1 0 1 0 0 0 0 0 1 0 0<br />　　0 2 0 0 0 0 0 0 0 1 1 0 0 0 0<br />　　1 0 3 0 1 0 1 0 0 0 0 0 1 0 0<br />　　0 0 0 4 0 0 0 2 1 0 0 1 0 0 0<br />　　1 0 1 0 5 0 1 0 0 0 0 0 2 0 0<br />　　1 0 1 0 1 0 1 0 0 0 0 0 1 0 0<br />　　0 0 0 2 0 0 0 2 1 0 0 1 0 0 0<br />　　0 0 0 0 0 0 0 0 0 0 0 0 0 1 0<br />　　0 0 0 0 0 0 0 0 0 0 0 0 0 1 0<br />　　0 0 0 0 0 1 0 0 0 0 0 0 0 0 0<br />　　0 0 0 0 0 0 0 0 0 0 0 0 0 0 0<br />当 字符匹配的时候，我们并不是简单的给相应元素赋上1，而是赋上其左上角元素的值加一。我们用两个标记变量来标记矩阵中值最大的元素的位置，在矩阵生成的过 程中来判断当前生成的元素的值是不是最大的，据此来改变标记变量的值，那么到矩阵完成的时候，最长匹配子串的位置和长度就已经出来了。</p><p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">//最长公共子串<br />char* LCS(char*left,char* right){<br />&nbsp;int lenLeft,lenRight;<br />&nbsp;lenLeft = strlen(left);<br />&nbsp;lenRight = strlen(right);<br />&nbsp;int *c = new int[lenRight];<br />&nbsp;int start,end,len;<br />&nbsp;end = len = 0;<br />&nbsp;for(int i = 0; i &lt; lenLeft; i++){<br />&nbsp; for(int j = lenRight-1; j &gt;= 0; j--){<br />&nbsp;&nbsp; if(left[i] == right[j]){<br />&nbsp;&nbsp;&nbsp; if(i == 0 || j == 0)<br />&nbsp;&nbsp;&nbsp;&nbsp; c[j] = 1;<br />&nbsp;&nbsp;&nbsp; else<br />&nbsp;&nbsp;&nbsp;&nbsp; c[j] = c[j-1]+1;<br />&nbsp;&nbsp; }<br />&nbsp;&nbsp; else<br />&nbsp;&nbsp;&nbsp; c[j] = 0;<br />&nbsp;&nbsp; if(c[j] &gt; len){<br />&nbsp;&nbsp;&nbsp; len = c[j];<br />&nbsp;&nbsp;&nbsp; end = j;<br />&nbsp;&nbsp; }<br />&nbsp; }<br />&nbsp;}<br />&nbsp;char *p = new char[len+1];<br />&nbsp;start = end - len + 1;<br />&nbsp;for(i = start; i &lt;= end; i++)<br />&nbsp; p[i - start] = right[i];<br />&nbsp;p[len] = '/0';<br />&nbsp;return p;<br />}<br />3. 余弦定理 (向量空间算法)<br />余弦定理古老而广泛的数学概念，在各个学科及实践中都得到了大量的应用，这里简单的介绍下其在判断两个字符串相似度的应用。<br />在余弦定理中基本的公式为：</p><p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; "><a href="http://www.chinablog.com/UploadFiles/2006-12/129992553.gif" style="color: #336699; text-decoration: none; "><img src="http://www.chinablog.com/UploadFiles/2006-12/129992553.gif" alt="" style="border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; border-image: initial; " /></a></p><p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">假如字符串s1与s2，比较两个字符串的相似度，sim(s1,s2)，假设s1,s2中含有n个不同的字符，其分别为c1,c2,...cn，判 断字符串的相似度转换为两个字符串对应的向量v1,v2之间夹角大小的判断，余弦值越大其向量之间的夹角越小，s1与S2的相似度越大。<br />向量空间算法的介绍：<br />在 向量空间模型中，文本泛指各种机器可读的记录。用D（Document）表示，特征项（Term，用t表示）是指出现在文档D中且能够代表该文档内容的基 本语言单位，主要是由词或者短语构成，文本可以用特征项集表示为D(T1，T2，&#8230;，Tn)，其中Tk是特征项，1&lt;=k&lt;=N。例如一篇文 档中有a、b、c、d四个特征项，那么这篇文档就可以表示为D(a，b，c，d)。对含有n个特征项的文本而言，通常会给每个特征项赋予一定的权重表示其 重要程度。即D＝D(T1，W1；T2，W2；&#8230;，Tn，Wn)，简记为D＝D(W1，W2，&#8230;，Wn)，我们把它叫做文本D的向量表示。其中Wk是Tk 的权重，1&lt;=k&lt;=N。在上面那个例子中，假设a、b、c、d的权重分别为30，20，20，10，那么该文本的向量表示为 D(30，20，20，10)。在向量空间模型中，两个文本D1和D2之间的内容相关度Sim(D1，D2)常用向量之间夹角的余弦值表示，公式为：&nbsp;&nbsp;</p><p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; "><img src="http://www.chinablog.com/UploadFiles/2006-12/129240237.gif" alt="" style="border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; border-image: initial; " />&nbsp;<br />其中，W1k、W2k分别表示文本D1和D2第K个特征项的权值，1&lt;=k&lt;=N。我们可以利用类似的方法来计算两个字符串的相关度。&nbsp;&nbsp;&nbsp;&nbsp;<br />这个算法网上没找到，虽然我写过，但是没什么通用性，就不贴出来。很简单的，有兴趣的可以自己写一个。</p><img src ="http://www.cppblog.com/chenglong7997/aggbug/169740.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/chenglong7997/" target="_blank">Snape</a> 2012-04-01 07:52 <a href="http://www.cppblog.com/chenglong7997/articles/169740.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>【转】面向对象设计中的经验原则</title><link>http://www.cppblog.com/chenglong7997/articles/169739.html</link><dc:creator>Snape</dc:creator><author>Snape</author><pubDate>Sat, 31 Mar 2012 23:41:00 GMT</pubDate><guid>http://www.cppblog.com/chenglong7997/articles/169739.html</guid><wfw:comment>http://www.cppblog.com/chenglong7997/comments/169739.html</wfw:comment><comments>http://www.cppblog.com/chenglong7997/articles/169739.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/chenglong7997/comments/commentRss/169739.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/chenglong7997/services/trackbacks/169739.html</trackback:ping><description><![CDATA[<h1><a id="cb_post_title_url" href="http://www.cnblogs.com/shiyulun1984/archive/2009/05/19/1460644.html" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #6466b3; text-decoration: none; ">【转】面向对象设计中的经验原则</a></h1><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; clear: both; color: #464646; font-family: verdana, 'ms song', Arial, Helvetica, sans-serif; font-size: 12px; line-height: normal; text-align: left; background-color: #faf7ef; "></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 5px; padding-right: 2px; padding-bottom: 5px; padding-left: 5px; line-height: 1.5; color: #393939; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: black; font-family: verdana, 'ms song', Arial, Helvetica, sans-serif; text-align: left; background-color: #faf7ef; "><div id="cnblogs_post_body" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(1)所有数据都应该隐藏在所在的类的内部。</span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(2)类的使用者必须依赖类的共有接口，但类不能依赖它的使用者。</span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(3)尽量减少类的协议中的消息。</span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(4)实现所有类都理解的最基本公有接口[例如，拷贝操作(深拷贝和浅拷贝)、相等性判断、正确输出内容、从ASCII描述解析等等]。</span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(5)不要把实现细节(例如放置共用代码的私有函数)放到类的公有接口中。如果类的两个方法有一段公共代码，那么就可以创建一个防止这些公共代码的私有函数。</span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(6)不要以用户无法使用或不感兴趣的东西扰乱类的公有接口。</span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(7)类之间应该零耦合，或者只有导出耦合关系。也即，一个类要么同另一个类毫无关系，要么只使用另一个类的公有接口中的操作。</span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(8)类应该只表示一个关键抽象。包中的所有类对于同一类性质的变化应该是共同封闭的。一个变化若对一个包影响，则将对包中的所有类产生影响，而对其他的包不造成任何影响。</span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(9)把相关的数据和行为集中放置。设计者应当留意那些通过get之类操作从别的对象中获取数据的对象。这种类型的行为暗示着这条经验原则被违反了。</span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(10)把不相关的信息放在另一个类中(也即：互不沟通的行为)。朝着稳定的方向进行依赖。</span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(11)确保你为之建模的抽象概念是类，而不只是对象扮演的角色。</span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(12)在水平方向上尽可能统一地分布系统功能，也即：按照设计，顶层类应当统一地共享工作。</span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(13)在你的系统中不要创建全能类/对象。对名字包含Driver、Manager、System、Susystem的类要特别多加小心。规划一个接口而不是实现一个接口。</span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(14)对公共接口中定义了大量访问方法的类多加小心。大量访问方法意味着相关数据和行为没有集中存放。</span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(15)对包含太多互不沟通的行为的类多加小心。这个问题的另一表现是在你的应用程序中的类的公有接口中创建了很多的get和set函数。</span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(16)在由同用户界面交互的面向对象模型构成的应用程序中，模型不应该依赖于界面，界面则应当依赖于模型。</span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(17)尽可能地按照现实世界建模(我们常常为了遵守系统功能分布原则、避免全能类原则以及集中放置相关数据和行为的原则而违背这条原则) 。</span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(18)从你的设计中去除不需要的类。一般来说，我们会把这个类降级成一个属性。</span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(19)去除系统外的类。系统外的类的特点是，抽象地看它们只往系统领域发送消息但并不接受系统领域内其他类发出的消息。</span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(20)不要把操作变成类。质疑任何名字是动词或者派生自动词的类，特别是只有一个有意义行为的类。考虑一下那个有意义的行为是否应当迁移到已经存在或者尚未发现的某个类中。</span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(21)我们在创建应用程序的分析模型时常常引入代理类。在设计阶段，我们常会发现很多代理没有用的，应当去除。</span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(22)尽量减少类的协作者的数量。一个类用到的其他类的数目应当尽量少。</span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(23)尽量减少类和协作者之间传递的消息的数量。</span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(24)尽量减少类和协作者之间的协作量，也即：减少类和协作者之间传递的不同消息的数量。</span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(25)尽量减少类的扇出，也即：减少类定义的消息数和发送的消息数的乘积。</span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(26)如果类包含另一个类的对象，那么包含类应当给被包含的对象发送消息。也即：包含关系总是意味着使用关系。</span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(27)类中定义的大多数方法都应当在大多数时间里使用大多数数据成员。</span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(28)类包含的对象数目不应当超过开发者短期记忆的容量。这个数目常常是6。当类包含多于6个数据成员时，可以把逻辑相关的数据成员划分为一组，然后用一个新的包含类去包含这一组成员。</span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(29)让系统功能在窄而深的继承体系中垂直分布。</span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(30)在实现语义约束时，最好根据类定义来实现。这常常会导致类泛滥成灾，在这种情况下，约束应当在类的行为中实现，通常是在构造函数中实现，但不是必须如此。</span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(31)在类的构造函数中实现语义约束时，把约束测试放在构造函数领域所允许的尽量深的包含层次中。</span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(32)约束所依赖的语义信息如果经常改变，那么最好放在一个集中式的第3方对象中。</span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(33)约束所依赖的语义信息如果很少改变，那么最好分布在约束所涉及的各个类中。</span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(34)类必须知道它包含什么，但是不能知道谁包含它。</span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(35)共享字面范围(也就是被同一个类所包含)的对象相互之间不应当有使用关系。</span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(36)继承只应被用来为特化层次结构建模。</span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(37)派生类必须知道基类，基类不应该知道关于它们的派生类的任何信息。</span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(38)基类中的所有数据都应当是私有的，不要使用保护数据。类的设计者永远都不应该把类的使用者不需要的东西放在公有接口中。</span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(39)在理论上，继承层次体系应当深一点，越深越好。</span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(40)在实践中，继承层次体系的深度不应当超出一个普通人的短期记忆能力。一个广为接受的深度值是6。</span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(41)所有的抽象类都应当是基类。</span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(42)所有的基类都应当是抽象类。</span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(43)把数据、行为和/或接口的共性尽可能地放到继承层次体系的高端。</span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(44)如果两个或更多个类共享公共数据(但没有公共行为)，那么应当把公共数据放在一个类中，每个共享这个数据的类都包含这个类。</span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(45)如果两个或更多个类有共同的数据和行为(就是方法)，那么这些类的每一个都应当从一个表示了这些数据和方法的公共基类继承。</span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(46)如果两个或更多个类共享公共接口(指的是消息，而不是方法)，那么只有他们需要被多态地使用时，他们才应当从一个公共基类继承。</span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(47)对对象类型的显示的分情况分析一般是错误的。在大多数这样的情况下，设计者应当使用多态。</span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(48)对属性值的显示的分情况分析常常是错误的。类应当解耦合成一个继承层次结构，每个属性值都被变换成一个派生类。</span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(49)不要通过继承关系来为类的动态语义建模。试图用静态语义关系来为动态语义建模会导致在运行时切换类型。</span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(50)不要把类的对象变成派生类。对任何只有一个实例的派生类都要多加小心。</span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(51)如果你觉得需要在运行时刻创建新的类，那么退后一步以认清你要创建的是对象。现在，把这些对象概括成一个类。</span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(52)在派生类中用空方法(也就是什么也不做的方法)来覆写基类中的方法应当是非法的。</span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(53)不要把可选包含同对继承的需要相混淆。把可选包含建模成继承会带来泛滥成灾的类。</span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(54)在创建继承层次时，试着创建可复用的框架，而不是可复用的组件。</span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(55)如果你在设计中使用了多重继承，先假设你犯了错误。如果没犯错误，你需要设法证明。</span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(56)只要在面向对象设计中用到了继承，问自己两个问题：(1)派生类是否是它继承的那个东西的一个特殊类型？(2)基类是不是派生类的一部分？</span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(57)如果你在一个面向对象设计中发现了多重继承关系，确保没有哪个基类实际上是另一个基类的派生类。</span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(58)在面向对象设计中如果你需要在包含关系和关联关系间作出选择，请选择包含关系。</span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(59)不要把全局数据或全局函数用于类的对象的薄记工作。应当使用类变量或类方法。</span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(60)面向对象设计者不应当让物理设计准则来破坏他们的逻辑设计。但是，在对逻辑设计作出决策的过程中我们经常用到物理设计准则。</span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(61)不要绕开公共接口去修改对象的状态。</span></p></div></div><img src ="http://www.cppblog.com/chenglong7997/aggbug/169739.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/chenglong7997/" target="_blank">Snape</a> 2012-04-01 07:41 <a href="http://www.cppblog.com/chenglong7997/articles/169739.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>关于默认构造函数的几个错误认识（四种情况下，编译器会生成默认构造函数）</title><link>http://www.cppblog.com/chenglong7997/articles/169731.html</link><dc:creator>Snape</dc:creator><author>Snape</author><pubDate>Sat, 31 Mar 2012 22:19:00 GMT</pubDate><guid>http://www.cppblog.com/chenglong7997/articles/169731.html</guid><wfw:comment>http://www.cppblog.com/chenglong7997/comments/169731.html</wfw:comment><comments>http://www.cppblog.com/chenglong7997/articles/169731.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/chenglong7997/comments/commentRss/169731.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/chenglong7997/services/trackbacks/169731.html</trackback:ping><description><![CDATA[<p style="color: #333333; font-family: Verdana, Arial, sans-serif, 'Lucida Grande'; text-align: left; background-color: #d6d3d6; "><div>&nbsp;假期间闲来无事,就下载了某大师的VC++视频资料。在讲到C++时,说是如果程序员没有<span style="color: red; ">自己定义默认构造函数</span>,那么编译器会自动为我们<span style="color: red; ">产生一个默认的构造函数</span>。 本来这个错误的认识很多程序员都有,不足为奇。但有这么多年编程经验的高手也有这样的错误认识就不禁让我哑然了。</div><div>&nbsp; &nbsp; &nbsp; &nbsp; 其实编程语言和我们所用的任何软件没有区别,例如Photoshop、AutoCAD之类。其唯一不同的是我们用的编程语言是基于编译器的,而应用软件是基于我们的编程语言的。</div><div>&nbsp; &nbsp; &nbsp; &nbsp; 既然我们所用的软件是基于编译器的,那么理解编译器在背后到底为我们做了些什么、在什么情况下做了哪些事情就显得异常重要。这就像Photoshop会为你产生一些基本图形例如矩形、三角形之类,而不会凭空产生一些风景优美的图片一样。</div><div>&nbsp; &nbsp; &nbsp; &nbsp; 在《C++ Annotated Reference Manual(ARM)[ELLIS90]》中的Section 12.1告诉我们:"Default constructors...在需要的时候被编译器产生出来"。</div><div>&nbsp; &nbsp; &nbsp; &nbsp; 其实默认构造函数也是分为两类的:有用的、无用的。</div><div>&nbsp; &nbsp; &nbsp; &nbsp; 所谓<span style="color: red; ">有用的标准也是就默认构造函数会为我们的类做一些初始化操作</span>。那么无用的就不会做任何工作,从而对我们的类也就没有任何意义。所以,我们通常所说的默认构造函数是指<span style="color: red; ">有用的默认构造函数,其英文名字叫nontrivial default constructor。</span></div><div>&nbsp; &nbsp; &nbsp; &nbsp; 那么到底什么时候编译器会为我们产生nontrivial default constructor呢?有下面四中情况:</div><div>&nbsp; &nbsp; &nbsp; &nbsp;&#9312;如果一个类里面<span style="color: red; ">某个成员对象有nontrivial default constructor,编译器就会为我们的类产生nontrivial default constructor。</span></div><div>&nbsp; &nbsp; &nbsp; &nbsp;那么编译器这样做的理由是什么?</div><div>&nbsp; &nbsp; &nbsp; &nbsp;答案是因为类成员对象有nontrivial default constructor,那么编译器就需要<span style="color: red; ">显式的来调用这个类成员对象的</span>nontrivial default constructor。而编译器想显式的调用类成员对象的nontrivial default constructor,就需要自己来合成一些代码来调用。但是记住,编译器合成的nontrivial default constructor<span style="color: red; ">仅仅调用类成员对象的默认构造函数,而不对我们类里面的其它变量做任何初始化操作。</span></div><div>&nbsp; &nbsp; &nbsp; &nbsp; 也就是说,如果你想初始化类成员变量以外的变量例如一个int、一个String,那么必<span style="color: red; ">须自己定义默</span>认构造函数来完成这些变量的初始化。而编译器会对你定义的默认构造函数做相应的扩展,从而调用类成员对象的nontrivial default constructor。</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &#9313;如果一个派生类的<span style="color: red; ">基类有</span>nontrivial default constructor,那么编译器会为派生类合成一个nontrivial default constructor。</div><div>&nbsp; &nbsp; &nbsp; &nbsp; 编译器这样的理由是:因为派生类被合成时需要显式调用基类的默认构造函数。</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &#9314;如何一个类里面隐式的含有<span style="color: red; ">任何virtual function table(或vtbl)、pointer member(或vptr)。</span></div><div>&nbsp; &nbsp; &nbsp; &nbsp; 编译器这样做的理由很简单:因为这些vtbl或vptr需要编译器隐式(implicit)的合成出来,那么编译器就把合成动作放到了默认构造函数里面。所以编译器必须自己产生一个默认构造函数来完成这些操作。</div><div>&nbsp; &nbsp; &nbsp; &nbsp; 所以如果你的类里带有任何<span style="color: red; ">virtual function,那么编译器会为你合成一个默认构造函数。</span></div><div>&nbsp; &nbsp; &nbsp; &nbsp; &#9315;如果一个<span style="color: red; ">类虚继承于其它类。</span></div><div>&nbsp; &nbsp; &nbsp; &nbsp; 编译器这样做的理由和&#9314;类似:因为虚继承需要维护一个类似指针一样,可以动态的决定内存地址的东西(不同编译器对虚继承的实现不仅相同)。</div><div>&nbsp; &nbsp; &nbsp; &nbsp; 那么<span style="color: red; ">除了以上四种情况,编译器并不会为我们的类产生默认构造函数。</span></div><div>&nbsp; &nbsp; &nbsp; &nbsp; 所以编程中切忌想当然,要明白哪些事情是编译器做的,哪些事情需要程序员来完成的。就像堆所占用的资源需要程序员自己来释放,而栈空间是编译器管理的一样。</div><div>&nbsp; &nbsp; &nbsp; &nbsp; 只有如此,才能编写出质量更高的代码。</div></p><img src ="http://www.cppblog.com/chenglong7997/aggbug/169731.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/chenglong7997/" target="_blank">Snape</a> 2012-04-01 06:19 <a href="http://www.cppblog.com/chenglong7997/articles/169731.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>如何学好C++语言（转）</title><link>http://www.cppblog.com/chenglong7997/articles/169730.html</link><dc:creator>Snape</dc:creator><author>Snape</author><pubDate>Sat, 31 Mar 2012 21:51:00 GMT</pubDate><guid>http://www.cppblog.com/chenglong7997/articles/169730.html</guid><wfw:comment>http://www.cppblog.com/chenglong7997/comments/169730.html</wfw:comment><comments>http://www.cppblog.com/chenglong7997/articles/169730.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/chenglong7997/comments/commentRss/169730.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/chenglong7997/services/trackbacks/169730.html</trackback:ping><description><![CDATA[<h2><a id="cb_post_title_url" href="http://www.cnblogs.com/rollenholt/articles/2410007.html" style="color: #0066ff; text-decoration: none; ">如何学好C++语言（转）</a></h2><div style="padding-left: 5px; line-height: 1.5; color: #333333; font-family: Verdana, Arial, sans-serif, 'Lucida Grande'; text-align: left; background-color: #d6d3d6; "><div id="cnblogs_post_body" style="line-height: 1.8; "><p>原文地址：<a href="http://coolshell.cn/articles/4119.html" style="color: #0066ff; text-decoration: none; ">http://coolshell.cn/articles/4119.html</a></p><p>因为之前收藏的地址找不到了，所以重新发一下</p><p>昨天写了一篇<a title="如何学好C语言" href="http://coolshell.cn/articles/4102.html" target="_blank" style="color: #0066ff; text-decoration: none; ">如何学好C语言</a>，就有人回复问我如何学好C++，所以，我把我个人的一些学习经验写在这里，希望对大家有用。首先，因为<a title="如何学好C语言" href="http://coolshell.cn/articles/4102.html" target="_blank" style="color: #0066ff; text-decoration: none; ">如何学好C语言</a>中谈到了算法和系统，所以这里就只谈C++语言。</p><ul style="margin-left: 45px; "><li style="font-size: 10pt; list-style: inherit; "><strong>C++是最难的语言</strong>。这个世界上最难的编程语言可能非C++莫属了。你千万不要以为<a title="&#8220;21天教你学会C++&#8221;" href="http://coolshell.cn/articles/2250.html" target="_blank" style="color: #0066ff; text-decoration: none; ">几天就可以学好C++</a>，C++的学习曲线是相当BT的，你可以看看<a title="C++ 程序员自信心曲线图" href="http://coolshell.cn/articles/2287.html" target="_blank" style="color: #0066ff; text-decoration: none; ">这篇文章</a>。C++是一门很自由的语言，自由到了有点<a title="恐怖的C++语言" href="http://coolshell.cn/articles/1724.html" target="_blank" style="color: #0066ff; text-decoration: none; ">BT和恐怖的地步</a>。我甚至认为C++并不是一门成熟的编程语言，因为太容易犯错了。所以，<strong>你一定要在一开始就要有很小心谨慎的态度，并把C++当成一种难以训服的猛兽来看待</strong>。</li></ul><ul style="margin-left: 45px; "><li style="font-size: 10pt; list-style: inherit; "><strong>多问&#8220;为什么要这样&#8221;的问题</strong>。学习C++一定要多问几个&#8220;为什么是这样&#8221;，&#8220;凭什么要这样&#8221;的问题。比如：很多人知道C++有拷贝构造函数和初始化列表，但你真的知道为什么要有拷贝构造函数？为什么要有初始化列表吗？为什么要有template，为什么要有RTTI，为什么不是别的呢？难道就是为了让一门语言变得Cool一些吗？完全不是这样的，C++中的任何一个feature都有些实实在在的原因，<strong>你一定要去了解为什么要把C++设计成这样的原因，你才能学好C++</strong>。有空看看《<a href="http://product.china-pub.com/5217" target="_blank" style="color: #0066ff; text-decoration: none; ">C++演化和设计</a>》一书。</li></ul><p>&nbsp;</p><ul style="margin-left: 45px; "><li style="font-size: 10pt; list-style: inherit; "><strong>看书，大量的C++书</strong>。你可以按如下先后顺序阅读（下面这些书，我花了大约4-5年的时间，今天我还在随时温习）<ul style="list-style-type: disc; margin-left: 45px; "><li style="font-size: 10pt; list-style: inherit; ">《<a href="http://product.china-pub.com/28767" target="_blank" style="color: #0066ff; text-decoration: none; ">C++ Primer</a>》，这本初级读本可能让会你啃得很痛苦，所有的语言的特性和为什么都在里面了，好好读读。当然由C++之父写的《<a href="http://product.china-pub.com/196448" target="_blank" style="color: #0066ff; text-decoration: none; ">C++程序设计语言</a>》也不错。两本看一本就好了（我看的是前者）。</li><li style="font-size: 10pt; list-style: inherit; ">了解C++的语法仅仅是万里长征的第一步，你还需要看看《<a href="http://product.china-pub.com/197414" target="_blank" style="color: #0066ff; text-decoration: none; ">Effective C++</a>》和《<a href="http://product.china-pub.com/197665" target="_blank" style="color: #0066ff; text-decoration: none; ">More Effective C++</a>》这两本书并不厚，但我从02年就一直看到现在，每次读我都有新的体会，这两本书太经典了。如果你对C语言不熟，这两本书会让你回去补C语言的课。</li><li style="font-size: 10pt; list-style: inherit; "><a href="http://product.china-pub.com/4801" target="_blank" style="color: #0066ff; text-decoration: none; ">Think in C++</a>同样是另一本经典之极的书，学c++必读，但是中文版的翻译的很不好，所以还是去读英文版的吧。</li><li style="font-size: 10pt; list-style: inherit; ">《<a href="http://product.china-pub.com/38130&amp;ref=browse" target="_blank" style="color: #0066ff; text-decoration: none; ">C++沉思录</a>》同样非常值得一读，这里教的不是编程，而是思考的方法，这是相当珍贵的。</li><li style="font-size: 10pt; list-style: inherit; ">《<a href="http://product.china-pub.com/33333" target="_blank" style="color: #0066ff; text-decoration: none; ">Exceptional C++</a>》和《<a href="http://product.china-pub.com/197666" target="_blank" style="color: #0066ff; text-decoration: none; ">More Exceptional C++</a>》让你看看各种问题的解决方法和一些常见的经典错误。</li><li style="font-size: 10pt; list-style: inherit; ">《<a href="http://product.china-pub.com/16697" target="_blank" style="color: #0066ff; text-decoration: none; ">Advanced C++</a>》和《<a href="http://product.china-pub.com/9700" target="_blank" style="color: #0066ff; text-decoration: none; ">Modern C++</a>》可以让你知道C++各种神奇的用法。</li><li style="font-size: 10pt; list-style: inherit; ">《<a href="http://product.china-pub.com/9864" target="_blank" style="color: #0066ff; text-decoration: none; ">泛型编程与STL</a>》是把C++实践到了极致的东西。很强大。STL&#8212;&#8212;神一样的模板库（容器，算法和函数对象），不得不服。</li><li style="font-size: 10pt; list-style: inherit; ">《<a href="http://www.china-pub.com/3290&amp;ref=browse" target="_blank" style="color: #0066ff; text-decoration: none; ">深入探索C++对象模型</a>》让你了解编译器下的C++是什么样的，让你了解C++的性能并不差。这个对于C++的程序员太关键了。我以前写过的《<a href="http://blog.csdn.net/haoel/archive/2007/12/18/1948051.aspx" target="_blank" style="color: #0066ff; text-decoration: none; ">C++虚函数表解析</a>》还有《<a href="http://blog.csdn.net/haoel/archive/2008/10/15/3081328.aspx" target="_blank" style="color: #0066ff; text-decoration: none; ">C++对象内存布局</a>》属于这个范畴。</li></ul></li></ul><ul style="margin-left: 45px; "><li style="font-size: 10pt; list-style: inherit; "><strong>和Java语言做对比</strong>。我个人以为Java对C++这个并不成熟的语言做了很多调整，规范和限制。所以，对比一下Java和C++，想一想，为什么一些东西在C++中可以做，但在Java中却不行。比如：Java的异常是必需要catch的，不然就会编译不通过。为什么Java不提供操作符重载？为什么Java会引入接口来做多重继承？为什么Java没有像C++那样的I/O字符流？为什么Java不支持指针？为什么Java可以做到垃圾回收？等等。<strong>Java体现着很多面向对象设计的东西，学习Java有助于你学会怎么更好地使用C++来编程</strong>。</li></ul><ul style="margin-left: 45px; "><li style="font-size: 10pt; list-style: inherit; "><strong>面向对象设计</strong>&nbsp;。虽然<a title="面向对象是个骗局？！" href="http://coolshell.cn/articles/3036.html" target="_blank" style="color: #0066ff; text-decoration: none; ">面向对象可能是个骗局</a>。但是我觉得面向对象设计中的一些实践非常的不错，比如，单一原则，依赖倒置原则，等等，都非常地经典。《<a href="http://product.china-pub.com/25961" target="_blank" style="color: #0066ff; text-decoration: none; ">设计模式</a>》必需一读，《<a href="http://product.china-pub.com/47106" target="_blank" style="color: #0066ff; text-decoration: none; ">面向对象的分析和设计</a>》可以一读。<strong>但不可以设计模式为中心来编程，而应该是用设计模式来解藕</strong>。</li></ul><ul style="margin-left: 45px; "><li style="font-size: 10pt; list-style: inherit; "><strong>类库学习</strong>。看看MFC是怎么封装Windows API的，看看ACE是怎么面向对象的，看看boost是怎么玩面向对象的，看看CPPUnit又是怎么设计的。当然，<a title="JDK里的设计模式" href="http://coolshell.cn/articles/3320.html" target="_blank" style="color: #0066ff; text-decoration: none; ">Java的JDK中有太多的设计模式</a>，可以参考。</li></ul><p>希望没有吓到大家，并欢迎大家补充。</p><p><em><strong>&#8212;&#8212;&#8212;&#8212;&#8212;更新 2011/03/30 19:20&#8212;&#8212;&#8212;&#8212;</strong></em></p><p>更新几个观点：</p><ul style="margin-left: 45px; "><li style="font-size: 10pt; list-style: inherit; ">1）我不擅长写书评，所以推荐的这些书可能会让你有点看点没有感觉，你可以上豆瓣或是China-pub上看看书评。</li><li style="font-size: 10pt; list-style: inherit; ">2）C++有很多奇淫技巧，有的很BT，包括虚函数表，也许会有人觉得有点没意思，但我觉得很有意思，一方面可以了解一门语言的实现细节，另一方面可以开阔思路。我从学习这些知识中受益很多。</li><li style="font-size: 10pt; list-style: inherit; ">3）上述是我的个人的学习历程，我觉得对我很有效，所以是经验之谈。</li><li style="font-size: 10pt; list-style: inherit; ">4）这类的文章在网上有很多很多，我不是第一个写这样的文章，我也不是写得最好的，我并不希望用长篇大论来谈论什么。只是想给大家了解一下大概的学习样子。毕竟，C++博大精深，任何一篇文章都无法说好。不如就简单一些。</li></ul></div></div><img src ="http://www.cppblog.com/chenglong7997/aggbug/169730.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/chenglong7997/" target="_blank">Snape</a> 2012-04-01 05:51 <a href="http://www.cppblog.com/chenglong7997/articles/169730.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>一个类的实例化对象所占空间的大小</title><link>http://www.cppblog.com/chenglong7997/articles/169729.html</link><dc:creator>Snape</dc:creator><author>Snape</author><pubDate>Sat, 31 Mar 2012 19:25:00 GMT</pubDate><guid>http://www.cppblog.com/chenglong7997/articles/169729.html</guid><wfw:comment>http://www.cppblog.com/chenglong7997/comments/169729.html</wfw:comment><comments>http://www.cppblog.com/chenglong7997/articles/169729.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/chenglong7997/comments/commentRss/169729.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/chenglong7997/services/trackbacks/169729.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 一个类的实例化对象所占空间的大小注意不要说类的大小,是类的对象的大小.首先，类的大小是什么？确切的说，类只是一个类型定义，它是没有大小可言的。&nbsp;用sizeof运算符对一个类型名操作，得到的是具有该类型实体的大小。&nbsp;如果&nbsp;Class A;&nbsp;A obj;&nbsp;那么sizeof(A)==sizeof(obj)&nbsp;那么sizeof(A)的大小和成员的大...&nbsp;&nbsp;<a href='http://www.cppblog.com/chenglong7997/articles/169729.html'>阅读全文</a><img src ="http://www.cppblog.com/chenglong7997/aggbug/169729.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/chenglong7997/" target="_blank">Snape</a> 2012-04-01 03:25 <a href="http://www.cppblog.com/chenglong7997/articles/169729.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>extern "C"</title><link>http://www.cppblog.com/chenglong7997/articles/169628.html</link><dc:creator>Snape</dc:creator><author>Snape</author><pubDate>Sat, 31 Mar 2012 03:43:00 GMT</pubDate><guid>http://www.cppblog.com/chenglong7997/articles/169628.html</guid><wfw:comment>http://www.cppblog.com/chenglong7997/comments/169628.html</wfw:comment><comments>http://www.cppblog.com/chenglong7997/articles/169628.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/chenglong7997/comments/commentRss/169628.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/chenglong7997/services/trackbacks/169628.html</trackback:ping><description><![CDATA[<h3>简介</h3><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　extern "C" 包含双重含义，从字面上即可得到：首先，被它修饰的目标是&#8220;extern&#8221;的；其次，被它修饰的目标是&#8220;C&#8221;的。让我们来详细解读这两重含义。<br /></span>含义<span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; "><br /></span><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　（1） 被extern 限定的</span><a target="_blank" href="http://baike.baidu.com/view/15061.htm" style="color: #136ec2; font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">函数</a><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">或</span><a target="_blank" href="http://baike.baidu.com/view/296689.htm" style="color: #136ec2; font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">变量</a><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">是extern类型的：</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　a. extern修饰变量的声明。举例来说，如果文件a.c需要引用b.c中变量int v，就可以在a.c中声明extern int v，然后就可以引用变量v。这里需要注意的是，被引用的变量v的链接属性必须是外链接（external）的，也就是说a.c要引用到v，不只是取决于在a.c中声明extern int v，还取决于变量v本身是能够被引用到的。这涉及到c语言的另外一个话题－－变量的作用域。能够被其他模块以extern修饰符引用到的变量通常是全局变量。还有很重要的一点是，extern int v可以放在a.c中的任何地方，比如你可以在a.c中的函数fun定义的开头处声明extern int v，然后就可以引用到变量v了，只不过这样只能在函数fun作用域中引用v罢了，这还是变量作用域的问题。对于这一点来说，很多人使用的时候都心存顾虑。好像extern声明只能用于文件作用域似的。</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　b. extern修饰函数声明。从本质上来讲，变量和函数没有区别。函数名是指向函数二进制块开头处的指针。如果文件a.c需要引用b.c中的函数，比如在b.c中原型是int fun(int mu)，那么就可以在a.c中声明extern int fun（int mu），然后就能使用fun来做任何事情。就像变量的声明一样，extern int fun（int mu）可以放在a.c中任何地方，而不一定非要放在a.c的文件作用域的范围中。对其他模块中函数的引用，最常用的方法是包含这些函数声明的头文件。使用extern和包含头文件来引用函数有什么区别呢？extern的引用方式比包含头文件要简洁得多！extern的使用方法是直接了当的，想引用哪个函数就用extern声明哪个函数。这大概是KISS原则的一种体现吧！这样做的一个明显的好处是，会加速程序的编译（确切的说是预处理）的过程，节省时间。在大型C程序编译过程中，这种差异是非常明显的。</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　（2） 被extern "C"修饰的变量和函数是按照C语言方式编译和连接的；</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　未加extern &#8220;C&#8221;声明时的编译方式。</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　首先看看C++中对类似C的函数是怎样编译的。</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　作为一种</span><a target="_blank" href="http://baike.baidu.com/view/125370.htm" style="color: #136ec2; font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">面向对象</a><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">的语言，C++支持函数重载，而过程式语言C则不支持。函数被C++编译后在符号库中的名字与C语言的不同。例如，假设某个函数的原型为：</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　</span><a target="_blank" href="http://baike.baidu.com/view/1004734.htm" style="color: #136ec2; font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">void</a><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">foo( int x, int y );</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　</span><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; color: red; ">该函数被C编译器编译后在符号库中的名字为_foo，而C++编译器则会产生像_foo_int_int之类的名字</span><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">（不同的编译器可能生成的名字不同，但是都采用了相同的机制，生成的新名字称为&#8220;mangled name&#8221;）。_foo_int_int这样的名字包含了函数名、函数参数数量及类型信息，C++就是靠这种机制来实现函数重载的。例如，在C++中，函数void foo( int x, int y )与void foo( int x, float y )编译生成的符号是不相同的，后者为_foo_int_float。</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　同样地，</span><a target="_blank" href="http://baike.baidu.com/view/824.htm" style="color: #136ec2; font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">C++</a><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">中的变量除支持</span><a target="_blank" href="http://baike.baidu.com/view/552847.htm" style="color: #136ec2; font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">局部变量</a><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">外，还支持类成员变量和全局变量。用户所编写程序的类成员变量可能与全局变量同名，我们以"."来区分。而本质上，编译器在进行编译时，与函数的处理相似，也为类中的变量取了一个独一无二的名字，这个名字与用户程序中同名的全局变量名字不同。</span><div style="height: 30px; line-height: 30px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　</span><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; color: red; ">未加extern "C</span><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">"声明时的连接方式</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　假设在C++中，模块A的头文件如下：</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　// 模块A头文件　moduleA.h</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　#ifndef MODULE_A_H</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　#define MODULE_A_H</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　int foo( int x, int y );</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　#endif</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　在模块B中引用该函数：</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　// 模块B实现文件　moduleB.cpp</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　#i nclude "moduleA.h"</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　foo(2,3);</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　实际上，在连接阶段，连接器会从模块A生成的目标文件moduleA.obj中寻找</span><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; color: red; ">_foo_int_int</span><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">这样的符号！</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　加extern "C"声明后的编译和连接方式</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　</span><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; color: red; ">加extern "C</span><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">"声明后，模块A的头文件变为：</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　// 模块A头文件　moduleA.h</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　#ifndef MODULE_A_H</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　#define MODULE_A_H</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　extern "C" int foo( int x, int y );</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　#endif</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　在模块B的实现文件中仍然调用foo（2,3），其结果是：</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　（1）模块A编译生成foo的目标代码时，没有对其名字进行特殊处理，</span><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; color: red; ">采用了C语言的方式；</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　（2）连接器在为模块B的目标代码寻找foo(2,3)调用时，寻找的是未经修改的符号名</span><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; color: red; ">_foo。</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　如果在模块A中函数声明了foo为extern "C"类型，而模块B中包含的是extern int foo( int x, int y ) ，则模块B找不到模块A中的函数；反之亦然。</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　所以，可以用一句话概括extern &#8220;C&#8221;这个声明的真实目的（任何语言中的任何语法特性的诞生都不是随意而为的，来源于真实世界的需求驱动。我们在思考问题时，不能只停留在这个语言是怎么做的，还要问一问它为什么要这么做，动机是什么，这样我们可以更深入地理解许多问题）：</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　</span><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; color: red; ">实现C++与C及其它语言的混合编程。</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　明白了C++中extern "C"的设立动机，我们下面来具体分析extern "C"通常的使用技巧。</span><h3>extern "C"的惯用法</h3><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　（1）</span><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; color: red; ">在C++中引用C语言中的函数和变量</span><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">，在包含C语言头文件（假设为cExample.h）时，需进行下列处理：</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　extern "C"</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　{</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　#i nclude "cExample.h"</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　}</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　而在C语言的头文件中，对其外部函数只能指定为extern类型，C语言中不支持extern "C"声明，在.c文件中包含了extern "C"时会出现编译语法错误。</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　笔者编写的C++引用C函数例子工程中包含的三个文件的</span><a target="_blank" href="http://baike.baidu.com/view/60376.htm" style="color: #136ec2; font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">源代码</a><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">如下：</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　/*&nbsp;</span><a target="_blank" href="http://baike.baidu.com/view/1219.htm" style="color: #136ec2; font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">c语言</a><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">头文件：cExample.h */</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　#ifndef C_EXAMPLE_H</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　#define C_EXAMPLE_H</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　</span><a target="_blank" href="http://baike.baidu.com/view/963932.htm" style="color: #136ec2; font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">extern</a><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">&nbsp;int add(int x,int y);</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　#endif</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　/* c语言实现文件：cExample.c */</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　#i nclude "cExample.h"</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　int add( int x, int y )</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　{</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　return x + y;</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　}</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　// c++实现文件，调用add：cppFile.cpp</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　extern "C"</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　{</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　#i nclude "cExample.h"</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　}</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　int main(int argc, char* argv[])</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　{</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　add(2,3);</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　return 0;</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　}</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　（</span><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; color: red; ">注意这里如果用GCC编译的时候</span><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">，请先使用gcc -c选项生成cExample.o，再使用g++ -o cppFile cppFile.cpp cExample.o才能生成预期的c++调用c函数的结果，否则，使用g++ -o cppFile cppFile.cpp cExample.c编译器会报错；而当cppFile.cpp 文件中不使用下列语句</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　extern "C"</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　{</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　#i nclude "cExample.h"</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　}</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　而改用</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　#i nclude "cExample.h"</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　extern "C" int add( int x, int y );</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　时</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　</span><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; color: red; ">g++ -o cppFile cppFile.cpp cExample.c的编译过程会把add函数按c++的方式解释为_foo_int_int这样的符号。</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　）</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　如果C++调用一个C语言编写的.DLL时，当包括.DLL的头文件或声明接口函数时，应加extern "C" {　}。</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　（2）</span><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; color: red; ">在C中引用C++语言中的函数和变量时</span><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">，C++的头文件需添加extern "C"，但是在C语言中不能直接引用声明了extern "C"的该头文件，应该仅将C文件中将C++中定义的</span><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; color: red; ">extern "C"函数声明为extern类型。</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　笔者编写的C引用C++函数例子工程中包含的三个文件的源代码如下：</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　//C++头文件 cppExample.h</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　#ifndef CPP_EXAMPLE_H</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　#define CPP_EXAMPLE_H</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　extern "C" int add( int x, int y );</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　#endif</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　//C++实现文件 cppExample.cpp</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　#i nclude "cppExample.h"</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　int&nbsp;</span><a title="Powered by Text-Enhance" id="_GPLITA_0" href="http://baike.baidu.com/view/2816461.htm#" in_rurl="http://www.textsrv.com/click?v=VVM6MTgxNTg6MTIxOmFkZDpmMDE5MDFlNTE4MDZkY2FiMGFmOGEwMmMzOTk4N2EyNDp6LTEwNDctMTQyMTI6YmFpa2UuYmFpZHUuY29t" style="color: #136ec2; font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">add</a><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">( int x, int y )</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　{</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　return x + y;</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　}</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　/* C实现文件 cFile.c</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　/* 这样会编译出错：#i nclude "cppExample.h" */</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　extern int add( int x, int y );</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　int main( int argc, char* argv[] )</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　{</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　add( 2, 3 );</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　</span><a title="Powered by Text-Enhance" id="_GPLITA_1" href="http://baike.baidu.com/view/2816461.htm#" in_rurl="http://www.textsrv.com/click?v=VVM6MTgxNTg6MTIxOnJldHVybjowYTY1ZTY2M2U2YWY5ZDYzZGQ1MGNiNjY5OWVmZDE1NTp6LTEwNDctMTQyMTI6YmFpa2UuYmFpZHUuY29t" in_hdr="null" style="color: #136ec2; font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">return</a><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">&nbsp;0;</span><div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; font-family: arial, 宋体, sans-serif; background-color: #ffffff; "></div><span style="font-family: arial, 宋体, sans-serif; line-height: 24px; background-color: #ffffff; ">　　}</span>&nbsp;<img src ="http://www.cppblog.com/chenglong7997/aggbug/169628.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/chenglong7997/" target="_blank">Snape</a> 2012-03-31 11:43 <a href="http://www.cppblog.com/chenglong7997/articles/169628.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C/C++. filename, line, onexit</title><link>http://www.cppblog.com/chenglong7997/articles/169589.html</link><dc:creator>Snape</dc:creator><author>Snape</author><pubDate>Fri, 30 Mar 2012 18:11:00 GMT</pubDate><guid>http://www.cppblog.com/chenglong7997/articles/169589.html</guid><wfw:comment>http://www.cppblog.com/chenglong7997/comments/169589.html</wfw:comment><comments>http://www.cppblog.com/chenglong7997/articles/169589.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/chenglong7997/comments/commentRss/169589.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/chenglong7997/services/trackbacks/169589.html</trackback:ping><description><![CDATA[<div style="padding-left: 5px; line-height: 1.5; color: #333333; font-family: Verdana, Arial, sans-serif, 'Lucida Grande'; text-align: left; background-color: #d6d3d6; "><div id="cnblogs_post_body" style="line-height: 1.8; "><p>如何判断一段程序是由C 编译程序还是由C++编译程序编译的？&nbsp;<br />答案：&nbsp;<br /></p><div id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:f85df64d-ea9e-4045-b62d-c7ed099617f2" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; display: inline; float: none; "><div style="width: 633px; "><div><div><a href="http://www.cnblogs.com/rollenholt/archive/2012/03/28/2422418.html#" style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">view plain</a><a href="http://www.cnblogs.com/rollenholt/archive/2012/03/28/2422418.html#" style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">copy to clipboard</a><a href="http://www.cnblogs.com/rollenholt/archive/2012/03/28/2422418.html#" style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">print</a><a href="http://www.cnblogs.com/rollenholt/archive/2012/03/28/2422418.html#" style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">?</a></div></div><ol start="1" style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; "><li style="font-size: 10pt; border-width: initial; border-color: initial; border-width: initial; border-color: initial; "><span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; "><span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">#ifdef&nbsp;__cplusplus</span><span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">&nbsp;&nbsp;</span></span></li><li style="font-size: 10pt; border-width: initial; border-color: initial; border-width: initial; border-color: initial; "><span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">cout&lt;&lt;<span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">"c++"</span><span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">;&nbsp;&nbsp;</span></span></li><li style="font-size: 10pt; border-width: initial; border-color: initial; border-width: initial; border-color: initial; "><span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; "><span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">#else</span><span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">&nbsp;&nbsp;</span></span></li><li style="font-size: 10pt; border-width: initial; border-color: initial; border-width: initial; border-color: initial; "><span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">cout&lt;&lt;<span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">"c"</span><span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">;&nbsp;&nbsp;</span></span></li><li style="font-size: 10pt; border-width: initial; border-color: initial; border-width: initial; border-color: initial; "><span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; "><span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">#endif</span><span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">&nbsp;&nbsp;</span></span></li></ol></div></div><p>&nbsp;</p><p>如何打印出当前源文件的文件名以及源文件的当前行号？&nbsp;<br />答案：&nbsp;<br />cout &lt;&lt; __FILE__ ;&nbsp;<br />cout&lt;&lt;__LINE__ ;&nbsp;<br />__FILE__和__LINE__是系统预定义宏，这种宏并不是在某个文件中定义的，而是由编译器定义的。</p><p>main 主函数执行完毕后，是否可能会再执行一段代码，给出说明？&nbsp;<br />答案：可以，可以用_onexit 注册一个函数，它会在main 之后执行。</p><div id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:00bbb557-72fd-4639-8059-cfc7c8ec6255" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; display: inline; float: none; "><div style="width: 633px; "><div><div><a href="http://www.cnblogs.com/rollenholt/archive/2012/03/28/2422418.html#" style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">view plain</a><a href="http://www.cnblogs.com/rollenholt/archive/2012/03/28/2422418.html#" style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">copy to clipboard</a><a href="http://www.cnblogs.com/rollenholt/archive/2012/03/28/2422418.html#" style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">print</a><a href="http://www.cnblogs.com/rollenholt/archive/2012/03/28/2422418.html#" style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">?</a></div></div><ol start="1" style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; "><li style="font-size: 10pt; border-width: initial; border-color: initial; border-width: initial; border-color: initial; "><span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; "><span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">#include&nbsp;&lt;iostream&gt;</span><span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">&nbsp;&nbsp;</span></span></li><li style="font-size: 10pt; border-width: initial; border-color: initial; border-width: initial; border-color: initial; "><span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; "><span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">using</span>&nbsp;<span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">namespace</span><span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">&nbsp;std;&nbsp;&nbsp;</span></span></li><li style="font-size: 10pt; border-width: initial; border-color: initial; border-width: initial; border-color: initial; "><span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">&nbsp;&nbsp;</span></li><li style="font-size: 10pt; border-width: initial; border-color: initial; border-width: initial; border-color: initial; "><span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; "><span style="border-width: initial; border-color: initial; color: #2e8b57; border-width: initial; border-color: initial; font-weight: bold; ">int</span><span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">&nbsp;fn1()&nbsp;&nbsp;</span></span></li><li style="font-size: 10pt; border-width: initial; border-color: initial; border-width: initial; border-color: initial; "><span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">{&nbsp;&nbsp;</span></li><li style="font-size: 10pt; border-width: initial; border-color: initial; border-width: initial; border-color: initial; "><span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">&nbsp;&nbsp;&nbsp;&nbsp;printf(&nbsp;<span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">"next.\n"</span><span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">&nbsp;);&nbsp;&nbsp;</span></span></li><li style="font-size: 10pt; border-width: initial; border-color: initial; border-width: initial; border-color: initial; "><span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">&nbsp;&nbsp;&nbsp;&nbsp;<span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">return</span><span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">&nbsp;0;&nbsp;&nbsp;</span></span></li><li style="font-size: 10pt; border-width: initial; border-color: initial; border-width: initial; border-color: initial; "><span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">}&nbsp;&nbsp;</span></li><li style="font-size: 10pt; border-width: initial; border-color: initial; border-width: initial; border-color: initial; "><span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; "><span style="border-width: initial; border-color: initial; color: #2e8b57; border-width: initial; border-color: initial; font-weight: bold; ">int</span><span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">&nbsp;fn2()&nbsp;&nbsp;</span></span></li><li style="font-size: 10pt; border-width: initial; border-color: initial; border-width: initial; border-color: initial; "><span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">{&nbsp;&nbsp;</span></li><li style="font-size: 10pt; border-width: initial; border-color: initial; border-width: initial; border-color: initial; "><span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">&nbsp;&nbsp;&nbsp;&nbsp;printf(&nbsp;<span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">"executed&nbsp;"</span><span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">&nbsp;);&nbsp;&nbsp;</span></span></li><li style="font-size: 10pt; border-width: initial; border-color: initial; border-width: initial; border-color: initial; "><span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">&nbsp;&nbsp;&nbsp;&nbsp;<span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">return</span><span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">&nbsp;0;&nbsp;&nbsp;</span></span></li><li style="font-size: 10pt; border-width: initial; border-color: initial; border-width: initial; border-color: initial; "><span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">}&nbsp;&nbsp;</span></li><li style="font-size: 10pt; border-width: initial; border-color: initial; border-width: initial; border-color: initial; "><span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; "><span style="border-width: initial; border-color: initial; color: #2e8b57; border-width: initial; border-color: initial; font-weight: bold; ">int</span><span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">&nbsp;fn3()&nbsp;&nbsp;</span></span></li><li style="font-size: 10pt; border-width: initial; border-color: initial; border-width: initial; border-color: initial; "><span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">{&nbsp;&nbsp;</span></li><li style="font-size: 10pt; border-width: initial; border-color: initial; border-width: initial; border-color: initial; "><span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">&nbsp;&nbsp;&nbsp;&nbsp;printf(&nbsp;<span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">"is&nbsp;"</span><span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">&nbsp;);&nbsp;&nbsp;</span></span></li><li style="font-size: 10pt; border-width: initial; border-color: initial; border-width: initial; border-color: initial; "><span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">&nbsp;&nbsp;&nbsp;&nbsp;<span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">return</span><span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">&nbsp;0;&nbsp;&nbsp;</span></span></li><li style="font-size: 10pt; border-width: initial; border-color: initial; border-width: initial; border-color: initial; "><span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">}&nbsp;&nbsp;</span></li><li style="font-size: 10pt; border-width: initial; border-color: initial; border-width: initial; border-color: initial; "><span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; "><span style="border-width: initial; border-color: initial; color: #2e8b57; border-width: initial; border-color: initial; font-weight: bold; ">int</span><span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">&nbsp;fn4()&nbsp;&nbsp;</span></span></li><li style="font-size: 10pt; border-width: initial; border-color: initial; border-width: initial; border-color: initial; "><span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">{&nbsp;&nbsp;</span></li><li style="font-size: 10pt; border-width: initial; border-color: initial; border-width: initial; border-color: initial; "><span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">&nbsp;&nbsp;&nbsp;&nbsp;printf(&nbsp;<span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">"This&nbsp;"</span><span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">&nbsp;);&nbsp;&nbsp;</span></span></li><li style="font-size: 10pt; border-width: initial; border-color: initial; border-width: initial; border-color: initial; "><span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">&nbsp;&nbsp;&nbsp;&nbsp;<span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">return</span><span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">&nbsp;0;&nbsp;&nbsp;</span></span></li><li style="font-size: 10pt; border-width: initial; border-color: initial; border-width: initial; border-color: initial; "><span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">}&nbsp;&nbsp;</span></li><li style="font-size: 10pt; border-width: initial; border-color: initial; border-width: initial; border-color: initial; "><span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">&nbsp;&nbsp;</span></li><li style="font-size: 10pt; border-width: initial; border-color: initial; border-width: initial; border-color: initial; "><span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; "><span style="border-width: initial; border-color: initial; color: #2e8b57; border-width: initial; border-color: initial; font-weight: bold; ">int</span><span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">&nbsp;_tmain(</span><span style="border-width: initial; border-color: initial; color: #2e8b57; border-width: initial; border-color: initial; font-weight: bold; ">int</span><span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">&nbsp;argc,&nbsp;_TCHAR*&nbsp;argv[])&nbsp;&nbsp;</span></span></li><li style="font-size: 10pt; border-width: initial; border-color: initial; border-width: initial; border-color: initial; "><span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li style="font-size: 10pt; border-width: initial; border-color: initial; border-width: initial; border-color: initial; "><span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">&nbsp;&nbsp;&nbsp;&nbsp;_onexit(&nbsp;fn1&nbsp;);&nbsp;&nbsp;</span></li><li style="font-size: 10pt; border-width: initial; border-color: initial; border-width: initial; border-color: initial; "><span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">&nbsp;&nbsp;&nbsp;&nbsp;_onexit(&nbsp;fn2&nbsp;);&nbsp;&nbsp;</span></li><li style="font-size: 10pt; border-width: initial; border-color: initial; border-width: initial; border-color: initial; "><span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">&nbsp;&nbsp;&nbsp;&nbsp;_onexit(&nbsp;fn3&nbsp;);&nbsp;&nbsp;</span></li><li style="font-size: 10pt; border-width: initial; border-color: initial; border-width: initial; border-color: initial; "><span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">&nbsp;&nbsp;&nbsp;&nbsp;_onexit(&nbsp;fn4&nbsp;);&nbsp;&nbsp;</span></li><li style="font-size: 10pt; border-width: initial; border-color: initial; border-width: initial; border-color: initial; "><span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">&nbsp;&nbsp;&nbsp;&nbsp;printf(&nbsp;<span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">"This&nbsp;is&nbsp;executed&nbsp;first.\n"</span><span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">&nbsp;);&nbsp;&nbsp;</span></span></li><li style="font-size: 10pt; border-width: initial; border-color: initial; border-width: initial; border-color: initial; "><span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">&nbsp;&nbsp;</span></li><li style="font-size: 10pt; border-width: initial; border-color: initial; border-width: initial; border-color: initial; "><span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">&nbsp;&nbsp;&nbsp;&nbsp;<span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">return</span><span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">&nbsp;0;&nbsp;&nbsp;</span></span></li><li style="font-size: 10pt; border-width: initial; border-color: initial; border-width: initial; border-color: initial; "><span style="border-width: initial; border-color: initial; border-width: initial; border-color: initial; ">}&nbsp;&nbsp;</span></li></ol></div></div>输出结果为：<a href="http://images.cnblogs.com/cnblogs_com/rollenholt/201203/201203282230081508.png" style="color: #0066ff; text-decoration: none; "><img title="image" border="0" alt="image" src="http://images.cnblogs.com/cnblogs_com/rollenholt/201203/201203282230096077.png" width="657" height="92" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; background-image: none; border-bottom-style: initial; border-bottom-color: initial; border-left-style: initial; border-left-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-style: initial; border-top-color: initial; border-right-style: initial; border-right-color: initial; padding-top: 0px; " /></a><p>The _onexit function is passed the address of a function (func) to be called when the program terminates normally. Successive calls to _onexit create a register of functions that are executed in LIFO (last-in-first-out) order.&nbsp;<strong>The functions passed to _onexit cannot take parameters.</strong></p><p>&nbsp;</p><p>类成员函数的重载、覆盖和隐藏区别？&nbsp;<br />答案：&nbsp;<br />a.成员函数被重载的特征：&nbsp;<br />（1）相同的范围（在同一个类中）；&nbsp;<br />（2）函数名字相同；&nbsp;<br />（3）参数不同；&nbsp;<br />（4）virtual 关键字可有可无。</p><p>&nbsp; （5）const的区别&nbsp;<br />b.覆盖是指派生类函数覆盖基类函数，特征是：&nbsp;<br />（1）不同的范围（分别位于派生类与基类）；&nbsp;<br />（2）函数名字相同；&nbsp;<br />（3）参数相同；&nbsp;<br />（4）基类函数必须有virtual 关键字。&nbsp;<br />c.&#8220;<strong>隐藏</strong>&#8221;是指派生类的函数屏蔽了与其同名的基类函数，规则如下：&nbsp;<br />（1）如果派生类的函数与基类的函数同名，但是参数不同。此时，不论有无virtual关键字，基类的函数将被隐藏（注意别与重载混淆）。&nbsp;<br />（2）如果派生类的函数与基类的函数同名，并且参数也相同，但是基类函数没有virtual 关键字。此时，基类的函数被隐藏（注意别与覆盖混淆）</p></div></div><img src ="http://www.cppblog.com/chenglong7997/aggbug/169589.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/chenglong7997/" target="_blank">Snape</a> 2012-03-31 02:11 <a href="http://www.cppblog.com/chenglong7997/articles/169589.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>邻接表存储图,深度和广度优先遍历</title><link>http://www.cppblog.com/chenglong7997/articles/169225.html</link><dc:creator>Snape</dc:creator><author>Snape</author><pubDate>Tue, 27 Mar 2012 20:39:00 GMT</pubDate><guid>http://www.cppblog.com/chenglong7997/articles/169225.html</guid><wfw:comment>http://www.cppblog.com/chenglong7997/comments/169225.html</wfw:comment><comments>http://www.cppblog.com/chenglong7997/articles/169225.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/chenglong7997/comments/commentRss/169225.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/chenglong7997/services/trackbacks/169225.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 就拿这个图做实验了?#include&lt;iostream&gt;#include&lt;vector&gt;#include&lt;queue&gt;&nbsp;using namespace std;&nbsp;struct ArcNode{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //弧...&nbsp;&nbsp;<a href='http://www.cppblog.com/chenglong7997/articles/169225.html'>阅读全文</a><img src ="http://www.cppblog.com/chenglong7997/aggbug/169225.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/chenglong7997/" target="_blank">Snape</a> 2012-03-28 04:39 <a href="http://www.cppblog.com/chenglong7997/articles/169225.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>最大子序列、最长公共子串、最长公共子序列[转]</title><link>http://www.cppblog.com/chenglong7997/articles/169140.html</link><dc:creator>Snape</dc:creator><author>Snape</author><pubDate>Tue, 27 Mar 2012 05:38:00 GMT</pubDate><guid>http://www.cppblog.com/chenglong7997/articles/169140.html</guid><wfw:comment>http://www.cppblog.com/chenglong7997/comments/169140.html</wfw:comment><comments>http://www.cppblog.com/chenglong7997/articles/169140.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/chenglong7997/comments/commentRss/169140.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/chenglong7997/services/trackbacks/169140.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 最大子序列最大子序列是要找出由数组成的一维数组中和最大的连续子序列。比如{5,-3,4,2}的最大子序列就是&nbsp;{5,-3,4,2}，它的和是8,达到最大；而&nbsp;{5,-6,4,2}的最大子序列是{4,2}，它的和是6。你已经看出来了，找最大子序列的方法很简单，只要前i项的和还没有小于0那么子序列就一直向后扩展，否则丢弃之前的子序列开始新的子序列，同时我们要记下各个子序列的和，最后...&nbsp;&nbsp;<a href='http://www.cppblog.com/chenglong7997/articles/169140.html'>阅读全文</a><img src ="http://www.cppblog.com/chenglong7997/aggbug/169140.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/chenglong7997/" target="_blank">Snape</a> 2012-03-27 13:38 <a href="http://www.cppblog.com/chenglong7997/articles/169140.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>用C++设计一个不能被继承的类</title><link>http://www.cppblog.com/chenglong7997/articles/168959.html</link><dc:creator>Snape</dc:creator><author>Snape</author><pubDate>Mon, 26 Mar 2012 00:33:00 GMT</pubDate><guid>http://www.cppblog.com/chenglong7997/articles/168959.html</guid><wfw:comment>http://www.cppblog.com/chenglong7997/comments/168959.html</wfw:comment><comments>http://www.cppblog.com/chenglong7997/articles/168959.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/chenglong7997/comments/commentRss/168959.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/chenglong7997/services/trackbacks/168959.html</trackback:ping><description><![CDATA[<h2><span style="background-color: #ffffff; font-family: Arial; line-height: 25px; ">题目：用C++ 设计一个不能被继承的类。</span></h2><div style="font-family: Arial; line-height: 25px; background-color: #ffffff; "><div id="cnblogs_post_body"><div style="margin-top: 0in; margin-right: 0in; margin-bottom: 10pt; margin-left: 0in; ">分析：这是Adobe 公司2007 年校园招聘的最新笔试题。这道题除了考察应聘者的C++ 基本功底外，还能考察反应能力，是一道很好的题目。</div><div style="margin-top: 0in; margin-right: 0in; margin-bottom: 10pt; margin-left: 0in; ">在Java 中定义了关键字final ，被final 修饰的类不能被继承。但在C++ 中没有final 这个关键字，要实现这个要求还是需要花费一些精力。</div><div style="margin-top: 0in; margin-right: 0in; margin-bottom: 10pt; margin-left: 0in; ">首先想到的是在C++ 中，子类的构造函数会自动调用父类的构造函数。同样，子类的析构函数也会自动调用父类的析构函数。要想一个类不能被继承，我们只要把它的构造函数和析构函数都定义为私有函数。那么当一个类试图从它那继承的时候，必然会由于试图调用构造函数、析构函数而导致编译错误。</div><div style="margin-top: 0in; margin-right: 0in; margin-bottom: 10pt; margin-left: 0in; ">可是这个类的构造函数和析构函数都是私有函数了，我们怎样才能得到该类的实例呢？这难不倒我们，我们可以通过定义静态来创建和释放类的实例。基于这个思路，我们可以写出如下的代码：</div><div style="margin-top: 0in; margin-right: 0in; margin-bottom: 0pt; margin-left: 0in; line-height: normal; "><span style="font-size: 10pt; color: green; ">///////////////////////////////////////////////////////////////////////</span></div><div style="margin-top: 0in; margin-right: 0in; margin-bottom: 0pt; margin-left: 0in; line-height: normal; "><span style="font-size: 10pt; color: green; ">// Define a class which can't be derived from</span></div><div style="margin-top: 0in; margin-right: 0in; margin-bottom: 0pt; margin-left: 0in; line-height: normal; "><span style="font-size: 10pt; color: green; ">///////////////////////////////////////////////////////////////////////</span></div><div style="margin-top: 0in; margin-right: 0in; margin-bottom: 0pt; margin-left: 0in; line-height: normal; "><span style="font-size: 10pt; color: blue; ">class</span>&nbsp;<span style="font-size: 10pt; "><span style="color: #010001; ">FinalClass1</span></span></div><div style="margin-top: 0in; margin-right: 0in; margin-bottom: 0pt; margin-left: 0in; line-height: normal; "><span style="font-size: 10pt; ">{</span></div><div style="margin-top: 0in; margin-right: 0in; margin-bottom: 0pt; margin-left: 0in; line-height: normal; "><span style="font-size: 10pt; color: blue; ">public</span><span style="font-size: 10pt; ">&nbsp;:</span></div><div style="margin-top: 0in; margin-right: 0in; margin-bottom: 0pt; margin-left: 0in; line-height: normal; "><span style="font-size: 10pt; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue; ">static</span>&nbsp;<span style="color: #010001; ">FinalClass1</span>*&nbsp;<span style="color: #010001; ">GetInstance</span>()</span></div><div style="margin-top: 0in; margin-right: 0in; margin-bottom: 0pt; margin-left: 0in; line-height: normal; "><span style="font-size: 10pt; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</span></div><div style="margin-top: 0in; margin-right: 0in; margin-bottom: 0pt; margin-left: 0in; line-height: normal; "><span style="font-size: 10pt; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue; ">return</span>&nbsp;<span style="color: blue; ">new</span>&nbsp;<span style="color: #010001; ">FinalClass1</span>;</span></div><div style="margin-top: 0in; margin-right: 0in; margin-bottom: 0pt; margin-left: 0in; line-height: normal; "><span style="font-size: 10pt; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></div><div style="margin-top: 0in; margin-right: 0in; margin-bottom: 0pt; margin-left: 0in; line-height: normal; ">&nbsp;</div><div style="margin-top: 0in; margin-right: 0in; margin-bottom: 0pt; margin-left: 0in; line-height: normal; "><span style="font-size: 10pt; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue; ">static</span>&nbsp;<span style="color: blue; ">void</span>&nbsp;<span style="color: #010001; ">DeleteInstance</span>(&nbsp;<span style="color: #010001; ">FinalClass1</span>*&nbsp;<span style="color: #010001; ">pInstance</span>)</span></div><div style="margin-top: 0in; margin-right: 0in; margin-bottom: 0pt; margin-left: 0in; line-height: normal; "><span style="font-size: 10pt; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</span></div><div style="margin-top: 0in; margin-right: 0in; margin-bottom: 0pt; margin-left: 0in; line-height: normal; "><span style="font-size: 10pt; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue; ">delete</span>&nbsp;<span style="color: #010001; ">pInstance</span>;</span></div><div style="margin-top: 0in; margin-right: 0in; margin-bottom: 0pt; margin-left: 0in; line-height: normal; "><span style="font-size: 10pt; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #010001; ">pInstance</span>&nbsp;= 0;</span></div><div style="margin-top: 0in; margin-right: 0in; margin-bottom: 0pt; margin-left: 0in; line-height: normal; "><span style="font-size: 10pt; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></div><div style="margin-top: 0in; margin-right: 0in; margin-bottom: 0pt; margin-left: 0in; line-height: normal; ">&nbsp;</div><div style="margin-top: 0in; margin-right: 0in; margin-bottom: 0pt; margin-left: 0in; line-height: normal; "><span style="font-size: 10pt; color: blue; ">private</span><span style="font-size: 10pt; ">&nbsp;:</span></div><div style="margin-top: 0in; margin-right: 0in; margin-bottom: 0pt; margin-left: 0in; line-height: normal; "><span style="font-size: 10pt; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #010001; ">FinalClass1</span>() {}</span></div><div style="margin-top: 0in; margin-right: 0in; margin-bottom: 0pt; margin-left: 0in; line-height: normal; "><span style="font-size: 10pt; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ~<span style="color: #010001; ">FinalClass1</span>() {}</span></div><div style="margin-top: 0in; margin-right: 0in; margin-bottom: 10pt; margin-left: 0in; "><span style="font-size: 10pt; line-height: 16px; ">};</span></div><div style="margin-top: 0in; margin-right: 0in; margin-bottom: 10pt; margin-left: 0in; ">这个类是不能被继承，但在总觉得它和一般的类有些不一样，使用起来也有点不方便。比如，我们只能得到位于堆上的实例，而得不到位于栈上实例。</div><div style="margin-top: 0in; margin-right: 0in; margin-bottom: 10pt; margin-left: 0in; ">能不能实现一个和一般类除了不能被继承之外其他用法都一样的类呢？办法总是有的，不过需要一些技巧。请看如下代码：</div><div style="margin-top: 0in; margin-right: 0in; margin-bottom: 0pt; margin-left: 0in; line-height: normal; "><span style="font-size: 10pt; color: green; ">///////////////////////////////////////////////////////////////////////</span></div><div style="margin-top: 0in; margin-right: 0in; margin-bottom: 0pt; margin-left: 0in; line-height: normal; "><span style="font-size: 10pt; color: green; ">// Define a class which can't be derived from</span></div><div style="margin-top: 0in; margin-right: 0in; margin-bottom: 0pt; margin-left: 0in; line-height: normal; "><span style="font-size: 10pt; color: green; ">///////////////////////////////////////////////////////////////////////</span></div><div style="margin-top: 0in; margin-right: 0in; margin-bottom: 0pt; margin-left: 0in; line-height: normal; "><span style="font-size: 10pt; color: blue; ">template</span>&nbsp;<span style="font-size: 10pt; ">&lt;<span style="color: blue; ">typename</span>&nbsp;<span style="color: #010001; ">T</span>&gt;&nbsp;<span style="color: blue; ">class</span>&nbsp;<span style="color: #010001; ">MakeFinal</span></span></div><div style="margin-top: 0in; margin-right: 0in; margin-bottom: 0pt; margin-left: 0in; line-height: normal; "><span style="font-size: 10pt; ">{</span></div><div style="margin-top: 0in; margin-right: 0in; margin-bottom: 0pt; margin-left: 0in; line-height: normal; "><span style="font-size: 10pt; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue; ">friend</span>&nbsp;<span style="color: #010001; ">T</span>;</span></div><div style="margin-top: 0in; margin-right: 0in; margin-bottom: 0pt; margin-left: 0in; line-height: normal; ">&nbsp;</div><div style="margin-top: 0in; margin-right: 0in; margin-bottom: 0pt; margin-left: 0in; line-height: normal; "><span style="font-size: 10pt; color: blue; ">private</span><span style="font-size: 10pt; ">&nbsp;:</span></div><div style="margin-top: 0in; margin-right: 0in; margin-bottom: 0pt; margin-left: 0in; line-height: normal; "><span style="font-size: 10pt; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #010001; ">MakeFinal</span>() {}</span></div><div style="margin-top: 0in; margin-right: 0in; margin-bottom: 0pt; margin-left: 0in; line-height: normal; "><span style="font-size: 10pt; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ~<span style="color: #010001; ">MakeFinal</span>() {}</span></div><div style="margin-top: 0in; margin-right: 0in; margin-bottom: 0pt; margin-left: 0in; line-height: normal; "><span style="font-size: 10pt; ">};</span></div><div style="margin-top: 0in; margin-right: 0in; margin-bottom: 0pt; margin-left: 0in; line-height: normal; ">&nbsp;</div><div style="margin-top: 0in; margin-right: 0in; margin-bottom: 0pt; margin-left: 0in; line-height: normal; "><span style="font-size: 10pt; color: blue; ">class</span>&nbsp;<span style="font-size: 10pt; "><span style="color: #010001; ">FinalClass2</span>&nbsp;:&nbsp;<span style="color: blue; ">virtual</span>&nbsp;<span style="color: blue; ">public</span>&nbsp;<span style="color: #010001; ">MakeFinal</span>&lt;<span style="color: #010001; ">FinalClass2</span>&gt;</span></div><div style="margin-top: 0in; margin-right: 0in; margin-bottom: 0pt; margin-left: 0in; line-height: normal; "><span style="font-size: 10pt; ">{</span></div><div style="margin-top: 0in; margin-right: 0in; margin-bottom: 0pt; margin-left: 0in; line-height: normal; "><span style="font-size: 10pt; color: blue; ">public</span><span style="font-size: 10pt; ">&nbsp;:</span></div><div style="margin-top: 0in; margin-right: 0in; margin-bottom: 0pt; margin-left: 0in; line-height: normal; "><span style="font-size: 10pt; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #010001; ">FinalClass2</span>() {}</span></div><div style="margin-top: 0in; margin-right: 0in; margin-bottom: 0pt; margin-left: 0in; line-height: normal; "><span style="font-size: 10pt; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ~<span style="color: #010001; ">FinalClass2</span>() {}</span></div><div style="margin-top: 0in; margin-right: 0in; margin-bottom: 10pt; margin-left: 0in; "><span style="font-size: 10pt; line-height: 16px; ">};</span></div><div style="margin-top: 0in; margin-right: 0in; margin-bottom: 10pt; margin-left: 0in; ">这个类使用起来和一般的类没有区别，可以在栈上、也可以在堆上创建实例。尽管类<span style="font-size: 10pt; color: #010001; line-height: 16px; ">&nbsp;MakeFinal</span><span style="font-size: 10pt; line-height: 16px; ">&nbsp;&lt;<span style="color: #010001; ">FinalClass2</span>&gt;</span>&nbsp;的构造函数和析构函数都是私有的，但由于类<span style="font-size: 10pt; color: #010001; line-height: 16px; ">&nbsp;FinalClass2</span>&nbsp;是它的友元函数，因此在<span style="font-size: 10pt; color: #010001; line-height: 16px; ">&nbsp;FinalClass2</span><span style="font-size: 10pt; color: #010001; line-height: 16px; ">&nbsp;中调用</span><span style="font-size: 10pt; color: #010001; line-height: 16px; ">&nbsp;MakeFinal</span><span style="font-size: 10pt; line-height: 16px; ">&nbsp;&lt;<span style="color: #010001; ">FinalClass2</span>&gt;</span>&nbsp;的构造函数和析构函数都不会造成编译错误。</div><div style="margin-top: 0in; margin-right: 0in; margin-bottom: 10pt; margin-left: 0in; ">但当我们试图从<span style="font-size: 10pt; color: #010001; line-height: 16px; ">&nbsp;FinalClass2</span>&nbsp;继承一个类并创建它的实例时，却不同通过编译。</div><div style="margin-top: 0in; margin-right: 0in; margin-bottom: 0pt; margin-left: 0in; line-height: normal; "><span style="font-size: 10pt; color: blue; ">class</span>&nbsp;<span style="font-size: 10pt; "><span style="color: #010001; ">Try</span>&nbsp;:&nbsp;<span style="color: blue; ">public</span>&nbsp;<span style="color: #010001; ">FinalClass2</span></span></div><div style="margin-top: 0in; margin-right: 0in; margin-bottom: 0pt; margin-left: 0in; line-height: normal; "><span style="font-size: 10pt; ">{</span></div><div style="margin-top: 0in; margin-right: 0in; margin-bottom: 0pt; margin-left: 0in; line-height: normal; "><span style="font-size: 10pt; color: blue; ">public</span><span style="font-size: 10pt; ">&nbsp;:</span></div><div style="margin-top: 0in; margin-right: 0in; margin-bottom: 0pt; margin-left: 0in; line-height: normal; "><span style="font-size: 10pt; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #010001; ">Try</span>() {}</span></div><div style="margin-top: 0in; margin-right: 0in; margin-bottom: 0pt; margin-left: 0in; line-height: normal; "><span style="font-size: 10pt; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ~<span style="color: #010001; ">Try</span>() {}</span></div><div style="margin-top: 0in; margin-right: 0in; margin-bottom: 0pt; margin-left: 0in; line-height: normal; "><span style="font-size: 10pt; ">};</span></div><div style="margin-top: 0in; margin-right: 0in; margin-bottom: 0pt; margin-left: 0in; line-height: normal; ">&nbsp;</div><div style="margin-top: 0in; margin-right: 0in; margin-bottom: 10pt; margin-left: 0in; "><span style="font-size: 10pt; color: #010001; line-height: 16px; ">Try</span>&nbsp;<span style="font-size: 10pt; line-height: 16px; "><span style="color: #010001; ">temp</span>;</span></div><div style="margin-top: 0in; margin-right: 0in; margin-bottom: 10pt; margin-left: 0in; ">由于类<span style="font-size: 10pt; color: #010001; line-height: 16px; ">&nbsp;FinalClass2</span>&nbsp;是从类<span style="font-size: 10pt; color: #010001; line-height: 16px; ">&nbsp;MakeFinal</span><span style="font-size: 10pt; line-height: 16px; ">&nbsp;&lt;<span style="color: #010001; ">FinalClass2</span>&gt;</span>&nbsp;虚继承过来的，在调用<span style="font-size: 10pt; color: #010001; line-height: 16px; ">&nbsp;Try</span>&nbsp;的构造函数的时候，会直接跳过<span style="font-size: 10pt; color: #010001; line-height: 16px; ">&nbsp;FinalClass2</span>&nbsp;而直接调用<span style="font-size: 10pt; color: #010001; line-height: 16px; ">&nbsp;MakeFinal</span><span style="font-size: 10pt; line-height: 16px; ">&lt;<span style="color: #010001; ">FinalClass2</span>&gt;</span>&nbsp;的构造函数。非常遗憾的是，<span style="font-size: 10pt; color: #010001; line-height: 16px; ">&nbsp;Try</span>&nbsp;不是<span style="font-size: 10pt; color: #010001; line-height: 16px; ">&nbsp;MakeFinal</span><span style="font-size: 10pt; line-height: 16px; ">&nbsp;&lt;<span style="color: #010001; ">FinalClass2</span>&gt;</span>&nbsp;的友元，因此不能调用其私有的构造函数。</div><div style="margin-top: 0in; margin-right: 0in; margin-bottom: 0pt; margin-left: 0in; line-height: normal; ">基于上面的分析，试图从<span style="font-size: 10pt; color: #010001; ">&nbsp;FinalClass2</span>&nbsp;继承的类，一旦实例化，都会导致编译错误，因此是<span style="font-size: 10pt; color: #010001; ">&nbsp;FinalClass2</span>&nbsp;不能被继承。这就满足了我们设计要求。</div></div></div><img src ="http://www.cppblog.com/chenglong7997/aggbug/168959.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/chenglong7997/" target="_blank">Snape</a> 2012-03-26 08:33 <a href="http://www.cppblog.com/chenglong7997/articles/168959.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C语言 二维数组动态分配和释放 [转]</title><link>http://www.cppblog.com/chenglong7997/articles/168955.html</link><dc:creator>Snape</dc:creator><author>Snape</author><pubDate>Sun, 25 Mar 2012 23:49:00 GMT</pubDate><guid>http://www.cppblog.com/chenglong7997/articles/168955.html</guid><wfw:comment>http://www.cppblog.com/chenglong7997/comments/168955.html</wfw:comment><comments>http://www.cppblog.com/chenglong7997/articles/168955.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/chenglong7997/comments/commentRss/168955.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/chenglong7997/services/trackbacks/168955.html</trackback:ping><description><![CDATA[<div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 5px; padding-right: 2px; padding-bottom: 5px; padding-left: 5px; line-height: 1.5; color: #393939; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: black; font-family: verdana, 'ms song', Arial, Helvetica, sans-serif; text-align: left; background-color: #faf7ef; "><div id="cnblogs_post_body" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">C 二维数组动态分配和释放</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><table style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-collapse: collapse; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #c0c0c0; border-right-color: #c0c0c0; border-bottom-color: #c0c0c0; border-left-color: #c0c0c0; "><tbody style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><tr style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><td style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 3px; padding-right: 3px; padding-bottom: 3px; padding-left: 3px; border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #c0c0c0; border-right-color: #c0c0c0; border-bottom-color: #c0c0c0; border-left-color: #c0c0c0; border-collapse: collapse; "><div id="blog_text" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">(1)已知第二维</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><fieldset style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; "><legend style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">Code-1</legend><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">char (*a)[N];<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">//指向数组的指针</span></span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">a = (char (*)[N])malloc(sizeof(char *) * m);</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">printf("%d\n", sizeof(a));<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">//4，指针</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">printf("%d\n", sizeof(a[0]));<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">//N，一维数组</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">free(a);</div></span><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div></fieldset><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">(2)已知第一维</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><fieldset style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; "><legend style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">Code-2</legend><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">char* a[M];<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">//指针的数组</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">int i;</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">for(i=0; i&lt;M; i++)</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">a[i] = (char *)malloc(sizeof(char) * n);</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">printf("%d\n", sizeof(a));<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">//4*M，指针数组</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">printf("%d\n", sizeof(a[0]));<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">//4，指针</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">for(i=0; i&lt;M; i++)</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">&nbsp;&nbsp; free(a[i]);</div></span><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div></fieldset><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">(3)已知第一维，一次分配内存(保证内存的连续性)</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><fieldset style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; "><legend style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">Code-3</legend><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">char* a[M];<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">//指针的数组</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">int i;</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">a[0] = (char *)malloc(sizeof(char) * M * n);</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">for(i=1; i&lt;M; i++)</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">a[i] = a[i-1] + n;</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">printf("%d\n", sizeof(a));<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">//4*M，指针数组</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">printf("%d\n", sizeof(a[0]));<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">//4，指针</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">free(a[0]);</div></span><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div></fieldset><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">(4)两维都未知</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><fieldset style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; "><legend style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">Code-4</legend><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">char **a;</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">int i;</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">a = (char **)malloc(sizeof(char *) * m);<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">//分配指针数组</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">for(i=0; i&lt;m; i++)</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">{</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">a[i] = (char *)malloc(sizeof(char) * n);<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">//分配每个指针所指向的数组</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">}</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">printf("%d\n", sizeof(a));<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">//4，指针</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">printf("%d\n", sizeof(a[0]));<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">//4，指针</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">for(i=0; i&lt;m; i++)</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">{</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">free(a[i]);</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">}</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">free(a);</div></span><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div></fieldset><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">(5)两维都未知，一次分配内存(保证内存的连续性)</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><fieldset style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; "><legend style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">Code-5</legend><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">char **a;</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">int i;</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">a = (char **)malloc(sizeof(char *) * m);<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">//分配指针数组</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">a[0] = (char *)malloc(sizeof(char) * m * n);<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">//一次性分配所有空间</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">for(i=1; i&lt;m; i++)</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">{</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">a[i] = a[i-1] + n;</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">}</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">printf("%d\n", sizeof(a));<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">//4，指针</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">printf("%d\n", sizeof(a[0]));<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">//4，指针</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">free(a[0]);</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">free(a);</div></span><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div></fieldset><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><strong style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">2.C++动态分配二维数组</strong></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">(1)已知第二维</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><fieldset style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; "><legend style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">Code-6</legend><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">char (*a)[N];<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">//指向数组的指针</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">a = new char[m][N];</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">printf("%d\n", sizeof(a));<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">//4，指针</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">printf("%d\n", sizeof(a[0]));<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">//N，一维数组</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">delete[] a;</div></span><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div></fieldset><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">(2)已知第一维</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><fieldset style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; "><legend style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">Code-7</legend><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">char* a[M];<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">//指针的数组</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">for(int i=0; i&lt;M; i++)</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">&nbsp;&nbsp; a[i] = new char[n];</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">printf("%d\n", sizeof(a));<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">//4*M，指针数组</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">printf("%d\n", sizeof(a[0]));<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">//4，指针</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">for(i=0; i&lt;M; i++)</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">&nbsp;&nbsp; delete[] a[i];</div></span><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div></fieldset><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">(3)已知第一维，一次分配内存(保证内存的连续性)</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><fieldset style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; "><legend style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">Code-8</legend><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">char* a[M];<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">//指针的数组</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">a[0] = new char[M*n];</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">for(int i=1; i&lt;M; i++)</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">a[i] = a[i-1] + n;</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">printf("%d\n", sizeof(a));<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">//4*M，指针数组</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">printf("%d\n", sizeof(a[0]));<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">//4，指针</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">delete[] a[0];</div></span><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div></fieldset><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">(4)两维都未知</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><fieldset style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; "><legend style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">Code-9</legend><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">char **a;</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">a = new char* [m];<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">//分配指针数组</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">for(int i=0; i&lt;m; i++)</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">{</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">a[i] = new char[n];<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">//分配每个指针所指向的数组</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">}</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">printf("%d\n", sizeof(a));<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">//4，指针</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">printf("%d\n", sizeof(a[0]));<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">//4，指针</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">for(i=0; i&lt;m; i++)</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">delete[] a[i];</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">delete[] a;</div></span><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div></fieldset><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">(5)两维都未知，一次分配内存(保证内存的连续性)</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><fieldset style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; "><legend style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">Code-10</legend><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">char **a;</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">a = new char* [m];</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">a[0] = new char[m * n];<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">//一次性分配所有空间</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">for(int i=1; i&lt;m; i++)</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">{</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">a[i] = a[i-1] + n;<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">//分配每个指针所指向的数组</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">}</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">printf("%d\n", sizeof(a));<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">//4，指针</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">printf("%d\n", sizeof(a[0]));<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">//4，指针</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">delete[] a[0];</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">delete[] a;</div></span><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div></fieldset><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">多说一句：new和delete要注意配对使用，即有多少个new就有多少个delete，这样才可以避免内存泄漏！</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><strong style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">3.静态二维数组作为函数参数传递</strong></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">如果采用上述几种方法动态分配二维数组，那么将对应的数据类型作为函数参数就可以了。这里讨论静态二维数组作为函数参数传递，即按照以下的调用方式：</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">int a[2][3];</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">func(a);</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">C语言中将静态二维数组作为参数传递比较麻烦，一般需要指明第二维的长度，如果不给定第二维长度，则只能先将其作为一维指针传递，然后利用二维数组的线性存储特性，在函数体内转化为对指定元素的访问。</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">首先写好测试代码，以验证参数传递的正确性：</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">(1)给定第二维长度</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><fieldset style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; "><legend style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">Code-11</legend><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">void func(int a[][N])</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">{</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">printf("%d\n", a[1][2]);</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">}</div></span><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div></fieldset><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">(2)不给定第二维长度</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><fieldset style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; "><legend style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">Code-12</legend><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">void func(int* a)</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">{</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">printf("%d\n", a[1 * N + 2]);<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">//计算元素位置</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">}</div></span><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div></fieldset><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">注意：使用该函数时需要将二维数组首地址强制转换为一维指针，即func((int*)a);</span></span></p></div></div></td></tr></tbody></table></div></div><img src ="http://www.cppblog.com/chenglong7997/aggbug/168955.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/chenglong7997/" target="_blank">Snape</a> 2012-03-26 07:49 <a href="http://www.cppblog.com/chenglong7997/articles/168955.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>