﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-逛奔的蜗牛-随笔分类-C/C++</title><link>http://www.cppblog.com/biao/category/9972.html</link><description> 我不聪明，但我会很努力</description><language>zh-cn</language><lastBuildDate>Sat, 03 Aug 2013 11:21:27 GMT</lastBuildDate><pubDate>Sat, 03 Aug 2013 11:21:27 GMT</pubDate><ttl>60</ttl><item><title>Qt：FontMetrics</title><link>http://www.cppblog.com/biao/archive/2013/08/03/202322.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Sat, 03 Aug 2013 10:27:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2013/08/03/202322.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/202322.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2013/08/03/202322.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/202322.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/202322.html</trackback:ping><description><![CDATA[<div>在一个Rect的正中间绘制文本: rect的中点与文本的中点重合.</div>
<div>
<div>int cx = rect.x + rect.width / 2;</div>
<div>int fx = cx - fm.width(text);</div>
</div>
<div><br />
</div>
<div>int cy = rect.y + rect.height / 2;</div>
<div>int fy = cy + fm.xHeight&nbsp;/ 2;</div>
<div><br />
</div>
<div><br />
</div>
<div><br />
</div>
<img src="http://www.cppblog.com/images/cppblog_com/biao/FontMetrics.png" width="506" height="274" alt="" />
@import url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);<img src ="http://www.cppblog.com/biao/aggbug/202322.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2013-08-03 18:27 <a href="http://www.cppblog.com/biao/archive/2013/08/03/202322.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C：函数指针数组的定义</title><link>http://www.cppblog.com/biao/archive/2013/07/26/202124.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Thu, 25 Jul 2013 23:56:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2013/07/26/202124.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/202124.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2013/07/26/202124.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/202124.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/202124.html</trackback:ping><description><![CDATA[<pre style="margin-top: 0px; margin-bottom: 0px;"><font color="#808000"><br />
</font></pre>
<pre style="margin-top: 0px; margin-bottom: 0px;"><font color="#808000">// [[1]] 使用typedef定义函数指针为一个类型，更喜欢这种方式</font></pre>
<pre style="margin-top: 0px; margin-bottom: 0px;"><!--StartFragment--><span style=" color:#808000;">typedef</span><span style=" color:#c0c0c0;"> </span><span style=" color:#808000;">int</span><span style=" color:#c0c0c0;"> </span>(*<span style=" color:#800080;">MYFUN</span>)(<span style=" color:#808000;">int</span>,<span style=" color:#c0c0c0;"> </span><span style=" color:#808000;">int</span>);</pre>
<pre style="margin-top: 0px; margin-bottom: 0px;"><span style=" color:#800080;">MYFUN</span><span style=" color:#c0c0c0;"> </span>funcs[<span style=" color:#000080;">10</span>];</pre>
<pre style="margin-top: 0px; margin-bottom: 0px;"><br />
</pre>
<pre style="margin-top: 0px; margin-bottom: 0px;"><font color="#808000">// [[2]] 使用typedef定义函数指针数组为一个类型，不是很直观</font></pre>
<pre style="margin-top: 0px; margin-bottom: 0px;"><span style=" color:#808000;">typedef</span><span style=" color:#c0c0c0;"> </span><span style=" color:#808000;">int</span><span style=" color:#c0c0c0;"> </span>(*<span style=" color:#800080;">MYFUN2</span>[<span style=" color:#000080;">10</span>])(<span style=" color:#808000;">int</span>,<span style=" color:#c0c0c0;"> </span><span style=" color:#808000;">int</span>);</pre>
<pre style="margin-top: 0px; margin-bottom: 0px;"><span style=" color:#800080;">MYFUN2</span><span style=" color:#c0c0c0;"> </span>funcs2;</pre>
<pre style="margin-top: 0px; margin-bottom: 0px;"><br />
</pre>
<pre style="margin-top: 0px; margin-bottom: 0px;"><span style=" color:#808000;">int</span><span style=" color:#c0c0c0;"> </span>main(<span style=" color:#808000;">int</span><span style=" color:#c0c0c0;"> </span>argc,<span style=" color:#c0c0c0;"> </span><span style=" color:#808000;">char</span><span style=" color:#c0c0c0;"> </span>*argv[])<span style=" color:#c0c0c0;"> </span>{</pre>
<pre style="margin-top: 0px; margin-bottom: 0px;"><span style=" color:#c0c0c0;">    </span>funcs[<span style=" color:#000080;">0</span>]<span style=" color:#c0c0c0;"> </span>=<span style=" color:#c0c0c0;"> </span>add;</pre>
<pre style="margin-top: 0px; margin-bottom: 0px;"><span style=" color:#c0c0c0;">    </span><span style=" color:#000080;">qDebug</span>()<span style=" color:#c0c0c0;"> </span>&lt;&lt;<span style=" color:#c0c0c0;"> </span>(*funcs[<span style=" color:#000080;">0</span>])(<span style=" color:#000080;">2</span>,<span style=" color:#c0c0c0;"> </span><span style=" color:#000080;">3</span>); <span style="color: #808000; font-family: verdana, 'courier new';">// 也可以用 funcs[0](2, 3).</span></pre>
<pre style="margin-top: 0px; margin-bottom: 0px;"><br />
</pre>
<pre style="margin-top: 0px; margin-bottom: 0px;"><span style=" color:#c0c0c0;">    </span>funcs2[<span style=" color:#000080;">0</span>]<span style=" color:#c0c0c0;"> </span>=<span style=" color:#c0c0c0;"> </span>add;</pre>
<pre style="margin-top: 0px; margin-bottom: 0px;"><span style=" color:#c0c0c0;">    </span><span style=" color:#000080;">qDebug</span>()<span style=" color:#c0c0c0;"> </span>&lt;&lt;<span style=" color:#c0c0c0;"> </span>(*funcs2[<span style=" color:#000080;">0</span>])(<span style=" color:#000080;">2</span>,<span style=" color:#c0c0c0;"> </span><span style=" color:#000080;">3</span>);</pre>
<pre style="margin-top: 0px; margin-bottom: 0px;"><br />
</pre>
<pre style="margin-top: 0px; margin-bottom: 0px;"><span style=" color:#c0c0c0;">    </span><span style=" color:#808000;">return</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000080;">0</span>;</pre>
<pre style="margin-top: 0px; margin-bottom: 0px;">}<!--EndFragment--></pre>
@import url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);<img src ="http://www.cppblog.com/biao/aggbug/202124.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2013-07-26 07:56 <a href="http://www.cppblog.com/biao/archive/2013/07/26/202124.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Qt：解读Q_GLOBAL_STATIC(QFontDatabasePrivate, privateDb)  </title><link>http://www.cppblog.com/biao/archive/2013/06/26/201312.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Wed, 26 Jun 2013 02:02:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2013/06/26/201312.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/201312.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2013/06/26/201312.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/201312.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/201312.html</trackback:ping><description><![CDATA[<span style="color: #333333; font-family: Arial, Helvetica, simsun, u5b8bu4f53; line-height: 24.545454025268555px; background-color: #ffffff;">From:&nbsp;</span><a href="http://zhgw01.blog.163.com/blog/static/1041481220105253491586/">http://zhgw01.blog.163.com/blog/static/1041481220105253491586/</a><br /><span style="color: #333333; font-family: Arial, Helvetica, simsun, u5b8bu4f53; line-height: 24.545454025268555px; background-color: #ffffff;">根据 src/corelib/global.h</span><br style="line-height: 24.545454025268555px; color: #333333; font-family: Arial, Helvetica, simsun, u5b8bu4f53; background-color: #ffffff;" /><table border="1" cellpadding="1" cellspacing="1" width="80%" style="line-height: 24.545454025268555px; color: #333333; font-family: Arial, Helvetica, simsun, u5b8bu4f53; font-size: 13.63636302947998px; background-color: #ffffff;"><tbody><tr><td>&nbsp;template &lt;typename T&gt;<br />class QGlobalStatic<br />{<br />public:<br />&nbsp;&nbsp;&nbsp; T *pointer;<br />&nbsp;&nbsp;&nbsp; inline QGlobalStatic(T *p) : pointer(p) { }<br />&nbsp;&nbsp;&nbsp; inline ~QGlobalStatic() { pointer = 0; }<br />};<br /><br />#define Q_GLOBAL_STATIC(TYPE, NAME)&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; \<br />&nbsp;&nbsp;&nbsp; static TYPE *NAME()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \<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;&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; \<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; static TYPE this_##NAME;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; static QGlobalStatic&lt;TYPE &gt; global_##NAME(&amp;this_##NAME); \<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return global_##NAME.pointer;&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 />&nbsp;&nbsp;&nbsp; }<br /><br /></td></tr></tbody></table><span style="color: #333333; font-family: Arial, Helvetica, simsun, u5b8bu4f53; line-height: 24.545454025268555px; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;</span><br style="line-height: 24.545454025268555px; color: #333333; font-family: Arial, Helvetica, simsun, u5b8bu4f53; background-color: #ffffff;" /><br style="line-height: 24.545454025268555px; color: #333333; font-family: Arial, Helvetica, simsun, u5b8bu4f53; background-color: #ffffff;" /><span style="color: #333333; font-family: Arial, Helvetica, simsun, u5b8bu4f53; line-height: 24.545454025268555px; background-color: #ffffff;">上面的展开成</span><br style="line-height: 24.545454025268555px; color: #333333; font-family: Arial, Helvetica, simsun, u5b8bu4f53; background-color: #ffffff;" /><table border="1" cellpadding="1" cellspacing="1" height="120" width="616" style="line-height: 24.545454025268555px; color: #333333; font-family: Arial, Helvetica, simsun, u5b8bu4f53; font-size: 13.63636302947998px; background-color: #ffffff;"><tbody><tr><td>&nbsp;static&nbsp; QFontDatabasePrivate *privateDb()<br />{<br />&nbsp;&nbsp;&nbsp; static QFontDatabasePrivate this_privateDb;&nbsp; // 注意第一次引用的时候会开始进行初始化, 以后不再生成<br />&nbsp;&nbsp; static QGLobalStatic&lt;QFontDatabasePrivate&gt; global_privateDb(&amp;this_privateDb); //对上面生成的对象的指针进行包装， 同上，只有第一次才会生成<br /><br />&nbsp; return global_privateDb;<br />}<br /></td></tr></tbody></table><p style="line-height: 24.545454025268555px; margin: 0px 0px 10px; padding: 0px; color: #333333; font-family: Arial, Helvetica, simsun, u5b8bu4f53; background-color: #ffffff;">QFontDatabase的构造函数中会调用createDatabase （里面调用了initializeDb), initializeDb里调用了privateDb(),一个这个时候是第一次调用， 会生成QFontDatabasePrivate对象</p><p style="line-height: 24.545454025268555px; margin: 0px 0px 10px; padding: 0px; color: #333333; font-family: Arial, Helvetica, simsun, u5b8bu4f53; background-color: #ffffff;">QFontDatabasePrivate中的QDataStream *stream成员才是真正处理qte的字体</p><img src ="http://www.cppblog.com/biao/aggbug/201312.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2013-06-26 10:02 <a href="http://www.cppblog.com/biao/archive/2013/06/26/201312.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C++：QT插件开发方式</title><link>http://www.cppblog.com/biao/archive/2013/06/25/201308.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Tue, 25 Jun 2013 15:26:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2013/06/25/201308.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/201308.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2013/06/25/201308.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/201308.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/201308.html</trackback:ping><description><![CDATA[From:&nbsp;http://blog.csdn.net/henreash/article/details/7264489@import url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);
<div>
<p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">创建一个QT的库项目,删除自动生成的.h和.cpp文件,添加一个接口定义.h文件和一个接口实现类(一个.h一个.cpp).代码如下:</p>
<p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">1.接口文件源码</p>
<p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">#ifndef PLUGININTERFACE_H<br />
#define PLUGININTERFACE_H</p>
<p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">#include &lt;QString&gt;</p>
<p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; "><br />
class EchoInterface<br />
{<br />
public:<br />
&nbsp;virtual ~EchoInterface() {}<br />
&nbsp;virtual QString echo(const QString &amp;message) = 0;<br />
};</p>
<p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">QT_BEGIN_NAMESPACE<br />
Q_DECLARE_INTERFACE(EchoInterface, "com.hollysys.plugin.EchoIntrface/1.0");<br />
QT_END_NAMESPACE</p>
<p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; "><br />
#endif</p>
<p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">2.接口实现类头文件</p>
<p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">#ifndef PLUGIN_H<br />
#define PLUGIN_H</p>
<p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">//#include "plugin_global.h"<br />
#include &lt;QObject&gt;<br />
#include "plugininterface.h"</p>
<p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">class Plugin : public QObject, EchoInterface<br />
{<br />
&nbsp;Q_OBJECT<br />
&nbsp;Q_INTERFACES(EchoInterface)<br />
public:<br />
&nbsp;Plugin();<br />
&nbsp;~Plugin();<br />
public:<br />
&nbsp;QString echo(const QString &amp;message);</p>
<p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; "><br />
};</p>
<p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">#endif // PLUGIN_H</p>
<p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">3.接口实现类cpp文件</p>
<p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">#include "plugin.h"<br />
#include &lt;QtGui&gt;</p>
<p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; "><br />
Plugin::Plugin()<br />
{</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; ">Plugin::~Plugin()<br />
{</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; ">QString Plugin::echo(const QString &amp;message)<br />
{<br />
&nbsp;return message;<br />
}</p>
<p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">Q_EXPORT_PLUGIN2("echoPlugin", Plugin);<br />
</p>
<p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">编译生成dll格式的插件.这里可能会遇到LNK2001错误,因为插件接口定义依赖于QT的元数据,而在代码中手动添加Q_OBJECT宏后,编译器不会自动为我们生成moc_XXXX.cpp文件,因此需要使用命令行生成moc元数据文件:cmd--&gt;cd 源码所在命令 --&gt;moc -o moc_XXXX.cpp XXXX.h.这样就可以手动创建出moc元数据文件,解决LNK2001编译错误.</p>
<p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">建立一个GUI测试项目,代码如下:</p>
<p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">#include "test.h"<br />
#include &lt;QtGui&gt;<br />
#include &lt;QMessageBox&gt;<br />
#include &lt;QDir&gt;</p>
<p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">Test::Test(QWidget *parent, Qt::WFlags flags)<br />
&nbsp;: QMainWindow(parent, flags)<br />
{<br />
&nbsp;ui.setupUi(this);<br />
&nbsp;QObject::connect(ui.pushButton, SIGNAL(clicked()), this, SLOT(ButtonClicked()));<br />
&nbsp;loadPlugin();<br />
}</p>
<p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">Test::~Test()<br />
{</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; ">bool Test::loadPlugin()<br />
{<br />
&nbsp;QDir pluginsDir(qApp-&gt;applicationDirPath());<br />
&nbsp;foreach (QString filename, pluginsDir.entryList(QDir::Files))<br />
&nbsp;{<br />
&nbsp;&nbsp;QPluginLoader pluginLoader(pluginsDir.absoluteFilePath(filename));<br />
&nbsp;&nbsp;QObject *plugin = pluginLoader.instance();<br />
&nbsp;&nbsp;if(plugin)<br />
&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;echoInterface = qobject_cast&lt;EchoInterface *&gt;(plugin);//echoInterface是成员变量<br />
&nbsp;&nbsp;&nbsp;if(echoInterface)<br />
&nbsp;&nbsp;&nbsp;&nbsp;return true;<br />
&nbsp;&nbsp;}<br />
&nbsp;}<br />
}</p>
<p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">void Test::ButtonClicked()<br />
{<br />
&nbsp;QString plugin = ui.lineEdit-&gt;text();<br />
&nbsp;QMessageBox::information(NULL, "", echoInterface-&gt;echo(plugin));<br />
}</p>
<p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">分析代码可见,这里对所有与exe文件同目录的文件尝试加载插件,如果加载成功则返回.代码实现非常简洁.在正式开发中可将已加载的插件接口存放在列表中,在需要的时候依次调用即可.</p>
<p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">另外如果想获取插件中类定义的元数据,可以使插件接口类从QObject继承,并使用Q_CLASSINFO宏添加键值对,如Q_CLASSINFO("Author**", "Henreash**").并从主程序中使用插件对象元数据获取这些键值对:</p>
<p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">&nbsp;const QMetaObject *mo = echoInterface-&gt;metaObject();<br />
&nbsp;for(int i = 0; i &lt; mo-&gt;classInfoCount(); i++)<br />
&nbsp;{<br />
&nbsp;&nbsp;qDebug() &lt;&lt; mo-&gt;classInfo(i).name() &lt;&lt; mo-&gt;classInfo(i).value();<br />
&nbsp;}</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; ">测试发现,如果在插件接口类中定义一个静态变量,那么这个静态变量在插件中的地址和在主程序中的地址是不相同的.</p>
</div><img src ="http://www.cppblog.com/biao/aggbug/201308.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2013-06-25 23:26 <a href="http://www.cppblog.com/biao/archive/2013/06/25/201308.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C++：单例 Singleton</title><link>http://www.cppblog.com/biao/archive/2013/06/25/201306.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Tue, 25 Jun 2013 13:46:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2013/06/25/201306.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/201306.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2013/06/25/201306.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/201306.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/201306.html</trackback:ping><description><![CDATA[<p style="margin: 0.4em 0px 0.5em; line-height: 22px; font-family: sans-serif; font-size: 15px; background-color: #ffffff; ">From:&nbsp;http://zh.wikipedia.org/wiki/单例模式</p>
<p style="margin: 0.4em 0px 0.5em; line-height: 22px; font-family: sans-serif; font-size: 15px; background-color: #ffffff; ">在<a href="http://zh.wikipedia.org/wiki/C%2B%2B" title="C++" style="text-decoration: none; color: #0b0080; background-image: none; background-position: initial initial; background-repeat: initial initial; ">C++</a>编程语言中，单例模式应用的例子如下述代码所示（这里仅仅提供一个示例，这个例子对多线程的情况并不是安全的）：</p>
<div dir="ltr" class="mw-geshi mw-code mw-content-ltr" style="direction: ltr; font-family: monospace, Courier; padding: 1em; border-left-color: #2f6fab; background-color: #f9f9f9; line-height: 1.1em; font-size: 15px; ">
<div class="cpp source-cpp" style="line-height: normal; font-family: monospace; ">
<pre class="de1" style="font-family: monospace, monospace; padding: 0px; border: 0px none white; line-height: 1.2em; font-size: 1em; margin-top: 0px; margin-bottom: 0px; background-image: none; vertical-align: top; "><span class="co1" style="color: #666666; ">// ...</span>
<span class="kw2" style="color: #0000ff; ">class</span> lock
<span class="br0" style="color: #008000; ">{</span>
<span class="kw2" style="color: #0000ff; ">public</span><span class="sy4" style="color: #008080; ">:</span>
lock<span class="br0" style="color: #008000; ">(</span><span class="br0" style="color: #008000; ">)</span><span class="sy4" style="color: #008080; ">;</span>
lock<span class="br0" style="color: #008000; ">(</span>lock <span class="kw4" style="color: #0000ff; ">const</span> <span class="sy3" style="color: #000040; ">&amp;</span> l<span class="br0" style="color: #008000; ">)</span><span class="sy4" style="color: #008080; ">;</span>
~lock<span class="br0" style="color: #008000; ">(</span><span class="br0" style="color: #008000; ">)</span><span class="sy4" style="color: #008080; ">;</span>
lock <span class="sy3" style="color: #000040; ">&amp;</span> operator <span class="sy1" style="color: #000080; ">=</span><span class="br0" style="color: #008000; ">(</span>lock <span class="kw4" style="color: #0000ff; ">const</span> <span class="sy3" style="color: #000040; ">&amp;</span> l<span class="br0" style="color: #008000; ">)</span><span class="sy4" style="color: #008080; ">;</span>
<span class="kw4" style="color: #0000ff; ">void</span> request<span class="br0" style="color: #008000; ">(</span><span class="br0" style="color: #008000; ">)</span><span class="sy4" style="color: #008080; ">;</span>
<span class="kw4" style="color: #0000ff; ">void</span> release<span class="br0" style="color: #008000; ">(</span><span class="br0" style="color: #008000; ">)</span><span class="sy4" style="color: #008080; ">;</span>
<span class="co1" style="color: #666666; ">// ...</span>
<span class="br0" style="color: #008000; ">}</span><span class="sy4" style="color: #008080; ">;</span>
lock<span class="sy4" style="color: #008080; ">::</span><span class="me2" style="color: #007788; ">lock</span><span class="br0" style="color: #008000; ">(</span><span class="br0" style="color: #008000; ">)</span>
<span class="br0" style="color: #008000; ">{</span>
<span class="co1" style="color: #666666; ">// ...</span>
<span class="br0" style="color: #008000; ">}</span>
<span class="co1" style="color: #666666; ">// ...</span>
lock<span class="sy4" style="color: #008080; ">::</span>~lock<span class="br0" style="color: #008000; ">(</span><span class="br0" style="color: #008000; ">)</span>
<span class="br0" style="color: #008000; ">{</span>
<span class="co1" style="color: #666666; ">// ...</span>
<span class="br0" style="color: #008000; ">}</span>
<span class="co1" style="color: #666666; ">// ...</span>
<span class="kw4" style="color: #0000ff; ">void</span> lock<span class="sy4" style="color: #008080; ">::</span><span class="me2" style="color: #007788; ">request</span><span class="br0" style="color: #008000; ">(</span><span class="br0" style="color: #008000; ">)</span>
<span class="br0" style="color: #008000; ">{</span>
<span class="co1" style="color: #666666; ">// ...</span>
<span class="br0" style="color: #008000; ">}</span>
<span class="kw4" style="color: #0000ff; ">void</span> lock<span class="sy4" style="color: #008080; ">::</span><span class="me2" style="color: #007788; ">release</span><span class="br0" style="color: #008000; ">(</span><span class="br0" style="color: #008000; ">)</span>
<span class="br0" style="color: #008000; ">{</span>
<span class="co1" style="color: #666666; ">// ...</span>
<span class="br0" style="color: #008000; ">}</span>
<span class="co1" style="color: #666666; ">// ...</span>
<span class="co1" style="color: #666666; ">// assumes _DATA_TYPE_ has a default constructor</span>
<span class="kw2" style="color: #0000ff; ">template</span><span class="sy1" style="color: #000080; ">&lt;</span><span class="kw2" style="color: #0000ff; ">typename</span> _DATA_TYPE_<span class="sy1" style="color: #000080; ">&gt;</span>
<span class="kw2" style="color: #0000ff; ">class</span> singleton
<span class="br0" style="color: #008000; ">{</span>
<span class="kw2" style="color: #0000ff; ">public</span><span class="sy4" style="color: #008080; ">:</span>
<span class="kw4" style="color: #0000ff; ">static</span> _DATA_TYPE_ <span class="sy2" style="color: #000040; ">*</span> request<span class="br0" style="color: #008000; ">(</span><span class="br0" style="color: #008000; ">)</span><span class="sy4" style="color: #008080; ">;</span>
<span class="kw4" style="color: #0000ff; ">static</span> <span class="kw4" style="color: #0000ff; ">void</span> release<span class="br0" style="color: #008000; ">(</span><span class="br0" style="color: #008000; ">)</span><span class="sy4" style="color: #008080; ">;</span>
<span class="kw2" style="color: #0000ff; ">private</span><span class="sy4" style="color: #008080; ">:</span>
singleton<span class="br0" style="color: #008000; ">(</span><span class="br0" style="color: #008000; ">)</span><span class="sy4" style="color: #008080; ">;</span>
singleton<span class="br0" style="color: #008000; ">(</span>singleton<span class="sy1" style="color: #000080; ">&lt;</span>_DATA_TYPE_<span class="sy1" style="color: #000080; ">&gt;</span> <span class="kw4" style="color: #0000ff; ">const</span> <span class="sy3" style="color: #000040; ">&amp;</span> s<span class="br0" style="color: #008000; ">)</span><span class="sy4" style="color: #008080; ">;</span>
~singleton<span class="br0" style="color: #008000; ">(</span><span class="br0" style="color: #008000; ">)</span><span class="sy4" style="color: #008080; ">;</span>
singleton<span class="sy1" style="color: #000080; ">&lt;</span>_DATA_TYPE_<span class="sy1" style="color: #000080; ">&gt;</span> <span class="sy3" style="color: #000040; ">&amp;</span> operator <span class="sy1" style="color: #000080; ">=</span><span class="br0" style="color: #008000; ">(</span>singleton<span class="sy1" style="color: #000080; ">&lt;</span>_DATA_TYPE_<span class="sy1" style="color: #000080; ">&gt;</span> <span class="kw4" style="color: #0000ff; ">const</span> <span class="sy3" style="color: #000040; ">&amp;</span> s<span class="br0" style="color: #008000; ">)</span><span class="sy4" style="color: #008080; ">;</span>
<span class="kw4" style="color: #0000ff; ">static</span> _DATA_TYPE_ <span class="sy2" style="color: #000040; ">*</span> pointer<span class="sy4" style="color: #008080; ">;</span>
<span class="kw4" style="color: #0000ff; ">static</span> lock mutex<span class="sy4" style="color: #008080; ">;</span>
<span class="co1" style="color: #666666; ">// ...</span>
<span class="br0" style="color: #008000; ">}</span><span class="sy4" style="color: #008080; ">;</span>
<span class="kw2" style="color: #0000ff; ">template</span><span class="sy1" style="color: #000080; ">&lt;</span><span class="kw2" style="color: #0000ff; ">typename</span> _DATA_TYPE_<span class="sy1" style="color: #000080; ">&gt;</span>
_DATA_TYPE_ <span class="sy2" style="color: #000040; ">*</span> singleton<span class="sy1" style="color: #000080; ">&lt;</span>_DATA_TYPE_<span class="sy1" style="color: #000080; ">&gt;</span><span class="sy4" style="color: #008080; ">::</span><span class="me2" style="color: #007788; ">pointer</span> <span class="sy1" style="color: #000080; ">=</span> <span class="nu0" style="color: #0000dd; ">0</span><span class="sy4" style="color: #008080; ">;</span>
<span class="kw2" style="color: #0000ff; ">template</span><span class="sy1" style="color: #000080; ">&lt;</span><span class="kw2" style="color: #0000ff; ">typename</span> _DATA_TYPE_<span class="sy1" style="color: #000080; ">&gt;</span>
lock singleton<span class="sy1" style="color: #000080; ">&lt;</span>_DATA_TYPE_<span class="sy1" style="color: #000080; ">&gt;</span><span class="sy4" style="color: #008080; ">::</span><span class="me2" style="color: #007788; ">mutex</span><span class="sy4" style="color: #008080; ">;</span>
<span class="kw2" style="color: #0000ff; ">template</span><span class="sy1" style="color: #000080; ">&lt;</span><span class="kw2" style="color: #0000ff; ">typename</span> _DATA_TYPE_<span class="sy1" style="color: #000080; ">&gt;</span>
_DATA_TYPE_ <span class="sy2" style="color: #000040; ">*</span> singleton<span class="sy1" style="color: #000080; ">&lt;</span>_DATA_TYPE_<span class="sy1" style="color: #000080; ">&gt;</span><span class="sy4" style="color: #008080; ">::</span><span class="me2" style="color: #007788; ">request</span><span class="br0" style="color: #008000; ">(</span><span class="br0" style="color: #008000; ">)</span>
<span class="br0" style="color: #008000; ">{</span>
<span class="kw1" style="color: #0000ff; ">if</span><span class="br0" style="color: #008000; ">(</span>singleton<span class="sy1" style="color: #000080; ">&lt;</span>_DATA_TYPE_<span class="sy1" style="color: #000080; ">&gt;</span><span class="sy4" style="color: #008080; ">::</span><span class="me2" style="color: #007788; ">pointer</span> <span class="sy1" style="color: #000080; ">==</span> <span class="nu0" style="color: #0000dd; ">0</span><span class="br0" style="color: #008000; ">)</span>
<span class="br0" style="color: #008000; ">{</span>
singleton<span class="sy1" style="color: #000080; ">&lt;</span>_DATA_TYPE_<span class="sy1" style="color: #000080; ">&gt;</span><span class="sy4" style="color: #008080; ">::</span><span class="me2" style="color: #007788; ">mutex</span>.<span class="me1" style="color: #007788; ">request</span><span class="br0" style="color: #008000; ">(</span><span class="br0" style="color: #008000; ">)</span><span class="sy4" style="color: #008080; ">;</span>
<span class="kw1" style="color: #0000ff; ">if</span><span class="br0" style="color: #008000; ">(</span>singleton<span class="sy1" style="color: #000080; ">&lt;</span>_DATA_TYPE_<span class="sy1" style="color: #000080; ">&gt;</span><span class="sy4" style="color: #008080; ">::</span><span class="me2" style="color: #007788; ">pointer</span> <span class="sy1" style="color: #000080; ">==</span> <span class="nu0" style="color: #0000dd; ">0</span><span class="br0" style="color: #008000; ">)</span>
<span class="br0" style="color: #008000; ">{</span>
singleton<span class="sy1" style="color: #000080; ">&lt;</span>_DATA_TYPE_<span class="sy1" style="color: #000080; ">&gt;</span><span class="sy4" style="color: #008080; ">::</span><span class="me2" style="color: #007788; ">pointer</span> <span class="sy1" style="color: #000080; ">=</span> <span class="kw3" style="color: #0000dd; ">new</span> _DATA_TYPE_<span class="sy4" style="color: #008080; ">;</span>
<span class="br0" style="color: #008000; ">}</span>
singleton<span class="sy1" style="color: #000080; ">&lt;</span>_DATA_TYPE_<span class="sy1" style="color: #000080; ">&gt;</span><span class="sy4" style="color: #008080; ">::</span><span class="me2" style="color: #007788; ">mutex</span>.<span class="me1" style="color: #007788; ">release</span><span class="br0" style="color: #008000; ">(</span><span class="br0" style="color: #008000; ">)</span><span class="sy4" style="color: #008080; ">;</span>
<span class="br0" style="color: #008000; ">}</span>
<span class="kw1" style="color: #0000ff; ">return</span> singleton<span class="sy1" style="color: #000080; ">&lt;</span>_DATA_TYPE_<span class="sy1" style="color: #000080; ">&gt;</span><span class="sy4" style="color: #008080; ">::</span><span class="me2" style="color: #007788; ">pointer</span><span class="sy4" style="color: #008080; ">;</span>
<span class="br0" style="color: #008000; ">}</span>
<span class="kw2" style="color: #0000ff; ">template</span><span class="sy1" style="color: #000080; ">&lt;</span><span class="kw2" style="color: #0000ff; ">typename</span> _DATA_TYPE_<span class="sy1" style="color: #000080; ">&gt;</span>
<span class="kw4" style="color: #0000ff; ">void</span> singleton<span class="sy1" style="color: #000080; ">&lt;</span>_DATA_TYPE_<span class="sy1" style="color: #000080; ">&gt;</span><span class="sy4" style="color: #008080; ">::</span><span class="me2" style="color: #007788; ">release</span><span class="br0" style="color: #008000; ">(</span><span class="br0" style="color: #008000; ">)</span>
<span class="br0" style="color: #008000; ">{</span>
<span class="kw1" style="color: #0000ff; ">if</span><span class="br0" style="color: #008000; ">(</span>singleton<span class="sy1" style="color: #000080; ">&lt;</span>_DATA_TYPE_<span class="sy1" style="color: #000080; ">&gt;</span><span class="sy4" style="color: #008080; ">::</span><span class="me2" style="color: #007788; ">pointer</span> <span class="sy3" style="color: #000040; ">!</span><span class="sy1" style="color: #000080; ">=</span> <span class="nu0" style="color: #0000dd; ">0</span><span class="br0" style="color: #008000; ">)</span>
<span class="br0" style="color: #008000; ">{</span>
singleton<span class="sy1" style="color: #000080; ">&lt;</span>_DATA_TYPE_<span class="sy1" style="color: #000080; ">&gt;</span><span class="sy4" style="color: #008080; ">::</span><span class="me2" style="color: #007788; ">mutex</span>.<span class="me1" style="color: #007788; ">request</span><span class="br0" style="color: #008000; ">(</span><span class="br0" style="color: #008000; ">)</span><span class="sy4" style="color: #008080; ">;</span>
<span class="kw1" style="color: #0000ff; ">if</span><span class="br0" style="color: #008000; ">(</span>singleton<span class="sy1" style="color: #000080; ">&lt;</span>_DATA_TYPE_<span class="sy1" style="color: #000080; ">&gt;</span><span class="sy4" style="color: #008080; ">::</span><span class="me2" style="color: #007788; ">pointer</span> <span class="sy3" style="color: #000040; ">!</span><span class="sy1" style="color: #000080; ">=</span> <span class="nu0" style="color: #0000dd; ">0</span><span class="br0" style="color: #008000; ">)</span>
<span class="br0" style="color: #008000; ">{</span>
<span class="kw3" style="color: #0000dd; ">delete</span> singleton<span class="sy1" style="color: #000080; ">&lt;</span>_DATA_TYPE_<span class="sy1" style="color: #000080; ">&gt;</span><span class="sy4" style="color: #008080; ">::</span><span class="me2" style="color: #007788; ">pointer</span><span class="sy4" style="color: #008080; ">;</span>
singleton<span class="sy1" style="color: #000080; ">&lt;</span>_DATA_TYPE_<span class="sy1" style="color: #000080; ">&gt;</span><span class="sy4" style="color: #008080; ">::</span><span class="me2" style="color: #007788; ">pointer</span> <span class="sy1" style="color: #000080; ">=</span> <span class="nu0" style="color: #0000dd; ">0</span><span class="sy4" style="color: #008080; ">;</span>
<span class="br0" style="color: #008000; ">}</span>
singleton<span class="sy1" style="color: #000080; ">&lt;</span>_DATA_TYPE_<span class="sy1" style="color: #000080; ">&gt;</span><span class="sy4" style="color: #008080; ">::</span><span class="me2" style="color: #007788; ">mutex</span>.<span class="me1" style="color: #007788; ">release</span><span class="br0" style="color: #008000; ">(</span><span class="br0" style="color: #008000; ">)</span><span class="sy4" style="color: #008080; ">;</span>
<span class="br0" style="color: #008000; ">}</span>
<span class="br0" style="color: #008000; ">}</span>
<span class="kw2" style="color: #0000ff; ">template</span><span class="sy1" style="color: #000080; ">&lt;</span><span class="kw2" style="color: #0000ff; ">typename</span> _DATA_TYPE_<span class="sy1" style="color: #000080; ">&gt;</span>
singleton<span class="sy1" style="color: #000080; ">&lt;</span>_DATA_TYPE_<span class="sy1" style="color: #000080; ">&gt;</span><span class="sy4" style="color: #008080; ">::</span><span class="me2" style="color: #007788; ">singleton</span><span class="br0" style="color: #008000; ">(</span><span class="br0" style="color: #008000; ">)</span>
<span class="br0" style="color: #008000; ">{</span>
<span class="co1" style="color: #666666; ">// ...</span>
<span class="br0" style="color: #008000; ">}</span>
<span class="co1" style="color: #666666; ">// ...</span>
<span class="kw4" style="color: #0000ff; ">int</span> main<span class="br0" style="color: #008000; ">(</span><span class="br0" style="color: #008000; ">)</span>
<span class="br0" style="color: #008000; ">{</span>
<span class="kw4" style="color: #0000ff; ">int</span> <span class="sy2" style="color: #000040; ">*</span> s<span class="sy4" style="color: #008080; ">;</span>
s <span class="sy1" style="color: #000080; ">=</span> singleton<span class="sy1" style="color: #000080; ">&lt;</span><span class="kw4" style="color: #0000ff; ">int</span><span class="sy1" style="color: #000080; ">&gt;</span><span class="sy4" style="color: #008080; ">::</span><span class="me2" style="color: #007788; ">request</span><span class="br0" style="color: #008000; ">(</span><span class="br0" style="color: #008000; ">)</span><span class="sy4" style="color: #008080; ">;</span>
<span class="co1" style="color: #666666; ">// ...</span>
singleton<span class="sy1" style="color: #000080; ">&lt;</span><span class="kw4" style="color: #0000ff; ">int</span><span class="sy1" style="color: #000080; ">&gt;</span><span class="sy4" style="color: #008080; ">::</span><span class="me2" style="color: #007788; ">release</span><span class="br0" style="color: #008000; ">(</span><span class="br0" style="color: #008000; ">)</span><span class="sy4" style="color: #008080; ">;</span>
<span class="kw1" style="color: #0000ff; ">return</span> <span class="nu0" style="color: #0000dd; ">0</span><span class="sy4" style="color: #008080; ">;</span>
<span class="br0" style="color: #008000; ">}</span>
</pre>
<div><span class="br0" style="color: #008000; "><br />
</span></div>
</div>
</div>
@import url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);<img src ="http://www.cppblog.com/biao/aggbug/201306.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2013-06-25 21:46 <a href="http://www.cppblog.com/biao/archive/2013/06/25/201306.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>OpenGL：展示OpenGL特性的软件Tutors</title><link>http://www.cppblog.com/biao/archive/2013/04/19/199557.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Thu, 18 Apr 2013 16:24:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2013/04/19/199557.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/199557.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2013/04/19/199557.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/199557.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/199557.html</trackback:ping><description><![CDATA[<table width="600" border="0" cellspacing="0" cellpadding="0" style="font-family: 'arial narrow', helvetica; background-color: #ffffff; ">
    <tbody>
        <tr>
            <td><img src="http://user.xmission.com/~nate/pics/naterobins.jpg" alt="nate robins" width="600" height="80" border="0" vspace="0" hspace="0" /></td>
        </tr>
    </tbody>
</table>
<table width="600" border="0" cellspacing="0" cellpadding="0" style="font-family: 'arial narrow', helvetica; background-color: #ffffff; ">
    <tbody>
        <tr>
            <td><img src="http://user.xmission.com/~nate/pics/menu-left.jpg" alt="" width="20" height="30" border="0" vspace="0" hspace="0" /></td>
            <td><a href="http://user.xmission.com/~nate/index.html"><img src="http://user.xmission.com/~nate/pics/main-dim.jpg" alt="main" width="80" height="30" border="0" vspace="0" hspace="0" /></a></td>
            <td><a href="http://user.xmission.com/~nate/opengl.html"><img src="http://user.xmission.com/~nate/pics/opengl-lit.jpg" alt="OPENGL" width="145" height="30" border="0" vspace="0" hspace="0" /></a></td>
            <td><a href="http://user.xmission.com/~nate/chronicles.html"><img src="http://user.xmission.com/~nate/pics/chronicles-dim.jpg" alt="chronicles" width="215" height="30" border="0" vspace="0" hspace="0" /></a></td>
            <td><a href="http://user.xmission.com/~nate/allies.html"><img src="http://user.xmission.com/~nate/pics/allies-dim.jpg" alt="allies" width="120" height="30" border="0" vspace="0" hspace="0" /></a></td>
            <td><img src="http://user.xmission.com/~nate/pics/menu-right.jpg" alt="" width="20" height="30" border="0" vspace="0" hspace="0" /></td>
        </tr>
    </tbody>
</table>
<table width="600" border="0" cellspacing="0" cellpadding="0" style="font-family: 'arial narrow', helvetica; background-color: #ffffff; ">
    <tbody>
        <tr>
            <td><img src="http://user.xmission.com/~nate/pics/up-left.jpg" alt="---" width="300" height="50" border="0" vspace="0" hspace="0" /></td>
            <td align="center" valign="bottom" width="200"><em>Tutors</em></td>
            <td><img src="http://user.xmission.com/~nate/pics/up-right.jpg" alt="---" width="100" height="50" border="0" vspace="0" hspace="0" /></td>
        </tr>
    </tbody>
</table>
<br style="font-family: 'arial narrow', helvetica; font-size: medium; line-height: normal; text-align: " />
<table width="550" border="0" cellpadding="0" cellspacing="20" style="font-family: 'arial narrow', helvetica; background-color: #ffffff; ">
    <tbody>
        <tr>
            <td align="center"><a href="http://user.xmission.com/~nate/tutors/tutors-src.zip">Tutors source code package (tutors-src.zip 920KB)</a>&nbsp;<br />
            <br />
            <a href="http://user.xmission.com/~nate/tutors/tutors-win32.zip">Source package + Windows executables (tutors-win32.zip 1.24MB)</a>&nbsp;<br />
            <br />
            <a href="http://user.xmission.com/~nate/tutors/tutors-macosx.zip">Source package + Mac OS X universal binaries (tutors-macosx.zip 1.2MB)</a>&nbsp;<br />
            <br />
            <a href="http://user.xmission.com/~nate/tutors-contrib.html">Contributions</a>&nbsp;from various people (Unix makefiles, etc).&nbsp;<br />
            <br />
            </td>
        </tr>
        <tr>
            <td>
            <table align="right" border="0" cellspacing="0" cellpadding="0">
                <tbody>
                    <tr>
                        <td><a href="http://user.xmission.com/~nate/tutors/fog.png"><img src="http://user.xmission.com/~nate/tutors/fog-.png" alt="fog" width="225" height="225" border="1" vspace="0" hspace="5" align="right" /></a></td>
                    </tr>
                    <tr>
                        <td><center><font size="1">(click on image to enlarge it)</font></center></td>
                    </tr>
                </tbody>
            </table>
            The fog tutorial program (shown at right) demonstrates how to use depth cueing (or fog) in OpenGL. It includes command panels that allow dynamic update of the parameters for linear, exponential and exponential<sup>2</sup>&nbsp;fog functions. The fog color can also be dynamically changed.</td>
        </tr>
        <tr>
            <td>
            <table align="left" border="0" cellspacing="0" cellpadding="0">
                <tbody>
                    <tr>
                        <td><a href="http://user.xmission.com/~nate/tutors/lightposition.png"><img src="http://user.xmission.com/~nate/tutors/lightposition-.png" alt="lightposition" width="225" height="225" border="1" vspace="0" hspace="5" /></a></td>
                    </tr>
                    <tr>
                        <td><center><font size="1">(click on image to enlarge it)</font></center></td>
                    </tr>
                </tbody>
            </table>
            The lightposition tutorial program (shown at left) demonstrates how lighting position is effected by the viewing transformation. The order in which the light position and the viewing transform are established is configurable, as are the parameters for the position and transform.</td>
        </tr>
        <tr>
            <td>
            <table align="center" border="0" cellspacing="0" cellpadding="0">
                <tbody>
                    <tr>
                        <td><a href="http://user.xmission.com/~nate/tutors/lightmaterial.png"><img src="http://user.xmission.com/~nate/tutors/lightmaterial-.png" alt="lightmaterial" width="475" height="317" border="1" vspace="0" hspace="5" /></a></td>
                    </tr>
                    <tr>
                        <td><center><font size="1">(click on image to enlarge it)</font></center></td>
                    </tr>
                </tbody>
            </table>
            <br />
            The lightmaterial tutorial program (shown above) demonstrates how OpenGL lighting and material properties interact and effect each other. All material &amp; light properties are tweakable. A second command panel allows spotlight parameters &amp; attenuation factors to be changed.</td>
        </tr>
        <tr>
            <td>
            <table align="right" border="0" cellspacing="0" cellpadding="0">
                <tbody>
                    <tr>
                        <td><a href="http://user.xmission.com/~nate/tutors/projection.png"><img src="http://user.xmission.com/~nate/tutors/projection-.png" alt="projection" width="225" height="225" border="1" vspace="0" hspace="5" /></a></td>
                    </tr>
                    <tr>
                        <td><center><font size="1">(click on image to enlarge it)</font></center></td>
                    </tr>
                </tbody>
            </table>
            The projection tutorial program (shown at right) demonstrates how the many types of projection transformations work. A command panel is included for gluPerspective(), glOrtho() and glFrustum(). In addition, the viewing transform can be modified via the gluLookat() function.</td>
        </tr>
        <tr>
            <td>
            <table align="left" border="0" cellspacing="0" cellpadding="0">
                <tbody>
                    <tr>
                        <td><a href="http://user.xmission.com/~nate/tutors/transformation.png"><img src="http://user.xmission.com/~nate/tutors/transformation-.png" alt="transformation" width="225" height="225" border="1" vspace="0" hspace="5" align="left" /></a></td>
                    </tr>
                    <tr>
                        <td><center><font size="1">(click on image to enlarge it)</font></center></td>
                    </tr>
                </tbody>
            </table>
            The transformation tutorial program (shown at left) demonstrates how the basic transformations of rotate, translate and scale operate in OpenGL. The order of the transforms can be changed to see how that effects rendering.</td>
        </tr>
        <tr>
            <td>
            <table align="center" border="0" cellspacing="0" cellpadding="0">
                <tbody>
                    <tr>
                        <td><a href="http://user.xmission.com/~nate/tutors/texture.png"><img src="http://user.xmission.com/~nate/tutors/texture-.png" alt="texture" width="475" height="317" border="1" vspace="0" hspace="5" /></a></td>
                    </tr>
                    <tr>
                        <td><center><font size="1">(click on image to enlarge it)</font></center></td>
                    </tr>
                </tbody>
            </table>
            <br />
            The texture tutorial program (shown above) demonstrates how OpenGL texturing works. Specifically, the texture coordinates for a polygon are provided for manipulation as are the polygons coordinates themselves. The texture parameter &amp; environment attributes are tweakable. A separate command panel allows manipulation of the texture matrix.</td>
        </tr>
        <tr>
            <td>
            <table align="right" border="0" cellspacing="0" cellpadding="0">
                <tbody>
                    <tr>
                        <td><a href="http://user.xmission.com/~nate/tutors/shapes.png"><img src="http://user.xmission.com/~nate/tutors/shapes-.png" alt="shapes" width="263" height="225" border="1" vspace="0" hspace="5" align="right" /></a></td>
                    </tr>
                    <tr>
                        <td><center><font size="1">(click on image to enlarge it)</font></center></td>
                    </tr>
                </tbody>
            </table>
            The shapes tutorial program (shown at right) demonstrates how the many types of primitives work. A command panel for each primitive type is included. This tutorial was written by Mason Woo (<a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#109;&#97;&#115;&#111;&#110;&#64;&#119;&#111;&#111;&#46;&#99;&#111;&#109;">mason@woo.com</a>).</td>
        </tr>
    </tbody>
</table>
<br style="font-family: 'arial narrow', helvetica; font-size: medium; line-height: normal; text-align: " />
<br style="font-family: 'arial narrow', helvetica; font-size: medium; line-height: normal; text-align: " />
<table width="600" border="0" cellspacing="0" cellpadding="0" style="font-family: 'arial narrow', helvetica; background-color: #ffffff; ">
    <tbody>
        <tr>
            <td align="center"><a href="http://www.digits.com/web_counter"><img src="http://counter.digits.com/wc/-d/8/-r/-z/-c/26/-f/aaaaaa/-b/ffffff/opengl_tutors" border="0"  alt="" /></a></td>
        </tr>
    </tbody>
</table>
<br style="font-family: 'arial narrow', helvetica; font-size: medium; line-height: normal; text-align: " />
<table width="600" border="0" cellspacing="0" cellpadding="0" style="font-family: 'arial narrow', helvetica; background-color: #ffffff; ">
    <tbody>
        <tr>
            <td><img src="http://user.xmission.com/~nate/pics/dn-left.jpg" alt="---" width="100" height="50" border="0" vspace="0" hspace="0" /></td>
            <td><img src="http://user.xmission.com/~nate/pics/dn-middle.jpg" alt="---" width="200" height="50" border="0" vspace="0" hspace="0" /></td>
            <td><img src="http://user.xmission.com/~nate/pics/dn-right.jpg" alt="---" width="300" height="50" border="0" vspace="0" hspace="0" /><br />
            <br />
            </td>
        </tr>
    </tbody>
</table>
@import url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);<img src ="http://www.cppblog.com/biao/aggbug/199557.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2013-04-19 00:24 <a href="http://www.cppblog.com/biao/archive/2013/04/19/199557.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C++：LIB和DLL的区别与使用</title><link>http://www.cppblog.com/biao/archive/2013/03/14/198416.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Thu, 14 Mar 2013 06:38:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2013/03/14/198416.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/198416.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2013/03/14/198416.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/198416.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/198416.html</trackback:ping><description><![CDATA[<p style="margin: 0px 0px 14px; color: #303030; font-family: Verdana, Helvetica, Arial; font-size: 12px; line-height: 18px; text-align: justify; background-color: #ffffff;"><span style="font-family: Courier;">共有两种库：</span><br /><span style="font-family: Courier; color: #ff0000;">一种是LIB包含了函数所在的DLL文件和文件中函数位置的信息（入口），代码由运行时加载在进程空间中的DLL提供，称为动态链接库dynamic link library。</span><br /><span style="font-family: Courier; color: #ff0000;">一种是LIB包含函数代码本身，在编译时直接将代码加入程序当中，称为静态链接库static link library。</span><br /><span style="font-family: Courier;">共有两种链接方式：</span><br /><span style="font-family: Courier;">动态链接使用动态链接库，允许可执行模块（.dll文件或.exe文件）仅包含在运行时定位DLL函数的可执行代码所需的信息。</span><br /><span style="font-family: Courier;">静态链接使用静态链接库，链接器从静态链接库LIB获取所有被引用函数，并将库同代码一起放到可执行文件中。</span><br /><br /><span style="font-family: Courier;">关于lib和dll的区别如下：</span><br /><span style="font-family: Courier;">（1）lib是编译时用到的，dll是运行时用到的。如果要完成源代码的编译，只需要lib；如果要使动态链接的程序运行起来，只需要dll。</span><br /><span style="font-family: Courier;">（2）如果有dll文件，那么lib一般是一些索引信息，记录了dll中函数的入口和位置，dll中是函数的具体内容；如果只有lib文件，那么这个lib文件是静态编译出来的，索引和实现都在其中。使用静态编译的lib文件，在运行程序时不需要再挂动态库，缺点是导致应用程序比较大，而且失去了动态库的灵活性，发布新版本时要发布新的应用程序才行。</span><br /><span style="font-family: Courier;">（3）动态链接的情况下，有两个文件：一个是LIB文件，一个是DLL文件。LIB包含被DLL导出的函数名称和位置，DLL包含实际的函数和数据，应用程序使用LIB文件链接到DLL文件。在应用程序的可执行文件中，存放的不是被调用的函数代码，而是DLL中相应函数代码的地址，从而节省了内存资源。DLL和LIB文件必须随应用程序一起发行，否则应用程序会产生错误。如果不想用lib文件或者没有lib文件，可以用WIN32 API函数LoadLibrary、GetProcAddress装载。</span><br /><br /><span style="font-family: Courier;">使用lib需注意两个文件：</span><br /><span style="font-family: Courier;">（1）.h头文件，包含lib中说明输出的类或符号原型或数据结构。应用程序调用lib时，需要将该文件包含入应用程序的源文件中。</span><br /><span style="font-family: Courier;">（2）.LIB文件，略。</span><br /><br /><span style="font-family: Courier;">使用dll需注意三个文件：</span><br /><span style="font-family: Courier;">（1）.h头文件，包含dll中说明输出的类或符号原型或数据结构的.h文件。应用程序调用dll时，需要将该文件包含入应用程序的源文件中。</span><br /><span style="font-family: Courier;">（2）.LIB文件，是dll在编译、链接成功之后生成的文件，作用是当其他应用程序调用dll时，需要将该文件引入应用程序，否则产生错误。</span><span style="color: red; font-family: Courier;">如果不想用lib文件或者没有lib文件，可以用WIN32 API函数LoadLibrary、GetProcAddress装载。<br /></span><span style="font-family: Courier;">（3）.dll文件，真正的可执行文件，开发成功后的应用程序在发布时，只需要有.exe文件和.dll文件，并不需要.lib文件和.h头文件。</span><br /><br /><span style="font-family: Courier;">使用lib的方法：</span><br /><span style="font-family: Courier;">静态lib中，一个lib文件实际上是任意个obj文件的集合，obj文件是cpp文件编译生成的。在编译这种静态库工程时，根本不会遇到链接错误；即使有错，也只会在使用这个lib的EXT文件或者DLL工程里暴露出来。</span><br /><span style="font-family: Courier;">在VC中新建一个static library类型的工程Lib，加入test.cpp文件和test.h文件（头文件内包括函数声明），然后编译，就生成了Lib.lib文件。</span><br /><span style="font-family: Courier;">别的工程要使用这个lib有两种方式：</span><br /><span style="font-family: Courier;">（1）在project-&gt;link-&gt;Object/Library Module中加入Lib.lib文件（先查询工程目录，再查询系统Lib目录）；或者在源代码中加入指令#pragma comment(lib, &#8220;Lib.lib&#8221;)。</span><br /><span style="font-family: Courier;">（2）将Lib.lib拷入工程所在目录，或者执行文件生成的目录，或者系统Lib目录中。</span><br /><span style="font-family: Courier;">（3）加入相应的头文件test.h。</span><br /><br /><span style="font-family: Courier;">使用DLL的方法：</span><br /><span style="font-family: Courier;">使用动态链接中的lib，不是obj文件的集合，即里面不会有实际的实现，它只是提供动态链接到DLL所需要的信息，这种lib可以在编译一个DLL工程时由编译器生成。</span><br /><span style="font-family: Courier;">创建DLL工程的方法（略）。</span><br /><span style="font-family: Courier; color: #ff0000;"><strong>（1）隐式链接</strong></span><br /><span style="font-family: Courier;">第一种方法是：通过project-&gt;link-&gt;Object/Library Module中加入.lib文件（或者在源代码中加入指令#pragma comment(lib, &#8220;Lib.lib&#8221;)），并将.dll文件置入工程所在目录，然后添加对应的.h头文件。</span></p><div style="font-size: 13px; color: #303030; font-family: Verdana, Helvetica, Arial; line-height: 18px; text-align: justify; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; width: 1061.3333740234375px; word-break: break-all;"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" style="border: 0px;" /><span style="color: #000000; font-family: Courier;">#include&nbsp;</span><span style="color: #000000; font-family: Courier;">"</span><span style="color: #000000; font-family: Courier;">stdafx.h</span><span style="color: #000000; font-family: Courier;">"</span><span style="color: #000000;"><br /><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" style="border: 0px;" /></span><span style="color: #000000; font-family: Courier;">#include&nbsp;</span><span style="color: #000000; font-family: Courier;">"</span><span style="color: #000000; font-family: Courier;">DLLSample.h</span><span style="color: #000000; font-family: Courier;">"</span><span style="color: #000000;"><br /><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" style="border: 0px;" /><br /><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" style="border: 0px;" /></span><span style="color: #000000; font-family: Courier;">#pragma&nbsp;comment(lib,&nbsp;</span><span style="color: #000000; font-family: Courier;">"</span><span style="color: #000000; font-family: Courier;">DLLSample.lib</span><span style="color: #000000; font-family: Courier;">"</span><span style="color: #000000; font-family: Courier;">)&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000; font-family: Courier;">//</span><span style="color: #008000; font-family: Courier;">你也可以在项目属性中设置库的链接</span><span style="color: #008000;"><br /><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" style="border: 0px;" /></span><span style="color: #000000;"><br /><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" style="border: 0px;" /></span><span style="color: #0000ff; font-family: Courier;">int</span><span style="color: #000000; font-family: Courier;">&nbsp;main()<br /><img id="Codehighlighter1_116_187_Open_Image" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" alt="" style="border: 0px;" /></span><span id="Codehighlighter1_116_187_Open_Text"><span style="color: #000000; font-family: Courier;">{<br /><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" alt="" style="border: 0px;" /></span><span style="color: #000000; font-family: Courier;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TestDLL(</span><span style="color: #000000; font-family: Courier;">123</span><span style="color: #000000; font-family: Courier;">);&nbsp;&nbsp;&nbsp;</span><span style="color: #008000; font-family: Courier;">//</span><span style="color: #008000; font-family: Courier;">dll中的函数，在DllSample.h中声明</span><span style="color: #008000;"><br /><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" alt="" style="border: 0px;" /></span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff; font-family: Courier;">return</span><span style="color: #000000; font-family: Courier;">(</span><span style="color: #000000; font-family: Courier;">1</span><span style="color: #000000; font-family: Courier;">);<br /><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" alt="" style="border: 0px;" /></span><span style="color: #000000; font-family: Courier;">}</span></span><span style="color: #000000;"><br /><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" style="border: 0px;" /></span></div><p style="margin: 0px 0px 14px; color: #303030; font-family: Verdana, Helvetica, Arial; font-size: 12px; line-height: 18px; text-align: justify; background-color: #ffffff;"><span style="font-family: Courier; color: #ff0000;"><strong>（2）显式链接</strong></span><br /><span style="font-family: Courier;">需要函数指针和WIN32 API函数LoadLibrary、GetProcAddress装载，使用这种载入方法，</span><span style="color: red; font-family: Courier;">不需要.lib文件和.h头文件，只需要.dll文件即可</span><span style="font-family: Courier;">（将.dll文件置入工程目录中）。</span></p><div style="font-size: 13px; color: #303030; font-family: Verdana, Helvetica, Arial; line-height: 18px; text-align: justify; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; width: 1061.3333740234375px; word-break: break-all;"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" style="border: 0px;" /><span style="color: #000000; font-family: Courier;">#include&nbsp;</span><span style="color: #000000; font-family: Courier;">&lt;</span><span style="color: #000000; font-family: Courier;">iostream</span><span style="color: #000000; font-family: Courier;">&gt;</span><span style="color: #000000;"><br /><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" style="border: 0px;" /></span><span style="color: #000000; font-family: Courier;">#include&nbsp;</span><span style="color: #000000; font-family: Courier;">&lt;</span><span style="color: #000000; font-family: Courier;">windows.h</span><span style="color: #000000; font-family: Courier;">&gt;</span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000; font-family: Courier;">//</span><span style="color: #008000; font-family: Courier;">使用函数和某些特殊变量</span><span style="color: #008000;"><br /><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" style="border: 0px;" /></span><span style="color: #000000; font-family: Courier;">typedef&nbsp;</span><span style="color: #0000ff; font-family: Courier;">void</span><span style="color: #000000; font-family: Courier;">&nbsp;(</span><span style="color: #000000; font-family: Courier;">*</span><span style="color: #000000; font-family: Courier;">DLLFunc)(</span><span style="color: #0000ff; font-family: Courier;">int</span><span style="color: #000000; font-family: Courier;">);<br /><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" style="border: 0px;" /></span><span style="color: #0000ff; font-family: Courier;">int</span><span style="color: #000000; font-family: Courier;">&nbsp;main()<br /><img id="Codehighlighter1_104_540_Open_Image" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" alt="" style="border: 0px;" /></span><span id="Codehighlighter1_104_540_Open_Text"><span style="color: #000000; font-family: Courier;">{<br /><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" alt="" style="border: 0px;" /></span><span style="color: #000000; font-family: Courier;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DLLFunc&nbsp;dllFunc;<br /><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" alt="" style="border: 0px;" /></span><span style="color: #000000; font-family: Courier;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HINSTANCE&nbsp;hInstLibrary&nbsp;</span><span style="color: #000000; font-family: Courier;">=</span><span style="color: #000000; font-family: Courier;">&nbsp;LoadLibrary(</span><span style="color: #000000; font-family: Courier;">"</span><span style="color: #000000; font-family: Courier;">DLLSample.dll</span><span style="color: #000000; font-family: Courier;">"</span><span style="color: #000000; font-family: Courier;">);<br /><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" alt="" style="border: 0px;" /><br /><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" alt="" style="border: 0px;" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff; font-family: Courier;">if</span><span style="color: #000000; font-family: Courier;">&nbsp;(hInstLibrary&nbsp;</span><span style="color: #000000; font-family: Courier;">==</span><span style="color: #000000; font-family: Courier;">&nbsp;NULL)<br /><img id="Codehighlighter1_237_284_Open_Image" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" alt="" style="border: 0px;" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span id="Codehighlighter1_237_284_Open_Text"><span style="color: #000000; font-family: Courier;">{<br /><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" alt="" style="border: 0px;" /></span><span style="color: #000000; font-family: Courier;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FreeLibrary(hInstLibrary);<br /><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" alt="" style="border: 0px;" /></span><span style="color: #000000; font-family: Courier;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000;"><br /><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" alt="" style="border: 0px;" /></span><span style="color: #000000; font-family: Courier;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dllFunc&nbsp;</span><span style="color: #000000; font-family: Courier;">=</span><span style="color: #000000; font-family: Courier;">&nbsp;(DLLFunc)GetProcAddress(hInstLibrary,&nbsp;</span><span style="color: #000000; font-family: Courier;">"</span><span style="color: #000000; font-family: Courier;">TestDLL</span><span style="color: #000000; font-family: Courier;">"</span><span style="color: #000000; font-family: Courier;">);<br /><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" alt="" style="border: 0px;" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff; font-family: Courier;">if</span><span style="color: #000000; font-family: Courier;">&nbsp;(dllFunc&nbsp;</span><span style="color: #000000; font-family: Courier;">==</span><span style="color: #000000; font-family: Courier;">&nbsp;NULL)<br /><img id="Codehighlighter1_391_438_Open_Image" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" alt="" style="border: 0px;" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span id="Codehighlighter1_391_438_Open_Text"><span style="color: #000000; font-family: Courier;">{<br /><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" alt="" style="border: 0px;" /></span><span style="color: #000000; font-family: Courier;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FreeLibrary(hInstLibrary);<br /><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" alt="" style="border: 0px;" /></span><span style="color: #000000; font-family: Courier;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000;"><br /><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" alt="" style="border: 0px;" /></span><span style="color: #000000; font-family: Courier;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dllFunc(</span><span style="color: #000000; font-family: Courier;">123</span><span style="color: #000000; font-family: Courier;">);<br /><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" alt="" style="border: 0px;" /></span><span style="color: #000000; font-family: Courier;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;std::cin.</span><span style="color: #0000ff; font-family: Courier;">get</span><span style="color: #000000; font-family: Courier;">();<br /><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" alt="" style="border: 0px;" /></span><span style="color: #000000; font-family: Courier;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FreeLibrary(hInstLibrary);<br /><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" alt="" style="border: 0px;" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff; font-family: Courier;">return</span><span style="color: #000000; font-family: Courier;">(</span><span style="color: #000000; font-family: Courier;">1</span><span style="color: #000000; font-family: Courier;">);<br /><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" alt="" style="border: 0px;" /></span><span style="color: #000000; font-family: Courier;">}</span></span><span style="color: #000000;"><br /><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" style="border: 0px;" /><br /><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" style="border: 0px;" /></span></div><p style="margin: 0px 0px 14px; color: #303030; font-family: Verdana, Helvetica, Arial; font-size: 12px; line-height: 18px; text-align: justify; background-color: #ffffff;"><span style="font-family: Courier;">LoadLibrary函数利用一个名称作为参数，获得DLL的实例（HINSTANCE类型是实例的句柄），通常调用该函数后需要查看一下函数返回是否成功，如果不成功则返回NULL（句柄无效），此时调用函数FreeLibrary释放DLL获得的内存。</span><br /><span style="font-family: Courier;">GetProcAddress函数利用DLL的句柄和函数的名称作为参数，返回相应的函数指针，同时必须使用强转；判断函数指针是否为NULL，如果是则调用函数FreeLibrary释放DLL获得的内存。此后，可以使用函数指针来调用实际的函数。</span><br /><span style="font-family: Courier;">最后要记得使用FreeLibrary函数释放内存。</span><br /><br /><span style="color: red; font-family: Courier;">注意：应用程序如何找到DLL文件？</span><br /><span style="font-family: Courier;">使用LoadLibrary显式链接，那么在函数的参数中可以指定DLL文件的完整路径；如果不指定路径，或者进行隐式链接，Windows将遵循下面的搜索顺序来定位DLL：</span><br /><span style="font-family: Courier;">（1）包含EXE文件的目录</span><br /><span style="font-family: Courier;">（2）工程目录</span><br /><span style="font-family: Courier;">（3）Windows系统目录</span><br /><span style="font-family: Courier;">（4）Windows目录</span><br /><span style="font-family: Courier;">（5）列在Path环境变量中的一系列目录</span><br /><br /><span style="font-family: Courier;">From:&nbsp;</span><a href="http://www.cppblog.com/ming81/archive/2013/03/04/198215.html"><span style="font-family: Courier;">http://www.cppblog.com/ming81/archive/2013/03/04/198215.html</span></a><br /><br /></p><p style="margin: 10px 0px; color: #000000; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13.63636302947998px; line-height: 20px;"><span style="color: red; font-family: Courier;">.h头文件是编译时必须的，lib是链接时需要的，dll是运行时需要的。</span></p><p style="margin: 10px 0px; color: #000000; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13.63636302947998px; line-height: 20px;"><span style="color: red; font-family: Courier;">附加依赖项的是.lib不是.dll，若生成了DLL,则肯定也生成 LIB文件。如果要完成源代码的编译和链接，有头文件和lib就够了。如果也使动态连接的程序运行起来，有dll就够了。在开发和调试阶段，当然最好都有。</span></p><p style="margin: 10px 0px; color: #000000; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13.63636302947998px; line-height: 20px;"><span style="font-family: Courier;">.h .lib .dll三者的关系是：</span></p><p style="margin: 10px 0px; color: #000000; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13.63636302947998px; line-height: 20px;"><span style="font-family: Courier;">H文件作用是:声明函数接口&nbsp;</span></p><p style="margin: 10px 0px; color: #000000; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13.63636302947998px; line-height: 20px;"><span style="font-family: Courier;">DLL文件作用是: 函数可执行代码&nbsp;</span></p><p style="margin: 10px 0px; color: #000000; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13.63636302947998px; line-height: 20px;"><span style="font-family: Courier;">当我们在自己的程序中引用了一个H文件里的函数,编链器怎么知道该调用哪个DLL文件呢?这就是LIB文件的作用: 告诉链接器 调用的函数在哪个DLL中，函数执行代码在DLL中的什么位置 ，这也就是为什么需要附加依赖项 .LIB文件，它起到桥梁的作用。如果生成静态库文件，则没有DLL ，只有lib，这时函数可执行代码部分也在lib文件中</span></p><p style="margin: 10px 0px; color: #000000; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13.63636302947998px; line-height: 20px;"><span style="font-family: Courier;">目前以lib后缀的库有两种，一种为静态链接库(Static Libary，以下简称&#8220;静态库&#8221;)，另一种为动态连接库(DLL，以下简称&#8220;动态库&#8221;)的导入库(Import Libary，以下简称&#8220;导入库&#8221;）。静态库是一个或者多个obj文件的打包 ，所以有人干脆把从obj文件生成lib的过程称为Archive，即合并到一起。比如你链接一个静态库，如果其中有错，它会准确的找到是哪个obj有错，即静态lib只是壳子。动态库一般会有对应的导入库，方便程序静态载入动态链接库 ，否则你可能就需要自己LoadLibary调入DLL文件，然后再手工GetProcAddress获得对应函数了。有了导入库，你只需要链接导入库后按照头文件函数接口的声明调用函数就可以了。导入库和静态库的区别很大，他们实质是不一样的东西。静态库本身就包含了实际执行代码、符号表等等，而对于导入库而言，其实际的执行代码位于动态库中，导入库只包含了地址符号表等，确保程序找到对应函数的一些基本地址信息。</span></p><p style="margin: 10px 0px; color: #000000; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13.63636302947998px; line-height: 20px;"><span style="font-family: Courier;">一般的动态库程序有lib文件和dll文件。lib文件是必须在编译期就连接到应用程序中的，而dll文件是运行期才会被调用的。 如果有dll文件，那么对应的lib文件一般是一些索引信息，具体的实现在dll文件中。如果只有lib文件，那么这个lib文件是静态编译出来的，索引和实现都在其中。静态编译的lib文件有好处：给用户安装时就不需要再挂动态库了。但也有缺点，就是导致应用程序比较大，而且失去了动态库的灵活性，在版本升级时，同时要发布新的应用程序才行。在动态库的情况下，有两个文件，而一个是引入库（.LIB）文件，一个是DLL文件，引入库文件包含被DLL导出的函数的名称和位置，DLL包含实际的函数和数据，应用程序使用LIB文件链接到所需要使用的DLL文件，库中的函数和数据并不复制到可执行文件中，因此在应用程序的可执行文件中，存放的不是被调用的函数代码，而是DLL中所要调用的函数的内存地址，这样当一个或多个应用程序运行是再把程序代码和被调用的函数代码链接起来，从而节省了内存资源。从上面的说明可以看出，DLL和.LIB文件必须随应用程序一起发行，否则应用程序将会产生错误。</span></p><p style="margin: 10px 0px; color: #000000; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13.63636302947998px; line-height: 20px;"><span style="font-family: Courier;">静态库和共享库都是一个obj文件的集合 ，但静态链接后，执行程序中存在自己所需obj的一份拷贝，而动态链接后，执行程序仅仅是包含对共享库的一个引用。共享库相当于一个由多个obj文件组合而成的obj文件，在链接后其所有代码被加载，不管需要的还是不需要的。</span><br /><span style="font-family: Courier;">似乎可以得出一个结论：</span><br /><span style="font-family: Courier;">&nbsp; 静态链接后的程序比动态链接的所用存储空间大，因为执行程序中包含了库中代码拷贝；</span><br /><span style="font-family: Courier;">&nbsp; 而动态链接的程序比静态链接的所用的运行空间大，因为它将不需要的代码也加载到运行空间。</span></p><p style="margin: 10px 0px; color: #000000; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13.63636302947998px; line-height: 20px;"><br /><span style="color: red; font-family: Courier;">针对上面的知识2 个问题：<br /><br /></span><span style="color: red; font-family: Courier;">1） DLL和.LIB文件必须随应用程序一起发行，否则应用程序将会产生错误。&nbsp;<br /></span><span style="color: red; font-family: Courier;">我的答案：lib应该不需要吧。<br /><br /></span><span style="color: red; font-family: Courier;">2）如果是某个程序中调用了一个动态库（通过header文件，lib+dll来调用），则对动态库的某个函数的内容修改了，但接口不改，则调用此动态库的程序需重新编译连接吗？如果是通过loadlibary动态加载，需要重新编译连接吗？<br /></span></p><div style="text-align: left;"><span style="color: #c0c0c0; font-family: Courier;">我的答案：通过header+lib+dll调用的话需要重新编译连接，但是通过loadlibrary来使用的话，不需要重新编译连接。</span></div><span style="color: #ff0000; line-height: normal; text-align: start; font-family: Courier;">第2个答案错了 应该是不需要重新编译 接口不变的话 .lib都不用更新<br />From:&nbsp;</span><a href="http://www.cppblog.com/mzty/archive/2010/11/28/134901.html">http://www.cppblog.com/mzty/archive/2010/11/28/134901.html<br /><br /><span style="color: #000000; font-family: 宋体; line-height: 25px; background-color: #ffffff;">&nbsp; &nbsp; 1） 节省内存。同一个软件模块，若是以源代码的形式重用，则会被编译到不同的可执行程序中，同时运行这些exe时这些模块的二进制码会被重复加载到内存中。如 果使用dll,则只在内存中加载一次，所有使用该dll的进程会共享此块内存（当然，像dll中的全局变量这种东西是会被每个进程复制一份的）。</span><br style="margin: 0px; padding: 0px; word-wrap: break-word; color: #000000; font-family: 宋体; line-height: 25px; background-color: #ffffff;" /><span style="color: #000000; font-family: 宋体; line-height: 25px; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp;</span><br style="margin: 0px; padding: 0px; word-wrap: break-word; color: #000000; font-family: 宋体; line-height: 25px; background-color: #ffffff;" /><span style="color: #000000; font-family: 宋体; line-height: 25px; background-color: #ffffff;">&nbsp;&nbsp;&nbsp; 2） 不需编译的软件系统升级，若一个软件系统使用了dll,则该dll被改变（函数名不变）时，系统升级只需要更换此dll即可，不需要重新编译整个系统。事实上，很多软件都是以这种方式升级的。例如我们经常玩的星际、魔兽等游戏也是这样进行版本升级的。</span><br style="margin: 0px; padding: 0px; word-wrap: break-word; color: #000000; font-family: 宋体; line-height: 25px; background-color: #ffffff;" /><span style="color: #000000; font-family: 宋体; line-height: 25px; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp;</span><br style="margin: 0px; padding: 0px; word-wrap: break-word; color: #000000; font-family: 宋体; line-height: 25px; background-color: #ffffff;" /><span style="color: #000000; font-family: 宋体; line-height: 25px; background-color: #ffffff;">&nbsp;&nbsp;&nbsp; 3） Dll库可以供多种编程语言使用，例如用c编写的dll可以在vb中调用。这一点上DLL还做得很不够，因此在dll的基础上发明了COM技术，更好的解决了一系列问题。</span><br /></a><p>&nbsp;</p><p>&nbsp;</p><img src ="http://www.cppblog.com/biao/aggbug/198416.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2013-03-14 14:38 <a href="http://www.cppblog.com/biao/archive/2013/03/14/198416.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C＋＋：C++ 的指针和句柄</title><link>http://www.cppblog.com/biao/archive/2012/08/07/186558.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Tue, 07 Aug 2012 04:44:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2012/08/07/186558.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/186558.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2012/08/07/186558.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/186558.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/186558.html</trackback:ping><description><![CDATA[<div style="margin: 0px; line-height: 1.5; font-family: 宋体, Arial; font-size: 12px; background-color: #f5f7f8; ">1、句柄，是整个windows编程的基础。一个句柄是指使用唯一的整数值，即一个四字节长的数值，来标志应用程序中的不同对象和同类对象中的不同实例。应用程序通过句柄访问相应对象的信息，但是据并不是指针，不能直接通过它来阅读文件中的信息。</div>
<div style="margin: 0px; line-height: 1.5; font-family: 宋体, Arial; font-size: 12px; background-color: #f5f7f8; ">&nbsp;2、对于c++中的句柄和指针容易混淆。</div>
<div style="margin: 0px; line-height: 1.5; font-family: 宋体, Arial; font-size: 12px; background-color: #f5f7f8; ">&nbsp; &nbsp; 指针：他是内存的编号，通过它我们可以直接对内存进行操作，只要地址不变，我们操作的物理地址业界不变。----与句柄的重大区别。</div>
<div style="margin: 0px; line-height: 1.5; font-family: 宋体, Arial; font-size: 12px; background-color: #f5f7f8; ">&nbsp; &nbsp;句柄：一般是指向系统资源的位置，也可以说是地址，但是这些地址一直保持不变。其实，对于windows 系统来说支持的是虚拟内存的技术，统一时间内可能有些资源被换出，有些资源被换进来。因此同一个资源在不同的时刻物理位置是不同的。window就是通过句柄来处理同一资源物理位置不断变化的问题。在window中固定的位置存储了一张表：记录了所有资源的实时地址，句柄其实没有直接指向资源的物理地址，而是指向对应表中的某一项，这样无论资源的物理地址怎么换，通过句柄都可以实时的找到他的实时位置。</div>
<div style="margin: 0px; line-height: 1.5; font-family: 宋体, Arial; font-size: 12px; background-color: #f5f7f8; "><br />
</div>
<div style="margin: 0px; line-height: 1.5; font-family: 宋体, Arial; font-size: 12px; background-color: #f5f7f8; ">From:&nbsp;http://blog.chinaunix.net/uid-26874138-id-3302108.html</div>
@import url(http://www.cppblog.com/cutesoft_client/cuteeditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);<img src ="http://www.cppblog.com/biao/aggbug/186558.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2012-08-07 12:44 <a href="http://www.cppblog.com/biao/archive/2012/08/07/186558.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>其他编程：版本控制入门简介</title><link>http://www.cppblog.com/biao/archive/2012/04/27/172924.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Fri, 27 Apr 2012 07:07:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2012/04/27/172924.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/172924.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2012/04/27/172924.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/172924.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/172924.html</trackback:ping><description><![CDATA[<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana; text-align: left; background-color: #e5ecf4; ">版本控制已经出现有些年头了。然而，我还是会被人问起一些，诸如版本控制是什么或者它是如何工作的，这样基础的问题。本文会概括地解释版本控制解决的重要问题，本文使用的场景针对的是源代码版本控制。</p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana; text-align: left; background-color: #e5ecf4; "><br 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; " />目前有很多不同类型的版本控制系统（Version Control System， VCS）。一些VCS，比如<a href="http://subversion.apache.org/" 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: #003366; text-decoration: none; ">Subversion</a>和<a href="http://savannah.nongnu.org/projects/cvs" 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: #003366; text-decoration: none; ">CVS</a>，以中央仓库（repository）为中心进行架构。此外，还有分布式的VCS（Distributed VCS，DVCS），&nbsp;<a href="http://git-scm.com/" 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: #003366; text-decoration: none; ">Git</a>&nbsp;和&nbsp;<a href="http://mercurial.selenic.com/" 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: #003366; text-decoration: none; ">Mercurial</a>&nbsp;是两个新近出现的DVCS。然而，在上述两种类型的环境中，通常会有一个&#8220;指定的&#8221;中央仓库。对应地，比如一个Subversion服务器或者一个<a href="https://github.com/" 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: #003366; text-decoration: none; ">GitHub</a>仓库。下面会基于这个场景进行图示说明。那么让我们开始吧。<br 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; " /><br 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; " />在开发者拷贝到本机之前，服务器需要创建一个仓库。创建初始仓库会由于产品不同而有所差别。从现在起，你所要知道的就是，在服务器上有一个初始空间。我把这个版本称作版本&#8220;A&#8221;。<br 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; " /><a href="http://img.cnbeta.com/newsimg/120427/08104301990317868.png" 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: #003366; text-decoration: none; "><img title="VCS1" src="http://img.cnbeta.com/newsimg/120427/08104301990317868.png" altwidth="237" style="margin-top: 5px !important; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-style: initial; border-color: initial; border-image: initial; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-width: initial; clear: both; display: block; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; border-top-left-radius: 6px; border-top-right-radius: 6px; border-bottom-right-radius: 6px; border-bottom-left-radius: 6px; border-top-color: #d4d4d4; border-right-color: #d4d4d4; border-bottom-color: #d4d4d4; border-left-color: #d4d4d4; "  alt="" /></a><br 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; " />现在，每个开发者（开发者1和开发者2）都会拷贝版本&#8220;A&#8221;到他们本地电脑。再一次地，从服务器拷贝的过程会由于产品不同采用的技术会有所差别。<br 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; " /><a href="http://img.cnbeta.com/newsimg/120427/0810451132929552.png" 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: #003366; text-decoration: none; "><img title="VCS2" src="http://img.cnbeta.com/newsimg/120427/0810451132929552.png" altwidth="243" style="margin-top: 5px !important; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-style: initial; border-color: initial; border-image: initial; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-width: initial; clear: both; display: block; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; border-top-left-radius: 6px; border-top-right-radius: 6px; border-bottom-right-radius: 6px; border-bottom-left-radius: 6px; border-top-color: #d4d4d4; border-right-color: #d4d4d4; border-bottom-color: #d4d4d4; border-left-color: #d4d4d4; "  alt="" /></a><br 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; " />每个开发者会在他们的本地拷贝上进行开发。他们的本地拷贝基于版本&#8220;A&#8221;。然而，由于他们<em 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; ">应该不会</em>做同样的开发，因而他们的版本会有所差别。因此，会有2个以上的版本会同时被创建，比如版本&#8220;B&#8221;和版本&#8220;C&#8221;。<br 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; " /><br 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; " /><a href="http://img.cnbeta.com/newsimg/120427/08104621156274916.png" 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: #003366; text-decoration: none; "><img title="VCS3" src="http://img.cnbeta.com/newsimg/120427/08104621156274916.png" altwidth="317" style="margin-top: 5px !important; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-style: initial; border-color: initial; border-image: initial; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-width: initial; clear: both; display: block; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; border-top-left-radius: 6px; border-top-right-radius: 6px; border-bottom-right-radius: 6px; border-bottom-left-radius: 6px; border-top-color: #d4d4d4; border-right-color: #d4d4d4; border-bottom-color: #d4d4d4; border-left-color: #d4d4d4; "  alt="" /></a><br 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; " />开发者1首先完成了她的工作并提交到服务器。服务器上的当前版本被更新成版本&#8220;B&#8221;。<br 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; " /><br 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; " /><a href="http://img.cnbeta.com/newsimg/120427/08104831137474324.png" 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: #003366; text-decoration: none; "><img title="VCS4" src="http://img.cnbeta.com/newsimg/120427/08104831137474324.png" altwidth="317" style="margin-top: 5px !important; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-style: initial; border-color: initial; border-image: initial; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-width: initial; clear: both; display: block; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; border-top-left-radius: 6px; border-top-right-radius: 6px; border-bottom-right-radius: 6px; border-bottom-left-radius: 6px; border-top-color: #d4d4d4; border-right-color: #d4d4d4; border-bottom-color: #d4d4d4; border-left-color: #d4d4d4; "  alt="" /></a><br 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现在完成了他的工作并试图提交到服务器。然而，这是服务器告知他基于开发的版本已经发生改变。<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; ">这也是为什么采取版本控制的首要原因之一。</strong>这个特性是对网络共享代码然后由开发者手动更新的一个跨越式发展，这确保了之前的编辑没有被新的修改覆盖。<br 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; " /><br 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; " /><a href="http://img.cnbeta.com/newsimg/120427/08104941810307228.png" 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: #003366; text-decoration: none; "><img title="VCS5" src="http://img.cnbeta.com/newsimg/120427/08104941810307228.png" altwidth="396" style="margin-top: 5px !important; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-style: initial; border-color: initial; border-image: initial; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-width: initial; clear: both; display: block; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; border-top-left-radius: 6px; border-top-right-radius: 6px; border-bottom-right-radius: 6px; border-bottom-left-radius: 6px; border-top-color: #d4d4d4; border-right-color: #d4d4d4; border-bottom-color: #d4d4d4; border-left-color: #d4d4d4; "  alt="" /></a><br 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必须首先获得所有版本&#8220;B&#8221;的变化，并合并到他的修改中，然后才可以提交到服务器。这个过程听起来有些复杂。然而，大多数现代的版本控制系统十分高级，能够自动在开发者的本地拷贝上完成合并。有几种情况会产生冲突（例如：开发者1和开发者2同时修改了同一个文件的同一行）。这就是一些VCS产品比其他更高级的地方。不论如何完成合并，现在开发者2在他们的本地系统上同时混合了版本B和版本C。<br 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; " /><br 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; " /><a href="http://img.cnbeta.com/newsimg/120427/0810515916028618.png" 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: #003366; text-decoration: none; "><img title="VCS6" src="http://img.cnbeta.com/newsimg/120427/0810515916028618.png" altwidth="459" style="margin-top: 5px !important; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-style: initial; border-color: initial; border-image: initial; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-width: initial; clear: both; display: block; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; border-top-left-radius: 6px; border-top-right-radius: 6px; border-bottom-right-radius: 6px; border-bottom-left-radius: 6px; border-top-color: #d4d4d4; border-right-color: #d4d4d4; border-bottom-color: #d4d4d4; border-left-color: #d4d4d4; "  alt="" /></a><br 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可以提交他的版本到服务器。<br 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; " /><a href="http://img.cnbeta.com/newsimg/120427/08105262025047221.png" 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: #003366; text-decoration: none; "><img title="VCS7" src="http://img.cnbeta.com/newsimg/120427/08105262025047221.png" altwidth="410" style="margin-top: 5px !important; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-style: initial; border-color: initial; border-image: initial; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-width: initial; clear: both; display: block; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; border-top-left-radius: 6px; border-top-right-radius: 6px; border-bottom-right-radius: 6px; border-bottom-left-radius: 6px; border-top-color: #d4d4d4; border-right-color: #d4d4d4; border-bottom-color: #d4d4d4; border-left-color: #d4d4d4; "  alt="" /></a><br 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; " />这是一个版本控制的基础。通过注意观察图中服务器的连线可以发现版本控制的原理。服务器记录了所有先前的版本包括发生的变化，什么时候发生以及由谁进行修改。当需要进行代码回溯或者引入其他bug时，这个记录能够解除困境。<br 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; " />我希望本文能够为版本控制系统提供一个基础的介绍。如果你有任何疑问，请就你问题发表评论。<br 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;<br 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; " />英文原文：<a href="http://www.greenmoonsoftware.com/2012/04/04/an-introduction-to-version-control/" 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: #003366; text-decoration: none; ">greenmoonsoftware&nbsp;</a>编译：<a href="http://www.jobbole.com/" 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: #003366; text-decoration: none; ">伯乐</a>在线 &#8211;&nbsp;<a href="http://blog.jobbole.com/11590/" 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: #003366; text-decoration: none; ">唐尤华</a></p><img src ="http://www.cppblog.com/biao/aggbug/172924.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2012-04-27 15:07 <a href="http://www.cppblog.com/biao/archive/2012/04/27/172924.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C++：位操作基础篇之位操作全面总结</title><link>http://www.cppblog.com/biao/archive/2012/03/20/168357.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Mon, 19 Mar 2012 23:01:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2012/03/20/168357.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/168357.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2012/03/20/168357.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/168357.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/168357.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: Title:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 位操作基础篇之位操作全面总结Author:&nbsp;&nbsp;&nbsp;&nbsp; MoreWindowsE-mail:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;morewindows@126.comKeyWord:&nbsp;&nbsp; C/C++ 位操作 位...&nbsp;&nbsp;<a href='http://www.cppblog.com/biao/archive/2012/03/20/168357.html'>阅读全文</a><img src ="http://www.cppblog.com/biao/aggbug/168357.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2012-03-20 07:01 <a href="http://www.cppblog.com/biao/archive/2012/03/20/168357.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C++：二维数组和二级指针的传递问题</title><link>http://www.cppblog.com/biao/archive/2012/01/12/164030.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Thu, 12 Jan 2012 01:30:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2012/01/12/164030.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/164030.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2012/01/12/164030.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/164030.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/164030.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 再次看这篇文章，感觉说的好多都是废话，在文章最前面补充一句话：&#8220;[]的优先级高于*&#8221;，大家可以带着这句话看下面的~~~========================再一次的见证了自己的基础不牢靠。。。幸好发现得早，看见网上说，华为的一个面试题就考了这个方面的。借那道华为的面试题引出问题，题目：char **p, a[16][8];&nbsp; 问：p=...&nbsp;&nbsp;<a href='http://www.cppblog.com/biao/archive/2012/01/12/164030.html'>阅读全文</a><img src ="http://www.cppblog.com/biao/aggbug/164030.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2012-01-12 09:30 <a href="http://www.cppblog.com/biao/archive/2012/01/12/164030.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C++：浮点数的存储方式</title><link>http://www.cppblog.com/biao/archive/2011/12/18/162330.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Sun, 18 Dec 2011 03:20:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2011/12/18/162330.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/162330.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2011/12/18/162330.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/162330.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/162330.html</trackback:ping><description><![CDATA[<span  style="font-family: simsun; line-height: 23px; ">float内存存储结构(共4字节，32位分别如下)：<br />
31 30 29----23 22----0 &nbsp;&nbsp;<br />
<br />
实数符号位 指数符号位 指数位 有效数位<br />
其中实数符号位0表示正，1表示负；指数符号位1表示正，0表示负<br />
<br />
float在计算机中的存储计算方法：<br />
1. 先将这个实数的绝对值化为二进制格式，方法是：实数的整数部分是除2取余和小数部分是乘2取整<br />
2. 将这个二进制格式实数的小数点左移或右移n位，直到小数点移动到第一个有效数字的右边。<br />
3. 从小数点右边第一位开始数出二十三位数字放入第22到第0位。<br />
4. 如果实数是正的，则在第31位放入&#8220;0&#8221;，否则放入&#8220;1&#8221;。<br />
5. 如果n 是左移得到的，说明指数是正的，第30位放入&#8220;1&#8221;。如果n是右移得到的或n=0，则第30位放入&#8220;0&#8221;。<br />
6. 如果n是左移得到的，则将n减去1后化为二进制，并在左边加&#8220;0&#8221;补足七位，放入第29到第23位。如果n是右移得到的或n=0，<br />
<br />
则将n化为二进制后在左边加&#8220;0&#8221;补足七位，再各位求反，再放入第29到第23位。&nbsp;&nbsp;<br />
<br />
根据上面那个步骤得：<br />
(1). 23.172001二进制形式为：0001,0111.0010,1100,0000,1000,0100,0001....<br />
(2)左移4位得：1.0111.0010,1100,0000,1000,0100,0001<br />
(3)取小数点后23位得： 0111.0010,1100,0000,1000,010（这23位为float数二进制码的后23位）<br />
(4)第31位： 0<br />
(5)第30位： 1<br />
(6)左移4位，4的二进制0011，不足7位则：0000,011<br />
最后得23.172001在计算机中的二进制表现形式为：0100,0001,1011,1001,0110,0000,0100,0010<br />
二进制转16进制得：41A96042</span>
@import url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);
<div><span  style="font-family: simsun; line-height: 23px; "><br />
</span></div>
<div><span  style="font-family: simsun; line-height: 23px; ">From:&nbsp;http://topic.csdn.net/u/20111217/19/6c5221d6-a779-4e6c-9eb0-72949857dc6e.html?95401</span></div><img src ="http://www.cppblog.com/biao/aggbug/162330.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2011-12-18 11:20 <a href="http://www.cppblog.com/biao/archive/2011/12/18/162330.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C/C++：清空输入缓冲区</title><link>http://www.cppblog.com/biao/archive/2010/12/17/136791.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Fri, 17 Dec 2010 10:17:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2010/12/17/136791.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/136791.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2010/12/17/136791.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/136791.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/136791.html</trackback:ping><description><![CDATA[<span  style="line-height: 15px; color: rgb(0, 134, 198); font-family: 'BBAlpha Sans', 'Prelude Medium', 'S60 Sans', Tahoma, Verdana, Arial, sans-serif; font-size: 12px; ">int main(int argc, char *argv[]) {<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp; std::string str1, str2;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp; std::cin &gt;&gt; str1;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp; std::cin.sync();<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp; std::cin &gt;&gt; str2;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp; std::cout &lt;&lt; str1 &lt;&lt; std::endl &lt;&lt; str2 &lt;&lt; std::endl;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp; //fflush(stdin);<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp; return EXIT_SUCCESS;<br style="line-height: 15px; ">}<br style="line-height: 15px; "><br style="line-height: 15px; ">int main(int argc, char *argv[]) {<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp; int v = 0;<br style="line-height: 15px; "><br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp; while (std::cin &gt;&gt; v) {<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; std::cout &lt;&lt; v &lt;&lt; std::endl;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp; }<br style="line-height: 15px; "><br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp; // The order clear() and sync() is very important.<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp; // Clear the input stream's state<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp; // and then clear the input stream's content<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp; // to input new string.<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp; std::cin.clear();<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp; std::cin.sync();<br style="line-height: 15px; "><br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp; std::string str;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp; std::cin &gt;&gt; str;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp; std::cout &lt;&lt; str &lt;&lt; std::endl;<br style="line-height: 15px; "><br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp; std::cout &lt;&lt; v &lt;&lt; std::endl;<br style="line-height: 15px; "><br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp; return EXIT_SUCCESS;<br style="line-height: 15px; ">}</span>
<img src ="http://www.cppblog.com/biao/aggbug/136791.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2010-12-17 18:17 <a href="http://www.cppblog.com/biao/archive/2010/12/17/136791.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C/C++：偷懒的qsort and bsearch</title><link>http://www.cppblog.com/biao/archive/2010/12/17/136788.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Fri, 17 Dec 2010 10:13:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2010/12/17/136788.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/136788.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2010/12/17/136788.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/136788.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/136788.html</trackback:ping><description><![CDATA[<span  style="line-height: 15px; color: rgb(0, 134, 198); font-family: 'BBAlpha Sans', 'Prelude Medium', 'S60 Sans', Tahoma, Verdana, Arial, sans-serif; font-size: 12px; "><div style="line-height: 15px; ">利用C标准库函数提供的快速排序和二分查找法来进行排序和查找,只需要自定义一个比较函数即可:</div><div style="line-height: 15px; "><table cellpadding="0" bgcolor="#ccccff" border="0" style="line-height: 15px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(204, 204, 255); background-position: initial initial; background-repeat: initial initial; "><tbody style="line-height: 15px; "><tr style="line-height: 15px; "><td style="line-height: 15px; 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 color="#0000ff" style="line-height: normal; "><strong style="line-height: 15px; font-weight: bold; "><span lang="EN-US" style="line-height: 15px; ">&nbsp;</span></strong><strong style="line-height: 15px; font-weight: bold; "><span lang="EN-US" style="line-height: 15px; "><span style="line-height: 15px; ">&nbsp;&nbsp;</span>#include &lt;stdlib.h&gt;</span></strong><strong style="line-height: 15px; font-weight: bold; "><span lang="EN-US" style="line-height: 15px; "><span style="line-height: 15px; ">&nbsp;&nbsp;</span>void qsort( void *buf, size_t num, size_t size, int (*compare)(const void *, const void *) );</span></strong></font></td></tr></tbody></table></div><div style="line-height: 15px; "><strong style="line-height: 15px; font-weight: bold; "><span style="line-height: 15px; font-family: 宋体; ">功能：</span></strong><span style="line-height: 18px; font-size: 10.5pt; font-family: 'Times New Roman'; ">&nbsp;</span><span style="line-height: 18px; font-size: 10.5pt; font-family: 宋体; ">对</span><em style="line-height: 15px; "><span lang="EN-US" style="line-height: 18px; font-size: 10.5pt; font-family: 'Times New Roman'; ">buf</span></em><span lang="EN-US" style="line-height: 18px; font-size: 10.5pt; font-family: 'Times New Roman'; ">&nbsp;</span><span style="line-height: 18px; font-size: 10.5pt; font-family: 宋体; ">指向的数据</span><span lang="EN-US" style="line-height: 18px; font-size: 10.5pt; font-family: 'Times New Roman'; ">(</span><span style="line-height: 18px; font-size: 10.5pt; font-family: 宋体; ">包含</span><em style="line-height: 15px; "><span lang="EN-US" style="line-height: 18px; font-size: 10.5pt; font-family: 'Times New Roman'; ">num</span></em><span lang="EN-US" style="line-height: 18px; font-size: 10.5pt; font-family: 'Times New Roman'; ">&nbsp;</span><span style="line-height: 18px; font-size: 10.5pt; font-family: 宋体; ">项</span><span lang="EN-US" style="line-height: 18px; font-size: 10.5pt; font-family: 'Times New Roman'; ">,</span><span style="line-height: 18px; font-size: 10.5pt; font-family: 宋体; ">每项的大小为</span><em style="line-height: 15px; "><span lang="EN-US" style="line-height: 18px; font-size: 10.5pt; font-family: 'Times New Roman'; ">size</span></em><span lang="EN-US" style="line-height: 18px; font-size: 10.5pt; font-family: 'Times New Roman'; ">)</span><span style="line-height: 18px; font-size: 10.5pt; font-family: 宋体; ">进行快速排序。如果函数</span><em style="line-height: 15px; "><span lang="EN-US" style="line-height: 18px; font-size: 10.5pt; font-family: 'Times New Roman'; ">compare</span></em><span lang="EN-US" style="line-height: 18px; font-size: 10.5pt; font-family: 'Times New Roman'; ">&nbsp;</span><span style="line-height: 18px; font-size: 10.5pt; font-family: 宋体; ">的第一个参数小于第二个参数，返回负值；如果等于返回零值；如果大于返回正值。函数对</span><em style="line-height: 15px; "><span lang="EN-US" style="line-height: 18px; font-size: 10.5pt; font-family: 'Times New Roman'; ">buf</span></em><span lang="EN-US" style="line-height: 18px; font-size: 10.5pt; font-family: 'Times New Roman'; ">&nbsp;</span><span style="line-height: 18px; font-size: 10.5pt; font-family: 宋体; ">指向的数据按升序排序。</span><span style="line-height: 18px; font-size: 10.5pt; font-family: 'Times New Roman'; "></span></div><div style="line-height: 15px; "><span style="line-height: 18px; font-size: 10.5pt; font-family: 'Times New Roman'; "></span>&nbsp;</div><div style="line-height: 15px; "><span style="line-height: 18px; font-size: 10.5pt; font-family: 'Times New Roman'; "><span lang="EN-US" style="line-height: 18px; "><table cellpadding="0" bgcolor="#ccccff" border="0" style="line-height: 18px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(204, 204, 255); background-position: initial initial; background-repeat: initial initial; "><tbody style="line-height: 18px; "><tr style="line-height: 18px; "><td style="line-height: 18px; 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 color="#0000ff" style="line-height: normal; "><strong style="line-height: 18px; font-weight: bold; "><span lang="EN-US" style="line-height: 18px; ">&nbsp;</span></strong><strong style="line-height: 18px; font-weight: bold; "><span lang="EN-US" style="line-height: 18px; "><span style="line-height: 18px; ">&nbsp;&nbsp;</span>#include &lt;stdlib.h&gt;</span></strong><strong style="line-height: 18px; font-weight: bold; "><span lang="EN-US" style="line-height: 18px; "><span style="line-height: 18px; ">&nbsp;&nbsp;</span>void *bsearch( const void *key, const void *buf, size_t num, size_t size, int (*compare)(const void *, const void *) );</span></strong></font></td></tr></tbody></table><strong style="line-height: 18px; font-weight: bold; "><span style="line-height: 18px; font-family: 宋体; ">功能：</span>&nbsp;</strong><span style="line-height: 18px; font-size: 10.5pt; font-family: 宋体; ">函数</span><span style="line-height: 18px; font-size: 10.5pt; font-family: 宋体; ">用折半查找法在从数组元素</span><span lang="EN-US" style="line-height: 18px; font-size: 10.5pt; font-family: 'Times New Roman'; ">buf[0]</span><span style="line-height: 18px; font-size: 10.5pt; font-family: 宋体; ">到</span><span lang="EN-US" style="line-height: 18px; font-size: 10.5pt; font-family: 'Times New Roman'; ">buf[num-1]&nbsp;</span><span style="line-height: 18px; font-size: 10.5pt; font-family: 宋体; ">匹配参数</span><span lang="EN-US" style="line-height: 18px; font-size: 10.5pt; font-family: 'Times New Roman'; ">key</span><span style="line-height: 18px; font-size: 10.5pt; font-family: 宋体; ">。如果函数</span><span lang="EN-US" style="line-height: 18px; font-size: 10.5pt; font-family: 'Times New Roman'; ">compare&nbsp;</span><span style="line-height: 18px; font-size: 10.5pt; font-family: 宋体; ">的第一个参数小于第二个参数，返回负值；如果等于返回零值；如果大于返回正值。数组</span><span lang="EN-US" style="line-height: 18px; font-size: 10.5pt; font-family: 'Times New Roman'; ">buf&nbsp;</span><span style="line-height: 18px; font-size: 10.5pt; font-family: 宋体; ">中的元素应以升序排列。函数</span><span lang="EN-US" style="line-height: 18px; font-size: 10.5pt; font-family: 'Times New Roman'; ">bsearch()</span><span style="line-height: 18px; font-size: 10.5pt; font-family: 宋体; ">的返回值是指向匹配项，如果没有发现匹配项，返回</span><span lang="EN-US" style="line-height: 18px; font-size: 10.5pt; font-family: 'Times New Roman'; ">NULL</span><span style="line-height: 18px; font-size: 10.5pt; font-family: 宋体; ">。</span><span style="line-height: 18px; font-size: 10.5pt; font-family: 'Times New Roman'; "><span lang="EN-US" style="line-height: 18px; "></span></span></span></span></div><div style="line-height: 15px; ">&nbsp;</div><div style="line-height: 15px; "><hr style="line-height: 15px; height: 1px; "></div><div style="line-height: 15px; ">struct Company {<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; char number[9];<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; char name[41];<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; char name_jp[31];<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; char region[9];<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; char address[51];<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; char telephonNumber[14]; // 因为电话号码后还跟了一个回车，所以得再多加１<br style="line-height: 15px; ">};</div><div style="line-height: 15px; "><hr style="line-height: 15px; height: 1px; "></div><div style="line-height: 15px; ">int compare(const void* company1, const void* company2) {<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return strcmp(((struct Company*)company1)-&gt;number, ((struct Company*)company2)-&gt;number);<br style="line-height: 15px; ">}</div><div style="line-height: 15px; "><hr style="line-height: 15px; height: 1px; "></div><div style="line-height: 15px; ">const struct Company* search(const char* number,<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; const struct Company* companys,<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int numberOfCompanys) {<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int compare(const void* company1, const void* company2);<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; struct Company tempCompany;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int low = 0;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int height = numberOfCompanys - 1;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int middle = 0;</div><div style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if ((strlen(number) &gt; 8) || (numberOfCompanys &lt; 1) ||(companys == NULL) || (number == NULL)) {<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return NULL;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</div><div style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; strcpy(tempCompany.number, number);&nbsp;<font color="#00ff00" style="line-height: normal; ">// 构造一个临时公司结构体,用于比较函数compare用.<br style="line-height: 15px; "></font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#00ff00" style="line-height: normal; ">/*while (low &lt;= height) {<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; middle = (low + height) / 2;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (compare(&amp;tempCompany, &amp;companys[middle])&gt; 0) {<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; low = middle + 1;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } else if (compare(&amp;tempCompany, &amp;companys[middle])&lt; 0) {<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; height = middle - 1;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } else if (compare(&amp;tempCompany, &amp;companys[middle])== 0) {<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return &amp;companys[middle];<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</font></div><div style="line-height: 15px; "><font color="#00ff00" style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return NULL;*/</font></div><div style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return (struct Company*)bsearch(&amp;tempCompany, companys, numberOfCompanys, sizeof(struct Company),<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; compare);<br style="line-height: 15px; ">}</div><div style="line-height: 15px; "><hr style="line-height: 15px; height: 1px; "></div><div style="line-height: 15px; ">void sort(struct Company* companys, int numberOfCompanys) {<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int compare(const void* company1, const void* company2);&nbsp;<font color="#00ff00" style="line-height: normal; ">// 快速排序函数qsort需要的比较函数<br style="line-height: 15px; "></font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; qsort(companys, numberOfCompanys, sizeof(struct Company), compare);&nbsp;<font color="#00ff00" style="line-height: normal; ">// 调用系统提供的快速排序进行排序.</font><br style="line-height: 15px; ">}</div><p style="line-height: 15px; margin-top: 0px; margin-right: 0px; margin-bottom: 1.35em; margin-left: 0px; ">&nbsp;</p></span>
<img src ="http://www.cppblog.com/biao/aggbug/136788.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2010-12-17 18:13 <a href="http://www.cppblog.com/biao/archive/2010/12/17/136788.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C/C++：命令行输入不回显</title><link>http://www.cppblog.com/biao/archive/2010/12/17/136787.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Fri, 17 Dec 2010 10:12:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2010/12/17/136787.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/136787.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2010/12/17/136787.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/136787.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/136787.html</trackback:ping><description><![CDATA[<span  style="line-height: 15px; color: rgb(0, 134, 198); font-family: 'BBAlpha Sans', 'Prelude Medium', 'S60 Sans', Tahoma, Verdana, Arial, sans-serif; font-size: 12px; "><div style="line-height: 15px; ">#include &lt;iostream&gt;<br style="line-height: 15px; ">#include &lt;conio.h&gt;</div><div style="line-height: 15px; "><font color="#00ff00" style="line-height: normal; ">/**<br style="line-height: 15px; ">&nbsp;* 秘密在于conio.h中的getch()从键盘中读取字符时,并不会在屏幕上输出已经输入的字符,<br style="line-height: 15px; ">&nbsp;* 而用一个putch('*')来哄骗,代表已经输入一个字符<br style="line-height: 15px; ">&nbsp;* 怪不得这个头文件要叫conio.h, con的意思就有哄骗,看来就是由此而来.<br style="line-height: 15px; ">&nbsp;*/</font></div><div style="line-height: 15px; ">using namespace std;</div><div style="line-height: 15px; ">int main() {<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; char* password;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; char* passwordConfirm;</div><div style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int length = 4;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; password = new char[length + 1];<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; passwordConfirm = new char[length + 1];</div><div style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; char* p = NULL;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int count = 0;</div><div style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cout &lt;&lt; "Input password : ";<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; p = password;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; count = 0;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //fflush(stdin);<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while (((*p = getch()) != 13) &amp;&amp; count &lt; length) {<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#00ff00" style="line-height: normal; ">// 这里不是'\n'(10), new line<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 而是'\r'(13), reback. 即是按下回车键,好像这个东西是linux的.<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 主要是与getch这个函数有关.<br style="line-height: 15px; "></font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; putch('*');<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fflush(stdin);</div><div style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; p++;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; count++;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; password[count] = '\0';</div><div style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cout &lt;&lt; endl &lt;&lt; "Confirm the password : ";<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; p = passwordConfirm;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; count = 0;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //fflush(stdin);<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while (((*p = getch()) != 13) &amp;&amp; count &lt; length) {<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; putch('*');<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fflush(stdin);</div><div style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; p++;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; count++;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; passwordConfirm[count] = '\0';</div><div style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cout &lt;&lt; endl;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (strcmp(password, passwordConfirm) == 0) {<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cout &lt;&lt; "The password is right." &lt;&lt; endl;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cout &lt;&lt; password &lt;&lt; endl;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } else {<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cout &lt;&lt; "Confirm password fail." &lt;&lt; endl;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cout &lt;&lt; password &lt;&lt; endl &lt;&lt; passwordConfirm &lt;&lt; endl;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</div><div style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return 0;<br style="line-height: 15px; ">}</div></span>
<img src ="http://www.cppblog.com/biao/aggbug/136787.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2010-12-17 18:12 <a href="http://www.cppblog.com/biao/archive/2010/12/17/136787.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C/C++：读取文件小细节</title><link>http://www.cppblog.com/biao/archive/2010/12/17/136786.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Fri, 17 Dec 2010 10:11:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2010/12/17/136786.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/136786.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2010/12/17/136786.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/136786.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/136786.html</trackback:ping><description><![CDATA[<span  style="line-height: 15px; color: rgb(0, 134, 198); font-family: 'BBAlpha Sans', 'Prelude Medium', 'S60 Sans', Tahoma, Verdana, Arial, sans-serif; font-size: 12px; "><div id="msgcns!FDDCD947AE5B2C06!237" class="bvMsg" style="line-height: 15px; width: 820px; overflow-x: hidden; overflow-y: hidden; text-overflow: ellipsis; "><div style="line-height: 15px; "><font color="#00ff00" style="line-height: normal; ">以Ｃ中读文件时，要非常小心，用二进制处理和用文本方式处理，对于用文本方式写入的文件，操作结果是相同的，但是对于用二进制写入的文件，结果却是大为不同.例如用其对.exe文件进行读取，</font></div><div style="line-height: 15px; ">#include &lt;iostream&gt;<br style="line-height: 15px; ">#include &lt;ctime&gt;<br style="line-height: 15px; ">#include &lt;fstream&gt;</div><div style="line-height: 15px; ">using namespace std;</div><div style="line-height: 15px; ">int main(){<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FILE* fp;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fp = fopen("d.txt", "wb");<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; char* str = "biao";<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fwrite(str, strlen(str), 1, fp);<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fclose(fp);<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fp = fopen("test.exe", "rb");<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fseek(fp, 0L, SEEK_END);<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int length = ftell(fp);<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cout &lt;&lt; "File length: " &lt;&lt; length &lt;&lt; endl;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rewind(fp);<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; char ch;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int count = 0;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while (!feof(fp)) {<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ch = fgetc(fp);<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //cout &lt;&lt; ch;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; count++;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cout &lt;&lt; endl &lt;&lt; count &lt;&lt; endl;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cout &lt;&lt; (int)ch &lt;&lt; endl;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cout &lt;&lt; ftell(fp) &lt;&lt; endl;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fclose(fp);<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return 0;<br style="line-height: 15px; ">}</div><div style="line-height: 15px; ">&nbsp;</div><div style="line-height: 15px; ">文件长度是476268，当用rb时，while (!feof(fp))能正常的读到文件末，但是如果用r来读取，到最后，ftell(fp)，正好是文件的长度，但用r时，while (!feof(fp))并不能到达文件尾，最后ftell(fp)的值是7859,相差太多，对图片文件也是一样的.也许对于用r和rb打开的文件操作时，对于feof(fp)来判断是否到达文件尾时的方式不一样. 即用r打开时，feof(fp)的判断是当读取遇到EOF即-1时，判断为文件结束，注意，即使是用rb方式打开文件，如果判断的时候不用while(!feof(fp))而用((ch = fgetc(fp)) != EOF)，一样的是读取遇到EOF时循环就不会继续进行，即EOF只适合于文本试，但feof(fp)却会根据是用文本方式还是用二进制方式来判断自动使用不同的判断是否到达文件尾.</div><div style="line-height: 15px; ">&nbsp;</div><div style="line-height: 15px; ">所以更通用一些的判断文件结束方式用feof.</div><div style="line-height: 15px; ">&nbsp;</div><div style="line-height: 15px; ">通过对fread(buf, size, num, fp)的测试，当size * num相同时，如<font color="#00ff00" style="line-height: normal; ">fread(buf, 255, 1, fp)与fread(buf, 1, 255, fp)的速度相差非常</font><font color="#00ff00" style="line-height: normal; ">小，</font>读取210M的二进制文件用了9秒多一些（缓冲区设置成256B, 128, 256, 1024时的速度都差不多，但是200K时却是13秒，看来并非是缓冲区越大越好，但是缓冲区大时，可以减少对硬盘进行操作的次数）.</div><div style="line-height: 15px; ">&nbsp;</div><div style="line-height: 15px; ">如用rb(二进制方式)把结构体的数据写入文件时，不会产生额外的数据，用<font color="#00ff00" style="line-height: normal; ">fseek(fp, 0L, SEEK_END), ftell(fp),</font>取得的长度正好是结构的长度＊个数，但是用r(文本方式)把结构体的数据写入文件时，却会产生一些客外的数据，此时，如果想从文件中从后向前读取结构体的数据，就不能正常得到所有数据，好在从前向后读没有问题.以防万一，应该用二进制方式来操作，这样，即可以从前向后读，也可以从后向前读.在fseek中，可以用负数来相对于当前文件指针，把文件指针向前移动,<font color="#00ff00" style="line-height: normal; ">fseek(fp, -sizeof(STRUCT), SEEK_CUR).</font></div><div><font color="#00ff00" style="line-height: normal; "><br></font></div></div></span>
<img src ="http://www.cppblog.com/biao/aggbug/136786.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2010-12-17 18:11 <a href="http://www.cppblog.com/biao/archive/2010/12/17/136786.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C/C++：~(~0 &lt;&lt; 6)</title><link>http://www.cppblog.com/biao/archive/2010/12/17/136785.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Fri, 17 Dec 2010 10:10:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2010/12/17/136785.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/136785.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2010/12/17/136785.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/136785.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/136785.html</trackback:ping><description><![CDATA[<span  style="line-height: 15px; color: rgb(0, 134, 198); font-family: 'BBAlpha Sans', 'Prelude Medium', 'S60 Sans', Tahoma, Verdana, Arial, sans-serif; font-size: 12px; "><div style="line-height: 15px; ">怎样使得一个数从右向左4~9位全是1:</div><div style="line-height: 15px; ">int a = 222222;</div><div style="line-height: 15px; ">int temp = ~(~0 &lt;&lt; 6);<font color="#00ff00" style="line-height: normal; ">// ~0 使用数的每一位全为1, 左移六位，使得最右边6位全为0, 再取反，例前面全为0,后面6位为1.</font></div><div style="line-height: 15px; ">a&nbsp;&gt;&gt;=&nbsp;4;</div><div style="line-height: 15px; ">a&nbsp;&amp;= temp;</div><div style="line-height: 15px; ">a &lt;&lt;= 4;</div><div style="line-height: 15px; ">&nbsp;</div><div style="line-height: 15px; "><font color="#00ff00" style="line-height: normal; ">32 bits binary nummer:</font></div><div style="line-height: 15px; ">#include &lt;iostream&gt;</div><div style="line-height: 15px; ">using namespace std;</div><div style="line-height: 15px; ">int main() {<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int ref = 1 &lt;&lt; 31;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int a = 14;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int i = 0; i &lt; 32; i++) {<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cout &lt;&lt; (a &amp; ref? '1': '0');<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; a &lt;&lt;= 1;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return 0;<br style="line-height: 15px; ">}</div></span>
<img src ="http://www.cppblog.com/biao/aggbug/136785.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2010-12-17 18:10 <a href="http://www.cppblog.com/biao/archive/2010/12/17/136785.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Java：强大的Eclipse</title><link>http://www.cppblog.com/biao/archive/2010/12/17/136784.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Fri, 17 Dec 2010 10:09:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2010/12/17/136784.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/136784.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2010/12/17/136784.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/136784.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/136784.html</trackback:ping><description><![CDATA[<span  style="line-height: 15px; color: rgb(0, 134, 198); font-family: 'BBAlpha Sans', 'Prelude Medium', 'S60 Sans', Tahoma, Verdana, Arial, sans-serif; font-size: 12px; "><div style="line-height: 15px; ">在Eclipse中写OpenGL, Qt, C, C++, Java, links方式安装插件，导入工程：</div><div style="line-height: 15px; "><font color="#00ff00" style="line-height: normal; ">Java:</font>&nbsp;Eclipse要运行在jre的环境之上，自带Java环境</div><div style="line-height: 15px; "><font color="#00ff00" style="line-height: normal; ">C, C++:&nbsp;&nbsp;</font>下载CDT，不同版本的CDT与不同版本的Eclipse匹配，因为他们的更新很快</div><div style="line-height: 15px; ">&nbsp;</div><div style="line-height: 15px; ">安装CDT和Eclipse时，并没有C/C++编译器，得自己下一个，对于windows有两个比较好:Cygwin和MinGW，都是使用Linux下的GCC windows移植版，对标准支持非常好，不像windows自己的，搞得乱七八糟，不过最好是安装MinGW，因为大多数都是支持MinGW，而且Qt的windows安装版本就只支持MinGW，如果要用Cygwin的话，那得自己编译Qt的源码。安装MinGW的一个更好的方式，使用免费开源的Dev-Cpp，其中就自带了MinGW，更好的一点是，可以用他的包管理器到网络上下载需要的程序库，非常方便，他还支持OpenGL，如想用glut库，他也可以在网上自动找到，Qt的安装版中已经支持了OpenGL，所以对于C/C++:</div><div style="line-height: 15px; ">Dev-Cpp + CDT + Eclipse = 搞定.</div><div style="line-height: 15px; ">&nbsp;</div><div style="line-height: 15px; "><font color="#00ff00" style="line-height: normal; ">OpenGL + glut:</font>&nbsp;用Dev-Cpp下载glut库，然后在Eclipse的工程的库中引入外部库：</div><div style="line-height: 15px; ">opengl32, glu32, glut32, gdi32, winmm = 搞定</div><div style="line-height: 15px; ">&nbsp;</div><div style="line-height: 15px; "><font color="#00ff00" style="line-height: normal; ">links方式安装插件：</font>在Eclipse的目录下建一个links文件夹，在里面任意建文件xxxx.link，在xxxx.link里写上插件所在目录：</div><div style="line-height: 15px; ">path=D:/Software/Eclipse-SDK-3.3M6-win32/CDT-4.0.0-M7-win32.x86，这里必须注意，其中的文件分隔符用的是linux的文件分隔符/，而不是windows的文件分隔符\，写错了插件就显示不出来...开始的时候用\，晕ing，插件没显示出来，还好不傻，试了一下/，嘿嘿，出来了。</div><div style="line-height: 15px; ">&nbsp;</div><div style="line-height: 15px; "><font color="#00ff00" style="line-height: normal; ">Qt:&nbsp;</font>先装CDT，然后下载Qt Eclipse Integration安装，直接搞定。对于新加入的.h, .cpp文件，.pro文件会自动更新。</div><div style="line-height: 15px; ">Download:&nbsp;<a href="http://trolltech.com/developer/downloads/qt/index?searchterm=eclipse+integration" style="line-height: 15px; font-weight: inherit; text-decoration: none; color: rgb(0, 102, 167); cursor: pointer; ">http://trolltech.com/developer/downloads/qt/index?searchterm=eclipse+integration</a></div><div style="line-height: 15px; ">&nbsp;</div><div style="line-height: 15px; "><font color="#00ff00" style="line-height: normal; ">完整安装顺序：</font></div><div style="line-height: 15px; "><font color="#00ff00" style="line-height: normal; ">JDK + Eclipse + Dev-Cpp(MinGW...package tool + glut / boost==) + CDT(links安装) + Qt4.3 + Qt Eclipse Integration</font></div></span>
<img src ="http://www.cppblog.com/biao/aggbug/136784.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2010-12-17 18:09 <a href="http://www.cppblog.com/biao/archive/2010/12/17/136784.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C/C++：Effective C++</title><link>http://www.cppblog.com/biao/archive/2010/12/17/136782.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Fri, 17 Dec 2010 10:07:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2010/12/17/136782.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/136782.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2010/12/17/136782.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/136782.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/136782.html</trackback:ping><description><![CDATA[<span  style="line-height: 15px; color: rgb(0, 134, 198); font-family: 'BBAlpha Sans', 'Prelude Medium', 'S60 Sans', Tahoma, Verdana, Arial, sans-serif; font-size: 12px; "><div style="line-height: 15px; ">1.&nbsp;&nbsp; 尽量用const和inline，而不用#define，即尽量用编译器，而不用预处理器。</div><div style="line-height: 15px; ">2.&nbsp;&nbsp; 尽量用iostream，而不用stdio.h。</div><div style="line-height: 15px; ">3.&nbsp;&nbsp; 尽量用new和delete，而不用malloc和free，因为new和delete会执行构造函数和析构函数，而malloc和free不会。</div><div style="line-height: 15px; ">4.&nbsp;&nbsp; 尽量在函数中使用C++风格的注释 // 函数头用/* */</div><div style="line-height: 15px; ">5.&nbsp;&nbsp; new和delete与malloc和free要成对出现。</div><div style="line-height: 15px; ">6.&nbsp;&nbsp; 析构函数里对指针成员delete，delete NULL指针并不影响什么。</div><div style="line-height: 15px; ">7.&nbsp;&nbsp; 预先准备好内在不够的情况，try{ new..} catch(bad_alloc baex) {}</div><div style="line-height: 15px; ">8.&nbsp;&nbsp; 写operator new和operator delete时要遵循常规。</div><div style="line-height: 15px; ">9.&nbsp;&nbsp; 如果写了operator new，就要写operator delete。</div><div style="line-height: 15px; ">10. 为需要动态分配内存的类提供一个复制构造函数和赋值操作运算符。</div><div style="line-height: 15px; ">11. 尽量使用初始化，而不在构造函数里赋值。CC::CC(int v_value) : value(v_value) {}</div><div style="line-height: 15px; ">12. 初始化列表中成员的列出顺序与成员的在类中的声明顺序相同。</div><div style="line-height: 15px; ">13. 确定基类有虚析构函数，否则释放子类时只能执行基类的析构函数，而不会执行子类的虚构函数。</div><div style="line-height: 15px; ">14. 让operator= 返回*this的引用， (a = b) = c;只是为了这个能正确执行，不过这可是一个很阿格里拉的编程风格。</div><div style="line-height: 15px; ">15. 在operator= 中对所有数据成员赋值。</div><div style="line-height: 15px; ">16. 在operator= 中检查对自己赋值的情况。如不处理这种情况，在operator= 中先释放指针对象数据，</div><div style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp; 然后再把目标的数据复制过来，因为是自己给自己复制，但数据已经释放，所以发生错误。</div><div style="line-height: 15px; ">17. 争取使类的接口完整并最小。</div><div style="line-height: 15px; ">18. 分清成员函数，非成员函数和友元函数。</div><div style="line-height: 15px; ">19. 避免public接口出现数据成员。set, get.</div><div style="line-height: 15px; ">20. 尽可能使用const。</div><div style="line-height: 15px; ">21. 尽量传引用，而不是传值。const ClassName&amp;。</div><div style="line-height: 15px; ">22. 必须返回一个对象时，不要试图返回一个引用。如函数中的临时变量的引用。</div><div style="line-height: 15px; ">23. 在函数重载和设定参数缺省值的函数之间慎重选择。</div><div style="line-height: 15px; ">24. 避免指针和数字类型重载：void func(int x)与void func(string* str)传入0时：func(static_cast&lt;string*&gt;(null)。</div><div style="line-height: 15px; ">25. 如果不想使用隐式生成的函数，就显示的禁用它：private Array&amp; operator=(const Array&amp; ref);</div><div style="line-height: 15px; ">26. 使用名字空间的好处：尽可能的避免名字冲突引发的问题。假如一个程序中有几百个名字冲突，名字空间能很好的解决这个问题。</div><div style="line-height: 15px; ">27. 避免返回内部数据的句柄：指针。</div><div style="line-height: 15px; ">28. 避免返回private和protected成员的非const引用和指针。</div><div style="line-height: 15px; ">29. 千万不要返回局部对象的引用，也不要返回函数内部定义的new初始化的指针，容易忘掉delete。</div><div style="line-height: 15px; ">30. 尽可能推迟变量的定义，这个在C中做不到，定义无用的变量必然伴随无用的开销。</div><div style="line-height: 15px; ">31. 将文件间的编译依赖性降至最低：分离的关键在于：对类的声明的依赖与对类的定义的依赖：</div><div style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp; 尽量让头文件不要依赖于另的文件。如使用ImageLoad.h中的Image类，在类的声明中不要#include"ImageLoad.h",</div><div style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp; 而只是声明类class Image; 在类的定义中才使用#include"ImageLoad.h"。</div><div style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp; 尽量不要在头文件中包含其他的头文件。</div><div style="line-height: 15px; ">32. 使用函数体现&#8220;是一个&#8221;的含义，而不是&#8220;有一个&#8221;。</div><div style="line-height: 15px; ">33. 明智的使用多继承，这东西不好玩。</div><div style="line-height: 15px; ">34. 编译器能为我们自动生成的函数：</div><div style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp; 构造函数，析构函数，复制构造函数，赋值运算符重载，一对取地址运算符。</div><div style="line-height: 15px; ">35. 宁可编译链接时出错，也不愿运行时出错。极少情况下会使用C++抛出异常：内存耗尽：</div><div style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp; C++中没有上溢，下溢，除零检查，数组越界检查。</div><div style="line-height: 15px; ">36. 重视编译器警告。</div><div style="line-height: 15px; ">37. 熟悉标准库，在名字空间std中的。C++的伪标准库，如iostream.h, limits.h。</div><div style="line-height: 15px; ">38. 常变量，类成员，父类构造函数要放在初始化列表初始化。</div><div style="line-height: 15px; ">39. 指针定义时初始化为NULL。</div><div style="line-height: 15px; ">40. 终极解决大法：良好的编译习惯可很避免上面很多情况的发生，如有一天C++的语法变得像Java一样限制那么多，</div><div style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp; 就不会出现这么多问题，不过也正因为如此，才出现了N多C++奇淫技巧，就如茴香豆的茴有四种写法，</div><div style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp; 这里可不止四种。</div></span>
<img src ="http://www.cppblog.com/biao/aggbug/136782.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2010-12-17 18:07 <a href="http://www.cppblog.com/biao/archive/2010/12/17/136782.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C/C++：Little-endian oder Big-endian --&gt; CPU</title><link>http://www.cppblog.com/biao/archive/2010/12/17/136781.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Fri, 17 Dec 2010 10:07:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2010/12/17/136781.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/136781.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2010/12/17/136781.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/136781.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/136781.html</trackback:ping><description><![CDATA[<span  style="line-height: 15px; color: rgb(0, 134, 198); font-family: 'BBAlpha Sans', 'Prelude Medium', 'S60 Sans', Tahoma, Verdana, Arial, sans-serif; font-size: 12px; "><div style="line-height: 15px; ">怎么确定CPU对操作数的存放方式，用的是Little-endian（从低字节到高字节）还是Big-endian？</div><div style="line-height: 15px; ">呵呵，如果知道怎么用联合体的话（再加上你是个天才：能几分钟内搞出这个来的就是天才，天才也能这样定义，出题的人大脑有问题，嘿嘿，这问题好像很容易啊）：</div><div style="line-height: 15px; ">union uT{</div><div style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; short a;</div><div style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; char b;</div><div style="line-height: 15px; ">}u;</div><div style="line-height: 15px; ">u.a = 0x1234;</div><div style="line-height: 15px; ">&nbsp;</div><div style="line-height: 15px; ">cout &lt;&lt; u.a &lt;&lt; endl;</div><div style="line-height: 15px; ">cout &lt;&lt; (int)u.b &lt;&lt; endl;&nbsp;<font color="#00ff00" style="line-height: normal; ">// 如果u.b == 0x12,则采用的是Little-endian，因为联合体占用同一个内存空间存放成员。</font></div><div style="line-height: 15px; "><font color="#00ff00" style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 我的CPU用的是Big-endian，从高字节到低字节，因为u.b = 52，TGA的文件也是这样存的。</font></div><div style="line-height: 15px; "><font color="#00ff00" style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 联合体的成员都是按地址顺序从低地址向高地址存放。</font></div></span>
<img src ="http://www.cppblog.com/biao/aggbug/136781.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2010-12-17 18:07 <a href="http://www.cppblog.com/biao/archive/2010/12/17/136781.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C/C++：读取文件</title><link>http://www.cppblog.com/biao/archive/2010/12/17/136775.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Fri, 17 Dec 2010 09:54:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2010/12/17/136775.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/136775.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2010/12/17/136775.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/136775.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/136775.html</trackback:ping><description><![CDATA[<span  style="line-height: 15px; color: rgb(0, 134, 198); font-family: 'BBAlpha Sans', 'Prelude Medium', 'S60 Sans', Tahoma, Verdana, Arial, sans-serif; font-size: 12px; "><div style="line-height: 15px; ">#include &lt;iostream&gt;<br style="line-height: 15px; ">#include &lt;cmath&gt;<br style="line-height: 15px; ">#include &lt;stdio.h&gt;</div><div style="line-height: 15px; "><br style="line-height: 15px; ">using namespace std;</div><div style="line-height: 15px; ">&nbsp;</div><div style="line-height: 15px; ">int main(){<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FILE* file = NULL;</div><div style="line-height: 15px; "><br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; file = fopen("main.cpp", "r");</div><div style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (NULL == file) {</div><div style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cerr &lt;&lt; "Cann't open the file." &lt;&lt; endl;</div><div style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return 1;</div><div style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br style="line-height: 15px; "><br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; char** line = NULL;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; line = new char*[60];<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int i = 0; i &lt; 60; i++) {<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; line[i] = new char[80];<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#ff9900" style="line-height: normal; ">&nbsp;line[i][0] = '\0';</font>&nbsp;<font color="#00ff00" style="line-height: normal; ">// 关键就在这里，如果不把每个串的第一个字符设为0，则在输出时，如果源文件的最后一行是空行，</font></div><div style="line-height: 15px; "><font color="#00ff00" style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 则最后一行是两个字符的乱码(x ?=)，这有可能是跟读到文件末时的文件结束标志有关。<br style="line-height: 15px; "></font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br style="line-height: 15px; ">&nbsp;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int row = 0;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while (!feof(file)) {<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fgets(line[row++], 80, file);&nbsp; // 每读取一行后，记录读取的行数加一。<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fclose(file);</div><div style="line-height: 15px; "><br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int i = 0; i &lt; row; i++) {<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cout &lt;&lt; line[i];<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</div><div style="line-height: 15px; "><br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return 0;&nbsp;<br style="line-height: 15px; ">}</div></span>
<img src ="http://www.cppblog.com/biao/aggbug/136775.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2010-12-17 17:54 <a href="http://www.cppblog.com/biao/archive/2010/12/17/136775.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C/C++：向函数传递二维指针的麻烦</title><link>http://www.cppblog.com/biao/archive/2010/12/17/136773.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Fri, 17 Dec 2010 09:52:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2010/12/17/136773.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/136773.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2010/12/17/136773.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/136773.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/136773.html</trackback:ping><description><![CDATA[<span  style="line-height: 15px; color: rgb(0, 134, 198); font-family: 'BBAlpha Sans', 'Prelude Medium', 'S60 Sans', Tahoma, Verdana, Arial, sans-serif; font-size: 12px; "><div style="line-height: 15px; ">array[3][3],这时，array占有9个连续的内存单元，地址是连续的，如果真可以直接简单的传给指向指针的指针的，则二维指针取下一个元素地址就是简单的加法了，但是如果传入的指针是int** p; p = new int*[3];呢？</div><div style="line-height: 15px; ">for (int i = 0; i &lt; 3; i++) {<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; *(p + i) = new int[3];<br style="line-height: 15px; ">}</div><div style="line-height: 15px; ">这时二维指针p的元素占有的9个内存单元却不能保证是连续的（只能保证有每行的3个是连续的），取下一行地址时，就不能如上面的那样简单的直接相加了（上一行末元素的地址直接加4），所以得传入一个指向指针的指针才行，如int* p[3]（存储的是每行的指针）或者int** p，为p初始化，分配空间;<br style="line-height: 15px; ">所以int** p 和int p[row][column]绝对不是一回事。</div><div style="line-height: 15px; "><br style="line-height: 15px; ">像下面这样可以正确运行：</div><div style="line-height: 15px; ">#include &lt;iostream&gt;<br style="line-height: 15px; ">#include &lt;stdio.h&gt;</div><div style="line-height: 15px; ">using namespace std;</div><div style="line-height: 15px; ">void func(int** array, int row, int column) {<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int i = 0; i &lt; row; i++) {<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int j = 0; j &lt; column; j++) {<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cout &lt;&lt; *(*(array+i) + j) &lt;&lt; "\t";<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cout &lt;&lt; endl;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br style="line-height: 15px; ">}</div><div style="line-height: 15px; "><br style="line-height: 15px; ">int main(){<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int array[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int* p[3];<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; p[0] = array[0];<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; p[1] = array[1];<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; p[2] = array[2];<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; func(p, 3, 3);&nbsp;<font color="#00ff00" style="line-height: normal; ">// 如果这里传入array, array[0], &amp;array[0][0]，则编译不通过<br style="line-height: 15px; "></font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#00ff00" style="line-height: normal; ">&nbsp;// 传入int* q = array[0]; int** p = &amp;q;编译通过，但运行时要么错退出，要么输出的结果是错的。<br style="line-height: 15px; "></font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return 0;<br style="line-height: 15px; ">}</div></span>
<img src ="http://www.cppblog.com/biao/aggbug/136773.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2010-12-17 17:52 <a href="http://www.cppblog.com/biao/archive/2010/12/17/136773.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C/C++：八皇后</title><link>http://www.cppblog.com/biao/archive/2010/12/17/136772.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Fri, 17 Dec 2010 09:51:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2010/12/17/136772.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/136772.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2010/12/17/136772.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/136772.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/136772.html</trackback:ping><description><![CDATA[<span  style="line-height: 15px; color: rgb(0, 134, 198); font-family: 'BBAlpha Sans', 'Prelude Medium', 'S60 Sans', Tahoma, Verdana, Arial, sans-serif; font-size: 12px; "><p style="line-height: 15px; margin-top: 0px; margin-right: 0px; margin-bottom: 1.35em; margin-left: 0px; ">#include &lt;iostream&gt;<br style="line-height: 15px; ">#include &lt;cstdio&gt;<br style="line-height: 15px; ">#include &lt;ctime&gt;<br style="line-height: 15px; ">#include &lt;cmath&gt;</p><p style="line-height: 15px; margin-top: 0px; margin-right: 0px; margin-bottom: 1.35em; margin-left: 0px; ">using namespace std;</p><p style="line-height: 15px; margin-top: 0px; margin-right: 0px; margin-bottom: 1.35em; margin-left: 0px; ">void queen(int** array, int row, FILE* fp);<br style="line-height: 15px; ">bool isAvailable(int** array, int y, int x);<br style="line-height: 15px; ">bool isResult(int** array);</p><p style="line-height: 15px; margin-top: 0px; margin-right: 0px; margin-bottom: 1.35em; margin-left: 0px; ">int main(int arg, char** argv) {<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int** array;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#00ff00" style="line-height: normal; ">&nbsp;// 分配空间<br style="line-height: 15px; "></font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; array = new int*[8];<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int i = 0; i &lt; 8; i++) {<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; array[i] = new int[8];<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#00ff00" style="line-height: normal; ">// 初始化皇后状态</font><br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int i = 0; i &lt; 8; i++) {<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int j = 0; j &lt; 8; j++) {<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; array[i][j] = 0;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FILE* fp = fopen("data.txt", "w");<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; queen(array, 0, fp);<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fclose(fp);<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#00ff00" style="line-height: normal; ">&nbsp;// 释放空间<br style="line-height: 15px; "></font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int i = 0; i &lt; 8; i++) {<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; delete[] array[i];<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; delete array;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; array = NULL;</p><p style="line-height: 15px; margin-top: 0px; margin-right: 0px; margin-bottom: 1.35em; margin-left: 0px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return 0;<br style="line-height: 15px; ">}</p><p style="line-height: 15px; margin-top: 0px; margin-right: 0px; margin-bottom: 1.35em; margin-left: 0px; ">void queen(int** array, int row, FILE* fp) {<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; static int size = 1;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (row &gt; 7) {<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int j = 0; j &lt; 8; j++) {<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#00ff00" style="line-height: normal; ">// 如果合法<br style="line-height: 15px; "></font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (isAvailable(array, row, j)) {<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; array[row][j] = 1;<br style="line-height: 15px; ">&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 style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#00ff00" style="line-height: normal; ">&nbsp;// 当前位置合法，赋值后，进入下一行<br style="line-height: 15px; "></font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; queen(array, row + 1, fp);</p><p style="line-height: 15px; margin-top: 0px; margin-right: 0px; margin-bottom: 1.35em; margin-left: 0px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#00ff00" style="line-height: normal; ">// 如果退回来后是结果，则打印出来<br style="line-height: 15px; "></font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (isResult(array)) {<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#00ff00" style="line-height: normal; ">// 把结果写入文件<br style="line-height: 15px; "></font>&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; fputc('\n', fp);<br style="line-height: 15px; ">&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; fputs("***********************************", fp);<br style="line-height: 15px; ">&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; fprintf(fp, " %d ", size++);<br style="line-height: 15px; ">&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; fputs("***********************************", fp);<br style="line-height: 15px; ">&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; fputc('\n', fp);<br style="line-height: 15px; ">&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; for (int m = 0; m &lt; 8; m++) {<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int n = 0; n &lt; 8; n++) {<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (array[m][n] == 1) {<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fputc('#', fp);<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fputc(' ', fp);<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fputc(' ', fp);<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } else {<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fputc('O', fp);<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fputc(' ', fp);<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fputc(' ', fp);<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fputc('\n', fp);&nbsp;<br style="line-height: 15px; ">&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; }</p><p style="line-height: 15px; margin-top: 0px; margin-right: 0px; margin-bottom: 1.35em; margin-left: 0px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; array[row][j] = 0;</p><p style="line-height: 15px; margin-top: 0px; margin-right: 0px; margin-bottom: 1.35em; margin-left: 0px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br style="line-height: 15px; ">}</p><p style="line-height: 15px; margin-top: 0px; margin-right: 0px; margin-bottom: 1.35em; margin-left: 0px; ">bool isAvailable(int** array, int row, int column) {&nbsp;<font color="#00ff00" style="line-height: normal; ">// 第row行，column列<br style="line-height: 15px; "></font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int i, j;</p><p style="line-height: 15px; margin-top: 0px; margin-right: 0px; margin-bottom: 1.35em; margin-left: 0px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#00ff00" style="line-height: normal; ">&nbsp; // 检查row行</font><br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (i = 0; i &lt; 8; i++) {<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (array[row][i] != 0) {<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return false;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p><p style="line-height: 15px; margin-top: 0px; margin-right: 0px; margin-bottom: 1.35em; margin-left: 0px; ">&nbsp;&nbsp;&nbsp;&nbsp;<font color="#00ff00" style="line-height: normal; ">&nbsp;&nbsp;&nbsp; // 检columnx列</font><br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (j = 0; j &lt; 8; j++) {<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (array[j][column] != 0) {<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return false;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p><p style="line-height: 15px; margin-top: 0px; margin-right: 0px; margin-bottom: 1.35em; margin-left: 0px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#00ff00" style="line-height: normal; ">// 检查对角向左上</font><br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; i = row;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; j = column;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while (i &gt;= 0 &amp;&amp; j &gt;= 0) {<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (array[i][j] != 0) {<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return false;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; i--;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; j--;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p><p style="line-height: 15px; margin-top: 0px; margin-right: 0px; margin-bottom: 1.35em; margin-left: 0px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#00ff00" style="line-height: normal; ">&nbsp; // 检查对角向右上<br style="line-height: 15px; "></font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; i = row;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; j = column;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while (i &gt;= 0 &amp;&amp; j &lt; 8) {<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (array[i][j] != 0) {<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return false;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; i--;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; j++;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p><p style="line-height: 15px; margin-top: 0px; margin-right: 0px; margin-bottom: 1.35em; margin-left: 0px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#00ff00" style="line-height: normal; ">// 检查对角向左下</font><br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; i = row;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; j = column;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while (i &lt; 8 &amp;&amp; j &gt;= 0) {<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (array[i][j] != 0) {<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return false;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; i++;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; j--;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p><p style="line-height: 15px; margin-top: 0px; margin-right: 0px; margin-bottom: 1.35em; margin-left: 0px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#00ff00" style="line-height: normal; ">&nbsp; // 检查对角向右下</font><br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; i = row;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; j = column;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while (i &lt; 8 &amp;&amp; j &lt; 8) {<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (array[i][j] != 0) {<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return false;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; i++;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; j++;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p><p style="line-height: 15px; margin-top: 0px; margin-right: 0px; margin-bottom: 1.35em; margin-left: 0px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return true;<br style="line-height: 15px; ">}</p><p style="line-height: 15px; margin-top: 0px; margin-right: 0px; margin-bottom: 1.35em; margin-left: 0px; ">bool isResult(int** array) {<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int size = 0;&nbsp;<font color="#00ff00" style="line-height: normal; ">// 皇后个数</font></p><p style="line-height: 15px; margin-top: 0px; margin-right: 0px; margin-bottom: 1.35em; margin-left: 0px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#00ff00" style="line-height: normal; ">// 统计皇后个数<br style="line-height: 15px; "></font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int i = 0; i &lt; 8; i++) {<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int j = 0; j &lt; 8; j++) {<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (array[i][j] != 0) {<br style="line-height: 15px; ">&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; size++;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p><p style="line-height: 15px; margin-top: 0px; margin-right: 0px; margin-bottom: 1.35em; margin-left: 0px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#00ff00" style="line-height: normal; ">// 没有８个皇后，说明不是结果</font></p><p style="line-height: 15px; margin-top: 0px; margin-right: 0px; margin-bottom: 1.35em; margin-left: 0px; "><font color="#00ff00" style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 有8个皇后,因为每个皇后的放置都是在合法位置, 所以只需要简单的计算皇后个数就知道是不是结果.</font><br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (size != 8) {<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return false;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p><p style="line-height: 15px; margin-top: 0px; margin-right: 0px; margin-bottom: 1.35em; margin-left: 0px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return true;<br style="line-height: 15px; ">}</p></span>
<img src ="http://www.cppblog.com/biao/aggbug/136772.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2010-12-17 17:51 <a href="http://www.cppblog.com/biao/archive/2010/12/17/136772.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C/C++：C中随机读写文件</title><link>http://www.cppblog.com/biao/archive/2010/12/17/136766.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Fri, 17 Dec 2010 09:48:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2010/12/17/136766.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/136766.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2010/12/17/136766.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/136766.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/136766.html</trackback:ping><description><![CDATA[<span  style="line-height: 15px; color: rgb(0, 134, 198); font-family: 'BBAlpha Sans', 'Prelude Medium', 'S60 Sans', Tahoma, Verdana, Arial, sans-serif; font-size: 12px; "><div style="line-height: 15px; ">#include &lt;iostream&gt;<br style="line-height: 15px; ">#include &lt;cstdio&gt;</div><div style="line-height: 15px; ">&nbsp;</div><div style="line-height: 15px; ">using namespace std;</div><div style="line-height: 15px; ">&nbsp;</div><div style="line-height: 15px; ">int main(int argc, char** argv) {<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FILE* fp = NULL;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fp = fopen("data.txt", "w");</div><div style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fputs("gooooooooogle", fp);<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fclose(fp);<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fp = NULL;</div><div style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fp = fopen("data.txt", "r+");</div><div style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#00ff00" style="line-height: normal; ">// 读取修改前的内容<br style="line-height: 15px; "></font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; char str[50];<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cout &lt;&lt; "------------修改前的内容---------" &lt;&lt; endl;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cout &lt;&lt; fgets(str, 50, fp) &lt;&lt; endl;</div><div style="line-height: 15px; ">&nbsp;</div><div style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#00ff00" style="line-height: normal; ">// 随机修改文件.<br style="line-height: 15px; "></font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rewind(fp);<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fseek(fp, 3L, SEEK_SET);<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //cout &lt;&lt; ftell(fp) &lt;&lt; endl;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fputc('A', fp);<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fputc('B', fp);<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fseek(fp, 1L, SEEK_CUR);<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fputc('C', fp);</div><div style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#00ff00" style="line-height: normal; ">// 读出修改后的内容<br style="line-height: 15px; "></font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rewind(fp);<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cout &lt;&lt; "------------修改后的内容---------" &lt;&lt; endl;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cout &lt;&lt; fgets(str, 50, fp) &lt;&lt; endl;</div><div style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fclose(fp);<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return 0;<br style="line-height: 15px; ">}</div><div style="line-height: 15px; ">&nbsp;</div><div style="line-height: 15px; "><font color="#00ff00" style="line-height: normal; ">/**<br style="line-height: 15px; ">&nbsp;* 不能用fopen(fileName, "w+"); // 如果文件不存在,创建文件,如果存在,则清空,然后读写<br style="line-height: 15px; ">&nbsp;* fopen(fileName, "a+"); // 只能写到文件尾,但可以读取.<br style="line-height: 15px; ">&nbsp;* fopen(fileName, "r+"); // 可以读写, 如果文件不存在, 则发生错误,可以实现随机读写.<br style="line-height: 15px; ">&nbsp;*/</font></div></span>
<img src ="http://www.cppblog.com/biao/aggbug/136766.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2010-12-17 17:48 <a href="http://www.cppblog.com/biao/archive/2010/12/17/136766.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C/C++：Overload + Template</title><link>http://www.cppblog.com/biao/archive/2010/12/17/136757.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Fri, 17 Dec 2010 09:30:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2010/12/17/136757.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/136757.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2010/12/17/136757.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/136757.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/136757.html</trackback:ping><description><![CDATA[<span  style="line-height: 15px; color: rgb(0, 134, 198); font-family: 'BBAlpha Sans', 'Prelude Medium', 'S60 Sans', Tahoma, Verdana, Arial, sans-serif; font-size: 12px; "><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; ">函数重载：</font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; ">函数名字相同，最少起码是参数的类型不一样，参数个数不一样那更好了，但是不能只是返回类型不一样。</font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; ">void func(int n) {...};</font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; ">void func(float n) {...};</font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; ">一起调用：</font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; ">func(10);</font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; ">func(20.001);</font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; ">这时编译不通过，提示重载函数的调用不清楚，因为第二个可能会把float强制转换为int，所以必须明确的强制指定为float才行，下面的调用编译通过：</font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; ">func(10);</font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; ">func((float)20.001); 或者 func(20.00f);</font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; "></font>&nbsp;</div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; ">重载函数通过签名(signature)进行识别，签名是函数名和参数类型的组合。</font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; ">编译器用函数参个数和类型编码每个函数标识符(或名字改编)，以保证类型安全连接。如Borland C++中，每个</font></div><div style="line-height: 15px; "><font color="#00ff00" style="line-height: normal; ">改编名用 @加上函数名</font></div><div style="line-height: 15px; "><font color="#00ff00" style="line-height: normal; ">改编参数以 $q 开头</font></div><div style="line-height: 15px; "><font color="#00ff00" style="line-height: normal; ">zc 表示 char, pf 为 float*, pd 为 double*, i 为 int, pi, f, d,</font></div><div style="line-height: 15px; "><font color="#00ff00" style="line-height: normal; ">如 void nothing(int a, float b, char c, double *d); 的改编名为</font></div><div style="line-height: 15px; "><font color="#00ff00" style="line-height: normal; ">@nothing$qifzcpd</font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; "></font>&nbsp;</div><div style="line-height: 15px; "><font color="#ffcc00" style="line-height: normal; ">重载函数通常用于不同类型用不同程序逻辑进行类似的操作。</font></div><div style="line-height: 15px; "><font color="#ffcc00" style="line-height: normal; ">如果每种数据类型的程序逻辑和操作相同，那么使用函数模板会更好一些。</font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; ">template &lt;class T&gt;</font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; ">T max(T a, T b, T c) {</font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; T max;</font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; max= a &gt; b? a : b;</font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; max = max &gt; c? max : c;</font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return max;</font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; ">}</font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; "></font>&nbsp;</div></span>
<img src ="http://www.cppblog.com/biao/aggbug/136757.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2010-12-17 17:30 <a href="http://www.cppblog.com/biao/archive/2010/12/17/136757.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C/C++：Array and Pointer</title><link>http://www.cppblog.com/biao/archive/2010/12/17/136756.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Fri, 17 Dec 2010 09:30:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2010/12/17/136756.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/136756.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2010/12/17/136756.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/136756.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/136756.html</trackback:ping><description><![CDATA[<span  style="line-height: 15px; color: rgb(0, 134, 198); font-family: 'BBAlpha Sans', 'Prelude Medium', 'S60 Sans', Tahoma, Verdana, Arial, sans-serif; font-size: 12px; "><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; ">数组和指针这东西有时还是比较麻烦：</font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; "><font color="#00ff00" style="line-height: normal; ">指针是很危险的，但同时也是非常强大的，</font>就如一个高手拿AWP和一个菜鸟拿AWP一样，一个是最恐怖的魔鬼，另一个却是被虐的对象。</font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; ">const int *p 和 int const *p 指的是数组的内容不能改变。</font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; ">int * const p 指的是指针名不能改变。谁在 const 后面，谁就是不变的。</font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; "></font>&nbsp;</div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; ">二维数组动态分配：</font></div><div style="line-height: 15px; "><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; ">int **p;</font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; ">int row = 5;</font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; ">int column = 10;</font></div><div style="line-height: 15px; "><font color="#00ff00" style="line-height: normal; ">p = new int*[row];</font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; ">for (int i=0; i&lt;row; i++) {</font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; p[i] = new int[column];</font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; ">}</font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; "></font>&nbsp;</div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; "><font color="#00ff00" style="line-height: normal; ">指针即是地址(0xf0765fea)，是无符号整数，占用四个字节，３２位。</font>怎么判断一个指针所指向的类型？</font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; ">去掉一个　*　和变量名后所留下的部分就是指针所指向的类型。</font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; ">如 int *p; 去掉一个 * 和变量名 p 后就只有 int 了，所以 p 指向 int 的整形数。</font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; ">float **p; 则 p 指向的类型是 float *，即 float 类型的指针，或 float 类型的地址，也即数组。</font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; ">那么在给 p 分配内存时，</font><font color="#dba04f" style="line-height: normal; ">p = new float*[23]，即指向一些</font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; ">在Ｃ或Ｃ＋＋中有一个很好的地方是，即使你定义的变量是指针，但可与用访问数组下标的方法来访问指针所指向的数据；int **p;&nbsp;&nbsp; *(*(p+i) + j) 等同于 p[i][j]，使用数组下标来访问更直观。</font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; "></font>&nbsp;</div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; ">使用变量来声明数组的长度，使程序更具有健壮性. int length = 10; int array[length].</font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; ">给定一个二维数组和一个位置数loc，怎么确定这个数对应数组中的那一个位置？</font></div><div style="line-height: 15px; "><font color="#00ff00" style="line-height: normal; ">array[loc / column][loc % column]</font></div><div style="line-height: 15px; "><font color="#00ff00" style="line-height: normal; "></font>&nbsp;</div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; ">当定义一个数组时，但没有初始化，这时数组的内容是还是原来内存中的值。</font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; ">&nbsp;</font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; ">void * 的使用</font></div><div style="line-height: 15px; "><font color="#00ff00" style="line-height: normal; ">可以把任何类型的指针直接传给 void* 的变量。</font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; ">但是反过来，却不能直接把 void* 传给其他类型指针，必须明确的进行强制类型转换</font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; ">int *pi;</font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; ">void * pv = pi; // Ok</font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; ">float *pf = (float*)pv; // Ok</font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; ">double *pd = pv; // Error</font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; "></font>&nbsp;</div><div style="line-height: 15px; "><font color="#00ff00" style="line-height: normal; ">char *str = (char*)malloc(sizeof(char) * number);</font></div><div style="line-height: 15px; "><font color="#00ff00" style="line-height: normal; ">malloc分配一段内存，返回这段内存的首地址，其值为 void *，这样，不管什么类型的指针都可以用这个函数来进行内存分配，void * 实现了相当于 C++ 中的函数模板的功能，实现了在Ｃ中的多态。各种类型说明符，就是为了说明每次进行存取的时候操作多少字节。</font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; "></font>&nbsp;</div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; ">数组名是这个数组的首地址，与他的第一个元素的地址是指向同一个地址，但数组却是一个常量，只能进行右值计算，是不可改变的。指针变量也是指向地址，但指针变量是可以改变的，可以进行右值计算，也可以左值计算。</font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; "></font>&nbsp;</div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; ">字符数组的结束符号是'\0'，其整型值是 0，即 NULL</font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; ">虽然这样，但在OpenGL中，如在 glGenTextures 的调用中，经常用的是第一个元素的地址，而不是用数组名。</font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; "></font>&nbsp;</div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; ">用数组实现折半查找：</font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; ">在最坏的情况下，1024个元素只需要进行10查找，但查找前要先排序</font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; ">返回要查找的数在数组的下标，当不在数组中是，返回 -1，因为数组下标是从０开始的。</font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; ">int binarySearch(const int *p, const int g, int low, int height) {<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int mid;<br style="line-height: 15px; ">&nbsp;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while (low &lt;= height) {<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mid = (low + height) / 2;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; if (g &lt; p[mid]) {<br style="line-height: 15px; ">&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; height = mid - 1;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} else if (g &gt; p[mid]) {<br style="line-height: 15px; ">&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; low = mid + 1;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } else {<br style="line-height: 15px; ">&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; return mid;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br style="line-height: 15px; ">&nbsp;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return -1;<br style="line-height: 15px; ">}</font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; "></font>&nbsp;</div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; ">快速排序：</font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; ">void quickSort(int *p, int low, int height) {<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (low &gt;= height) {<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br style="line-height: 15px; ">&nbsp;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; const int l = low;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; const int h = height;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int ref = p[(low + height)/2];<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int temp = 0;</font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; "></font>&nbsp;</div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#00ff00" style="line-height: normal; ">&nbsp;// 如果没有等于这个条件，那么如　3, 2, 1时，就会造成下一次排序为1,2 ; 2, 3，多出来一个 2</font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while (low &lt;= height) {</font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#00ff00" style="line-height: normal; ">&nbsp;//&nbsp;找到大于等于 ref 的数，这里很重要，如果用 &lt;= 的话，即找到比 ref 大的数，</font></font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; "><font color="#00ff00" style="line-height: normal; ">　&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 可能会造成数组越界<br style="line-height: 15px; "></font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while (p[low] &lt; ref) {&nbsp;<br style="line-height: 15px; ">&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; low++;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while (p[height] &gt; ref) {<br style="line-height: 15px; ">&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; height--;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br style="line-height: 15px; ">&nbsp;&nbsp;</font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#00ff00" style="line-height: normal; ">// 这个条件是必须的，否则不能正确排序，　l１，３，４，２，２，３，h５, 3为ref</font></font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; "><font color="#00ff00" style="line-height: normal; "><font color="#dba04f" style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font></font></font><font color="#00ff00" style="line-height: normal; ">&nbsp;&nbsp;// 第一次排序后，1, 3, l4, 2,&nbsp;h2, 3, 5</font></div><div style="line-height: 15px; "><font color="#00ff00" style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// 第二次排序后，1, 3, 2, lh2, 4, 3, 5，这时还可以进行下一次排序</font></div><div style="line-height: 15px; "><font color="#00ff00" style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;第三次排序，h仍在2处，但l要找大于等于他的数，即1, 3, 2, h2, l4, 3, 5</font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; "><font color="#00ff00" style="line-height: normal; "><font color="#00ff00" style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// 如果没有这个条件，那么就要进行交换，1, 3, h2, 4, 2, l3, 5，有错误发生</font><br style="line-height: 15px; "></font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (low &lt;= height) {<br style="line-height: 15px; ">&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; temp = p[low];<br style="line-height: 15px; ">&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; p[low] = p[height];<br style="line-height: 15px; ">&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; p[height] = temp;<br style="line-height: 15px; ">&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; low++;<br style="line-height: 15px; ">&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; height--;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br style="line-height: 15px; ">&nbsp;&nbsp;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br style="line-height: 15px; ">&nbsp;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; quickSort(p, low, h);<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; quickSort(p, l, height);</font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#00ff00" style="line-height: normal; ">// 第一次压入(low, height)</font></div><div style="line-height: 15px; "><font color="#00ff00" style="line-height: normal; ">　　　&nbsp; // 从栈中取出一对(low, height)进行每个片段的排序</font></div><div style="line-height: 15px; "><font color="#00ff00" style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 每排完一遍后，即上面的while执行完一次，压入(low,h)和(l,height)</font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; "><font color="#00ff00" style="line-height: normal; ">　　　&nbsp; //&nbsp;利用栈消除递归</font><br style="line-height: 15px; ">&nbsp;<br style="line-height: 15px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return;<br style="line-height: 15px; ">}</font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; "></font>&nbsp;</div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; ">排序一般当元素个数大于三十个时利用快速排序的速度比较快，当元素个数小于三十个时，利用选择排序比较好，所以一般都是选择排序和快速排序结合起来使用。</font></div></div></span>
<img src ="http://www.cppblog.com/biao/aggbug/136756.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2010-12-17 17:30 <a href="http://www.cppblog.com/biao/archive/2010/12/17/136756.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C/C++：函数指针</title><link>http://www.cppblog.com/biao/archive/2010/12/17/136754.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Fri, 17 Dec 2010 09:27:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2010/12/17/136754.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/136754.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2010/12/17/136754.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/136754.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/136754.html</trackback:ping><description><![CDATA[<span  style="line-height: 15px; color: rgb(0, 134, 198); font-family: 'BBAlpha Sans', 'Prelude Medium', 'S60 Sans', Tahoma, Verdana, Arial, sans-serif; font-size: 12px; "><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; ">函数指针：</font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; ">函数名就是函数操作代码在内存中的起始地址，就像数组名是数组在内存中的起始地址。</font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; ">所以使用函数指针时，直接传入函数名就什么了。函数指针也是Ｃ中实现多态的一种方法。</font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; "></font>&nbsp;</div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; ">int x2(int x) { return x * x;}</font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; ">int x3(int x) {return x * x * x;}</font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; ">void func(int (*pf)(int), int x) { cout &lt;&lt; pf(x) &lt;&lt; endl;}</font></div><div style="line-height: 15px; "><font color="#00ff00" style="line-height: normal; ">== void func(int (*pf)(int), int x) { cout &lt;&lt; (*pf)(x) &lt;&lt; endl;}</font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; ">func(x2, 4); // 16</font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; ">func(x3, 4); // 64</font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; "></font>&nbsp;</div><div style="line-height: 15px; "><font color="#00ff00" style="line-height: normal; "><font color="#ff9900" style="line-height: normal; ">void *func(int* (*pf)(int*, const int**, float, char * const), double *);</font>&nbsp;// 呵呵，头晕吧</font></div><div style="line-height: 15px; "><font color="#00ff00" style="line-height: normal; "></font>&nbsp;</div><div style="line-height: 15px; "><font color="#00ff00" style="line-height: normal; "><font color="#ff9900" style="line-height: normal; ">void (*pf[10])(int);</font>&nbsp;// 表示pf是10个函数指针的数组</font></div><div style="line-height: 15px; "><font color="#00ff00" style="line-height: normal; ">函数指针的一个用法是建立一个菜单驱动程序：</font></div><div style="line-height: 15px; "><font color="#00ff00" style="line-height: normal; ">用户从菜单中选择一个选项，每个选项由不同的函数提供服务，每个函数的指针存放在函数指针数组中。用户选项作为数组下标，数组中的指针用于调用这个函数。</font></div><div style="line-height: 15px; "><font color="#ff9900" style="line-height: normal; ">void （*pf[3])(int) = {func1, func2, func3};</font></div></span><img src ="http://www.cppblog.com/biao/aggbug/136754.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2010-12-17 17:27 <a href="http://www.cppblog.com/biao/archive/2010/12/17/136754.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C：在终端里输出正玄余玄</title><link>http://www.cppblog.com/biao/archive/2010/11/07/132831.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Sat, 06 Nov 2010 22:40:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2010/11/07/132831.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/132831.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2010/11/07/132831.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/132831.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/132831.html</trackback:ping><description><![CDATA[

因为在终端里输出，只能往前，往下输出的特性，所以要移用对称性来实现输出正玄，余玄图案。
<div><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #5000ff"><span style="color: #a1006a">#include</span><span style="color: #000000"> </span>&lt;stdio.h&gt;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #a1006a">#include<span style="color: #000000"> </span><span style="color: #5000ff">&lt;math.h&gt;</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; min-height: 19.0px"><br></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco"><span style="color: #a1006a">int</span> main() {</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">&nbsp; &nbsp; <span style="color: #a1006a">double</span> y;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">&nbsp; &nbsp; <span style="color: #a1006a">int</span> x, f, i;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; min-height: 19.0px">&nbsp;&nbsp; &nbsp;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #2f9570"><span style="color: #000000">&nbsp; &nbsp; </span>/* 输出正玄, 单调区间 x: [-pi/2, pi/2], y: [-1, 1] */</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #2f9570"><span style="color: #000000">&nbsp; &nbsp; </span>/* 要分成两个部分来计算输出 */</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">&nbsp; &nbsp; <span style="color: #a1006a">for</span> (y = 1; y &gt; 0; y -= 0.1) {</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">&nbsp; &nbsp; &nbsp; &nbsp; x = asin(y) * 10;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; min-height: 19.0px">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a1006a">for</span> (i = 1; i &lt; x; ++i) printf(<span style="color: #5000ff">" "</span>);</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">&nbsp; &nbsp; &nbsp; &nbsp; printf(<span style="color: #5000ff">"*"</span>);</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a1006a">for</span> (; i &lt; 32 - x; ++i) printf(<span style="color: #5000ff">" "</span>);</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">&nbsp; &nbsp; &nbsp; &nbsp; printf(<span style="color: #5000ff">"*\n"</span>);</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">&nbsp; &nbsp; }</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; min-height: 19.0px">&nbsp;&nbsp; &nbsp;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">&nbsp; &nbsp; <span style="color: #a1006a">for</span> (y = 0; y &gt;= -1; y -= 0.1) {</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">&nbsp; &nbsp; &nbsp; &nbsp; x = asin(y) * 10; <span style="color: #2f9570">/* x是负值 */</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; min-height: 19.0px">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a1006a">for</span> (i = 1; i &lt; 34 - x; ++i) printf(<span style="color: #5000ff">" "</span>);</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">&nbsp; &nbsp; &nbsp; &nbsp; printf(<span style="color: #5000ff">"*"</span>);</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a1006a">for</span> (; i &lt; 66 + x; ++i) printf(<span style="color: #5000ff">" "</span>);</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">&nbsp; &nbsp; &nbsp; &nbsp; printf(<span style="color: #5000ff">"*\n"</span>);</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">&nbsp; &nbsp; }</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; min-height: 19.0px">&nbsp;&nbsp; &nbsp;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">&nbsp; &nbsp; printf(<span style="color: #5000ff">"\n\n"</span>);</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; min-height: 19.0px">&nbsp;&nbsp; &nbsp;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #2f9570"><span style="color: #000000">&nbsp; &nbsp; </span>/* 输出余玄, 单调区间 x: [0, pi], y: [1, -1] */</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">&nbsp; &nbsp; <span style="color: #a1006a">for</span> (y = 1; y &gt;= -1; y -= 0.1) {</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">&nbsp; &nbsp; &nbsp; &nbsp; x = (<span style="color: #a1006a">int</span>)(acos(y) * 10);</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; min-height: 19.0px">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a1006a">for</span> (i = 0; i &lt;= 64; ++i) {</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a1006a">if</span> (i == x || i == 64 - x) {</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf(<span style="color: #5000ff">"*"</span>);</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } <span style="color: #a1006a">else</span> {</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf(<span style="color: #5000ff">" "</span>);</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">&nbsp; &nbsp; &nbsp; &nbsp; }</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">&nbsp; &nbsp; &nbsp; &nbsp; printf(<span style="color: #5000ff">"\n"</span>);</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">&nbsp; &nbsp; }</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">}</p></div><div><br></div><img src ="http://www.cppblog.com/biao/aggbug/132831.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2010-11-07 06:40 <a href="http://www.cppblog.com/biao/archive/2010/11/07/132831.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C++：Snow Leopard里安装OpenCV 2.1 (折磨人的东西)</title><link>http://www.cppblog.com/biao/archive/2010/10/30/131849.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Sat, 30 Oct 2010 08:05:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2010/10/30/131849.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/131849.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2010/10/30/131849.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/131849.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/131849.html</trackback:ping><description><![CDATA[
<div>http://sites.google.com/site/michaelsafyan/setup-guide/mac-os-x-10-6-snow-leopard</div><strong>1. 安装MacPort：&nbsp;下载</strong><a href="http://distfiles.macports.org/MacPorts/MacPorts-1.8.2-10.6-SnowLeopard.dmg"><strong>Download MacPorts</strong></a><strong>的dmg，直接安装</strong><div><strong>2.&nbsp;</strong><span style="color: rgb(68, 68, 68); font-family: Arial, Verdana, sans-serif; font-size: 13px; line-height: 20px; "><strong>Install PkgConfig using MacPorts:&nbsp;</strong></span><span style="color: rgb(0, 96, 0); font-family: monospace; font-size: 13px; line-height: 20px; "><strong>sudo port install pkgconfig</strong></span></div><div><span style="color: rgb(0, 96, 0); font-family: monospace; font-size: 13px; line-height: 20px; "><strong>3.&nbsp;</strong></span><span style="color: rgb(68, 68, 68); font-family: Arial, Verdana, sans-serif; font-size: 13px; line-height: 20px; "><strong>Install CMake using MacPorts::&nbsp;</strong></span><span style="color: rgb(0, 96, 0); font-family: monospace; font-size: 13px; line-height: 20px; "><strong>sudo port install cmake</strong></span></div><div><span style="color: rgb(0, 96, 0); font-family: monospace; font-size: 13px; line-height: 20px; "><strong>4.&nbsp;</strong></span><span style="color: rgb(68, 68, 68); font-family: Arial, Verdana, sans-serif; font-size: 13px; line-height: 20px; "><strong>Configure PKG_CONFIG_PATH:&nbsp;</strong></span></div><div><span style="color: rgb(68, 68, 68); font-family: Arial, Verdana, sans-serif; font-size: 13px; line-height: 20px; "></span><span style="color: rgb(0, 96, 0); font-family: monospace; font-size: 13px; line-height: 20px; "><span class="Apple-tab-span" style="white-space:pre">	</span>export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:/usr/lib/pkgconfig:/usr/X11/lib/pkgconfig</span></div><div><span style="color: rgb(0, 96, 0); font-family: monospace; font-size: 13px; line-height: 20px; "><span class="Apple-tab-span" style="white-space:pre">	</span></span><span style="color: rgb(0, 96, 0); font-family: monospace; font-size: 13px; line-height: 20px; ">export PKG_CONFIG_PATH=~/.local/lib/pkgconfig:/opt/local/lib/pkgconfig:$PKG_CONFIG_PATH</span></div><div><span style="color: rgb(0, 96, 0); font-family: monospace; font-size: 13px; line-height: 20px; "><span class="Apple-tab-span" style="white-space:pre">	</span></span><span style="color: rgb(0, 96, 0); font-family: monospace; font-size: 13px; line-height: 20px; ">echo "export PKG_CONFIG_PATH=$PKG_CONFIG_PATH" &gt;&gt; ~/.profile</span></div><div><font color="#006000" face="monospace" size="3"><span style="font-size: 13px; line-height: 20px;"><strong>在安装cmake时要注意，因为在Snow Leopard 10.6.4中因为JDK已经升级过了，在</strong></span></font><span style="color: rgb(0, 96, 0); font-family: monospace; font-size: 13px; line-height: 20px; "><strong>sudo port install cmake时会提示错误，解决方法：</strong></span></div><div><span style="color: rgb(0, 96, 0); font-family: monospace; font-size: 13px; line-height: 20px; "><span style="color: rgb(0, 0, 0); font-family: 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif; line-height: normal; "><p>I am in a different position with respect to this fix. I had installed&nbsp;<span class="searchword1" style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(204, 255, 204); background-position: initial initial; background-repeat: initial initial; ">cmake</span>&nbsp;2.8.2_3 before the Apple Java update. After the Apple Java update, the cmake upgrade from 2.8.2_3 to 2.8.2_4 failed saying that I needed to install Java from Apple, which was already installed. As far as I can tell, the Apple update to Java 1.6.0_22 did not properly update the header path. In the updated version the header path leads to a dead end. I was able to fix this by doing the following.</p><p>1) Create directory</p><p>/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Headers</p><p>2) Copy entire contents of directory</p><p>/Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Headers</p><p>to directory</p><p>/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Headers</p><p>After this fix the upgrade to&nbsp;<span class="searchword1" style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(204, 255, 204); background-position: initial initial; background-repeat: initial initial; ">cmake</span>&nbsp;2.8.2_4 worked. However, if the&nbsp;<span class="searchword0" style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(255, 255, 153); background-position: initial initial; background-repeat: initial initial; ">macports</span>&nbsp;cmake could be made to look for the Java headers in /Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Headers this would resolve an existing problem.</p><p>From:&nbsp;https://trac.macports.org/ticket/26934</p><p><br></p><p><strong>5. 最终大戏来了，安装OpenCV:&nbsp;http://sunny.in.th/2010/04/27/installing-opencv-21-on-snow-leopard.html</strong></p><p><span style="font-family: Calibri, Tahoma, Verdana, sans-serif; font-size: 17px; color: rgb(51, 51, 51); line-height: 23px; "></span></p><p>Installing OpenCV 2.1 on Snow Leopard has become quite easy at least easier than 2.0. Version 2.1 can now be compiled as 64-bit library.</p><p>Before version 2.1 OpenCV used Carbon which is the old GUI interface for Mac OS. Carbon supports only 32-bit and has been deprecated by Apple. The new GUI interface for Mac OS is Cocoa. With Snow Leopard being a 64-bit kernel it would be nice if we could compile OpenCV as a 64-bit library and not not 32-bit just because of Carbon. Thanks to the OpenCV team they have added Cocoa support in version 2.1.</p><p>To install OpenCV check out the code from the svn repository and compile it.</p><div class="highlight" style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(255, 255, 255); background-position: initial initial; background-repeat: initial initial; "><pre style="font-family: 'andale mono', 'lucida console', monospace; "><code class="bash" style="font-family: 'andale mono', 'lucida console', monospace; "><span class="nv" style="color: rgb(0, 128, 128); ">$ </span>svn co https://code.ros.org/svn/opencv/trunk/opencv
<span class="nv" style="color: rgb(0, 128, 128); ">$ </span><span class="nb" style="color: rgb(0, 134, 179); ">cd </span>opencv
<span class="nv" style="color: rgb(0, 128, 128); ">$ </span>mkdir build 
<span class="nv" style="color: rgb(0, 128, 128); ">$ </span><span class="nb" style="color: rgb(0, 134, 179); ">cd </span>build 
<span class="nv" style="color: rgb(0, 128, 128); ">$ </span>cmake .. 
</code></pre></div><p>Configure the make by</p><div class="highlight" style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(255, 255, 255); background-position: initial initial; background-repeat: initial initial; "><pre style="font-family: 'andale mono', 'lucida console', monospace; "><code class="bash" style="font-family: 'andale mono', 'lucida console', monospace; "><span class="nv" style="color: rgb(0, 128, 128); ">$ </span>ccmake .
</code></pre></div><p>If you want you can build the samples as well.</p><p><img src="http://img.skitch.com/20100429-gpdgt6jp32qbt4tu6ws4xnnfq9.jpg" alt="ccmake"></p><p>Press&nbsp;<code style="font-family: 'andale mono', 'lucida console', monospace; ">c</code>&nbsp;to configure, followed by&nbsp;<code style="font-family: 'andale mono', 'lucida console', monospace; ">g</code>&nbsp;to generate. Next build and install OpenCV by</p><div class="highlight" style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(255, 255, 255); background-position: initial initial; background-repeat: initial initial; "><pre style="font-family: 'andale mono', 'lucida console', monospace; "><code class="bash" style="font-family: 'andale mono', 'lucida console', monospace; "><span class="nv" style="color: rgb(0, 128, 128); ">$ </span>make -j8
<span class="nv" style="color: rgb(0, 128, 128); ">$ </span>sudo make install
</code></pre></div><p>That&#8217;s it!!! Simple right.</p><p>If you want to use Python with OpenCV there&#8217;s a little more that needs to be done. By default OpenCV copies the shared object file required by the Python interface to&nbsp;<code style="font-family: 'andale mono', 'lucida console', monospace; ">/usr/local/lib/python2.6/site-packages/cv.so</code>&nbsp;which is not under Python path. Add the following line to your&nbsp;<code style="font-family: 'andale mono', 'lucida console', monospace; ">.bashrc</code>&nbsp;or&nbsp;<code style="font-family: 'andale mono', 'lucida console', monospace; ">.bash_profile</code></p><div class="highlight" style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(255, 255, 255); background-position: initial initial; background-repeat: initial initial; "><pre style="font-family: 'andale mono', 'lucida console', monospace; "><code class="bash" style="font-family: 'andale mono', 'lucida console', monospace; "><span class="nv" style="color: rgb(0, 128, 128); ">PYTHONPATH</span><span class="o" style="font-weight: bold; ">=</span>/usr/local/lib/python2.6/site-packages/cv.so:<span class="nv" style="color: rgb(0, 128, 128); ">$PYTHONPATH</span>
</code></pre></div><p>Trying running some&nbsp;<code style="font-family: 'andale mono', 'lucida console', monospace; ">C</code>&nbsp;samples which are in&nbsp;<code style="font-family: 'andale mono', 'lucida console', monospace; ">/usr/local/share/opencv/samples/c/</code>. The Python samples at<code style="font-family: 'andale mono', 'lucida console', monospace; ">/usr/local/share/opencv/samples/python/</code>&nbsp;doesn&#8217;t work since they are based on the old SWIG-Python interface. Try running the sample Python code which is in the&nbsp;<code style="font-family: 'andale mono', 'lucida console', monospace; ">opencv</code>&nbsp;directory that was checked out from subversion. It&#8217;s inside<code style="font-family: 'andale mono', 'lucida console', monospace; ">samples/python</code>.</p></span><p>&#160;</p></span></div><img src ="http://www.cppblog.com/biao/aggbug/131849.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2010-10-30 16:05 <a href="http://www.cppblog.com/biao/archive/2010/10/30/131849.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C++：Type Casting</title><link>http://www.cppblog.com/biao/archive/2010/10/08/128986.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Thu, 07 Oct 2010 21:25:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2010/10/08/128986.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/128986.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2010/10/08/128986.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/128986.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/128986.html</trackback:ping><description><![CDATA[
<span style="font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; ">Converting an expression of a given type into another type is known as</span><span style="font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; ">&nbsp;</span><span style="font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; "><em style="font-style: italic; ">type-casting</em></span><span style="font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; ">. We have already seen some ways to type cast:</span><h3 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-family: verdana, sans-serif; font-size: 14px; line-height: 21px; ">Implicit conversion</h3><span style="font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; ">Implicit conversions do not require any operator. They are automatically performed when a value is copied to a compatible type. For example:</span><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; "></p><table class="snippet" style="font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; "><tbody><tr><td class="code"><pre><span class="kw"><font color="#0000b0">short</font></span> a=2000;
<span class="kw"><font color="#0000b0">int</font></span> b;
b=a;
</pre></td></tr></tbody></table><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; "></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; ">Here, the value of&nbsp;<tt>a</tt>&nbsp;has been promoted from&nbsp;<tt>short</tt>&nbsp;to&nbsp;<tt>int</tt>&nbsp;and we have not had to specify any type-casting operator. This is known as a standard conversion. Standard conversions affect fundamental data types, and allow conversions such as the conversions between numerical types (<tt>short</tt>&nbsp;to&nbsp;<tt>int</tt>,&nbsp;<tt>int</tt>&nbsp;to&nbsp;<tt>float</tt>,&nbsp;<tt>double</tt>&nbsp;to&nbsp;<tt>int</tt>...), to or from&nbsp;<tt>bool</tt>, and some pointer conversions. Some of these conversions may imply a loss of precision, which the compiler can signal with a warning. This can be avoided with an explicit conversion.</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; ">Implicit conversions also include constructor or operator conversions, which affect classes that include specific constructors or operator functions to perform conversions. For example:</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; "></p><table class="snippet" style="font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; "><tbody><tr><td class="code"><pre><span class="kw"><font color="#0000b0">class</font></span> A {};
<span class="kw"><font color="#0000b0">class</font></span> B { <span class="kw"><font color="#0000b0">public</font></span>: B (A a) {} };

A a;
B b=a;
</pre></td></tr></tbody></table><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; "></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; ">Here, a implicit conversion happened between objects of&nbsp;<tt>class A</tt>&nbsp;and&nbsp;<tt>class B</tt>, because&nbsp;<tt>B</tt>&nbsp;has a constructor that takes an object of class&nbsp;<tt>A</tt>&nbsp;as parameter. Therefore implicit conversions from&nbsp;<tt>A</tt>&nbsp;to&nbsp;<tt>B</tt>&nbsp;are allowed.</p><h3 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-family: verdana, sans-serif; font-size: 14px; line-height: 21px; ">Explicit conversion</h3><span style="font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; ">C++ is a strong-typed language. Many conversions, specially those that imply a different interpretation of the value, require an explicit conversion. We have already seen two notations for explicit type conversion: functional and c-like casting:</span><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; "></p><table class="snippet" style="font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; "><tbody><tr><td class="code"><pre><span class="kw"><font color="#0000b0">short</font></span> a=2000;
<span class="kw"><font color="#0000b0">int</font></span> b;
b = (<span class="kw"><font color="#0000b0">int</font></span>) a;    <span class="comm"><font color="#007000">// c-like cast notation</font></span>
b = <span class="kw"><font color="#0000b0">int</font></span> (a);    <span class="comm"><font color="#007000">// functional notation</font></span>
</pre></td></tr></tbody></table><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; "></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; ">The functionality of these explicit conversion operators is enough for most needs with fundamental data types. However, these operators can be applied indiscriminately on classes and pointers to classes, which can lead to code that while being syntactically correct can cause runtime errors. For example, the following code is syntactically correct:</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; "></p><table class="codebox" style="font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; "><tbody><tr><td class="code"><pre><span class="comm"><font color="#007000">// class type-casting</font></span>
<span class="prep"><font color="#500070">#include &lt;iostream&gt;</font></span>
<span class="kw"><font color="#0000b0">using</font></span> <span class="kw"><font color="#0000b0">namespace</font></span> std;

<span class="kw"><font color="#0000b0">class</font></span> CDummy {
    <span class="kw"><font color="#0000b0">float</font></span> i,j;
};

<span class="kw"><font color="#0000b0">class</font></span> CAddition {
	<span class="kw"><font color="#0000b0">int</font></span> x,y;
  <span class="kw"><font color="#0000b0">public</font></span>:
	CAddition (<span class="kw"><font color="#0000b0">int</font></span> a, <span class="kw"><font color="#0000b0">int</font></span> b) { x=a; y=b; }
	<span class="kw"><font color="#0000b0">int</font></span> result() { <span class="kw"><font color="#0000b0">return</font></span> x+y;}
};

<span class="kw"><font color="#0000b0">int</font></span> main () {
  CDummy d;
  CAddition * padd;
  padd = (CAddition*) &amp;d;
  cout &lt;&lt; padd-&gt;result();
  <span class="kw"><font color="#0000b0">return</font></span> 0;
}
</pre></td><td class="result"><pre> 
</pre></td></tr></tbody></table><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; "></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; ">The program declares a pointer to&nbsp;<tt>CAddition</tt>, but then it assigns to it a reference to an object of another incompatible type using explicit type-casting:</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; "></p><table class="snippet" style="font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; "><tbody><tr><td class="code"><pre>padd = (CAddition*) &amp;d;
</pre></td></tr></tbody></table><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; "></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; ">Traditional explicit type-casting allows to convert any pointer into any other pointer type, independently of the types they point to. The subsequent call to member&nbsp;<tt>result</tt>&nbsp;will produce either a run-time error or a unexpected result.</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; ">In order to control these types of conversions between classes, we have four specific casting operators:&nbsp;<tt>dynamic_cast</tt>,&nbsp;<tt>reinterpret_cast</tt>,&nbsp;<tt>static_cast</tt>&nbsp;and&nbsp;<tt>const_cast</tt>. Their format is to follow the new type enclosed between angle-brackets (<tt>&lt;&gt;</tt>) and immediately after, the expression to be converted between parentheses.</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; "><tt>dynamic_cast &lt;new_type&gt; (expression)<br>reinterpret_cast &lt;new_type&gt; (expression)<br>static_cast &lt;new_type&gt; (expression)<br>const_cast &lt;new_type&gt; (expression)<br></tt></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; ">The traditional type-casting equivalents to these expressions would be:<br><br><tt>(new_type) expression<br>new_type (expression)<br></tt></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; ">but each one with its own special characteristics:</p><h3 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-family: verdana, sans-serif; font-size: 14px; line-height: 21px; ">dynamic_cast: 转换子类的指针(或引用)为父类的指针(或引用)</h3><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; "><tt>dynamic_cast</tt>&nbsp;can be used only with pointers and references to objects. Its purpose is to ensure that the result of the type conversion is a valid complete object of the requested class.</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; ">Therefore,&nbsp;<tt>dynamic_cast</tt>&nbsp;is always successful when we cast a class to one of its base classes:</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; "></p><table class="snippet" style="font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; "><tbody><tr><td class="code"><pre><span class="kw"><font color="#0000b0">class</font></span> CBase { };
<span class="kw"><font color="#0000b0">class</font></span> CDerived: <span class="kw"><font color="#0000b0">public</font></span> CBase { };

CBase b; CBase* pb;
CDerived d; CDerived* pd;

pb = <span class="kw"><font color="#0000b0">dynamic_cast</font></span>&lt;CBase*&gt;(&amp;d);     <span class="comm"><font color="#007000">// ok: derived-to-base</font></span>
pd = <span class="kw"><font color="#0000b0">dynamic_cast</font></span>&lt;CDerived*&gt;(&amp;b);  <span class="comm"><font color="#007000">// wrong: base-to-derived</font></span>
</pre></td></tr></tbody></table><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; "></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; ">The second conversion in this piece of code would produce a compilation error since base-to-derived conversions are not allowed with&nbsp;<tt>dynamic_cast</tt>&nbsp;unless the base class is polymorphic.</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; ">When a class is polymorphic,&nbsp;<tt>dynamic_cast</tt>&nbsp;performs a special checking during runtime to ensure that the expression yields a valid complete object of the requested class:</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; "></p><table class="codebox" style="font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; "><tbody><tr><td class="code"><pre><span class="comm"><font color="#007000">// dynamic_cast</font></span>
<span class="prep"><font color="#500070">#include &lt;iostream&gt;</font></span>
<span class="prep"><font color="#500070">#include &lt;exception&gt;</font></span>
<span class="kw"><font color="#0000b0">using</font></span> <span class="kw"><font color="#0000b0">namespace</font></span> std;

<span class="kw"><font color="#0000b0">class</font></span> CBase { <span class="kw"><font color="#0000b0">virtual</font></span> <span class="kw"><font color="#0000b0">void</font></span> dummy() {} };
<span class="kw"><font color="#0000b0">class</font></span> CDerived: <span class="kw"><font color="#0000b0">public</font></span> CBase { <span class="kw"><font color="#0000b0">int</font></span> a; };

<span class="kw"><font color="#0000b0">int</font></span> main () {
  <span class="kw"><font color="#0000b0">try</font></span> {
    CBase * pba = <span class="kw"><font color="#0000b0">new</font></span> CDerived;
    CBase * pbb = <span class="kw"><font color="#0000b0">new</font></span> CBase;
    CDerived * pd;

    pd = <span class="kw"><font color="#0000b0">dynamic_cast</font></span>&lt;CDerived*&gt;(pba);
    <span class="kw"><font color="#0000b0">if</font></span> (pd==0) cout &lt;&lt; <span class="str"><font color="#600030">"Null pointer on first type-cast"</font></span> &lt;&lt; endl;

    pd = <span class="kw"><font color="#0000b0">dynamic_cast</font></span>&lt;CDerived*&gt;(pbb);
    <span class="kw"><font color="#0000b0">if</font></span> (pd==0) cout &lt;&lt; <span class="str"><font color="#600030">"Null pointer on second type-cast"</font></span> &lt;&lt; endl;

  } <span class="kw"><font color="#0000b0">catch</font></span> (exception&amp; e) {cout &lt;&lt; <span class="str"><font color="#600030">"Exception: "</font></span> &lt;&lt; e.what();}
  <span class="kw"><font color="#0000b0">return</font></span> 0;
}
</pre></td><td class="result"><pre>Null pointer on second type-cast
</pre></td></tr></tbody></table><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; "></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; "></p><table class="boxed" style="font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; "><tbody><tr><td><strong>Compatibility note:</strong>&nbsp;<tt>dynamic_cast</tt>&nbsp;requires the Run-Time Type Information (RTTI) to keep track of dynamic types. Some compilers support this feature as an option which is disabled by default. This must be enabled for runtime type checking using&nbsp;<tt>dynamic_cast</tt>&nbsp;to work properly.<br></td></tr></tbody></table><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; "></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; ">The code tries to perform two dynamic casts from pointer objects of type&nbsp;<tt>CBase*</tt>&nbsp;(<tt>pba</tt>&nbsp;and&nbsp;<tt>pbb</tt>) to a pointer object of type&nbsp;<tt>CDerived*</tt>, but only the first one is successful. Notice their respective initializations:</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; "></p><table class="snippet" style="font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; "><tbody><tr><td class="code"><pre>CBase * pba = <span class="kw"><font color="#0000b0">new</font></span> CDerived;
CBase * pbb = <span class="kw"><font color="#0000b0">new</font></span> CBase;
</pre></td></tr></tbody></table><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; "></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; ">Even though both are pointers of type&nbsp;<tt>CBase*</tt>,&nbsp;<tt>pba</tt>&nbsp;points to an object of type&nbsp;<tt>CDerived</tt>, while&nbsp;<tt>pbb</tt>&nbsp;points to an object of type&nbsp;<tt>CBase</tt>. Thus, when their respective type-castings are performed using&nbsp;<tt>dynamic_cast</tt>,&nbsp;<tt>pba</tt>&nbsp;is pointing to a full object of class&nbsp;<tt>CDerived</tt>, whereas&nbsp;<tt>pbb</tt>&nbsp;is pointing to an object of class&nbsp;<tt>CBase</tt>, which is an incomplete object of class&nbsp;<tt>CDerived</tt>.<br><br>When&nbsp;<tt>dynamic_cast</tt>&nbsp;cannot cast a pointer because it is not a complete object of the required class -as in the second conversion in the previous example- it returns a null pointer to indicate the failure. If&nbsp;<tt>dynamic_cast</tt>&nbsp;is used to convert to a reference type and the conversion is not possible, an exception of type&nbsp;<tt>bad_cast</tt>&nbsp;is thrown instead.</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; "><tt>dynamic_cast</tt>&nbsp;can also cast null pointers even between pointers to unrelated classes, and can also cast pointers of any type to void pointers (<tt>void*</tt>).</p><h3 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-family: verdana, sans-serif; font-size: 14px; line-height: 21px; ">static_cast: 指针的转换: 1. 子类和父类之间指针互相转换(不进行安全检查). 非指针的转换: 2. 标准隐式转换(如int-&gt;float, double-&gt;int). 3. 用户定义转换(构造函数转换，转换函数)</h3><span style="font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; "><tt>static_cast</tt></span><span style="font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; ">&nbsp;</span><span style="font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; ">can perform conversions between pointers to related classes, not only from the derived class to its base, but also from a base class to its derived. This ensures that at least the classes are compatible if the proper object is converted, but no safety check is performed during runtime to check if the object being converted is in fact a full object of the destination type. Therefore, it is up to the programmer to ensure that the conversion is safe. On the other side, the overhead of the type-safety checks of</span><span style="font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; ">&nbsp;</span><span style="font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; "><tt>dynamic_cast</tt></span><span style="font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; ">&nbsp;</span><span style="font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; ">is avoided.</span><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; "></p><table class="snippet" style="font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; "><tbody><tr><td class="code"><pre><span class="kw"><font color="#0000b0">class</font></span> CBase {};
<span class="kw"><font color="#0000b0">class</font></span> CDerived: <span class="kw"><font color="#0000b0">public</font></span> CBase {};
CBase * a = <span class="kw"><font color="#0000b0">new</font></span> CBase;
CDerived * b = <span class="kw"><font color="#0000b0">static_cast</font></span>&lt;CDerived*&gt;(a);
</pre></td></tr></tbody></table><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; "></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; ">This would be valid, although&nbsp;<tt>b</tt>&nbsp;would point to an incomplete object of the class and could lead to runtime errors if dereferenced.</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; "><tt>static_cast</tt>&nbsp;can also be used to perform any other non-pointer conversion that could also be performed implicitly, like for example standard conversion between fundamental types:</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; "></p><table class="snippet" style="font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; "><tbody><tr><td class="code"><pre><span class="kw"><font color="#0000b0">double</font></span> d=3.14159265;
<span class="kw"><font color="#0000b0">int</font></span> i = <span class="kw"><font color="#0000b0">static_cast</font></span>&lt;<span class="kw"><font color="#0000b0">int</font></span>&gt;(d); 
</pre></td></tr></tbody></table><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; "></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; ">Or any conversion between classes with explicit constructors or operator functions as described in "implicit conversions" above.</p><h3 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-family: verdana, sans-serif; font-size: 14px; line-height: 21px; ">reinterpret_cast: 1. 任何指针之间的相互转换，即使这些类型之间没有任何关系. 2. 指针和整数类型的相互转换(指针-&gt;int时在Mac上会报错: loses precision).</h3><span style="font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; "><tt>reinterpret_cast</tt></span><span style="font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; ">&nbsp;</span><span style="font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; ">converts any pointer type to any other pointer type, even of unrelated classes. The operation result is a simple binary copy of the value from one pointer to the other. All pointer conversions are allowed: neither the content pointed nor the pointer type itself is checked.</span><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; ">It can also cast pointers to or from integer types. The format in which this integer value represents a pointer is platform-specific. The only guarantee is that a pointer cast to an integer type large enough to fully contain it, is granted to be able to be cast back to a valid pointer.</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; ">The conversions that can be performed by&nbsp;<tt>reinterpret_cast</tt>&nbsp;but not by&nbsp;<tt>static_cast</tt>&nbsp;have no specific uses in C++ are low-level operations, whose interpretation results in code which is generally system-specific, and thus non-portable. For example:</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; "></p><table class="snippet" style="font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; "><tbody><tr><td class="code"><pre><span class="kw"><font color="#0000b0">class</font></span> A {};
<span class="kw"><font color="#0000b0">class</font></span> B {};
A * a = <span class="kw"><font color="#0000b0">new</font></span> A;
B * b = <span class="kw"><font color="#0000b0">reinterpret_cast</font></span>&lt;B*&gt;(a);
</pre></td></tr></tbody></table><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; "></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; ">This is valid C++ code, although it does not make much sense, since now we have a pointer that points to an object of an incompatible class, and thus dereferencing it is unsafe.</p><h3 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-family: verdana, sans-serif; font-size: 14px; line-height: 21px; ">const_cast: 转换对象(primitive类型的不可以: int, float, double...)，指针，引用的const属性，有则去掉，没有则加上</h3><span style="font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; ">This type of casting manipulates the constness of an object, either to be set or to be removed. For example, in order to pass a const argument to a function that expects a non-constant parameter:</span><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; "></p><table class="codebox" style="font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; "><tbody><tr><td class="code"><pre><span class="comm"><font color="#007000">// const_cast</font></span>
<span class="prep"><font color="#500070">#include &lt;iostream&gt;</font></span>
<span class="kw"><font color="#0000b0">using</font></span> <span class="kw"><font color="#0000b0">namespace</font></span> std;

<span class="kw"><font color="#0000b0">void</font></span> print (<span class="kw"><font color="#0000b0">char</font></span> * str)
{
  cout &lt;&lt; str &lt;&lt; endl;
}

<span class="kw"><font color="#0000b0">int</font></span> main () {
  <span class="kw"><font color="#0000b0">const</font></span> <span class="kw"><font color="#0000b0">char</font></span> * c = <span class="str"><font color="#600030">"sample text"</font></span>;
  print ( <span class="kw"><font color="#0000b0">const_cast</font></span>&lt;<span class="kw"><font color="#0000b0">char</font></span> *&gt; (c) );
  <span class="kw"><font color="#0000b0">return</font></span> 0;
}
</pre></td><td class="result"><pre>sample text
</pre></td></tr></tbody></table><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; "></p><h3 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-family: verdana, sans-serif; font-size: 14px; line-height: 21px; ">typeid</h3><span style="font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; "><tt>typeid</tt></span><span style="font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; ">&nbsp;</span><span style="font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; ">allows to check the type of an expression:</span><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; "><tt>typeid (expression)<br></tt></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; ">This operator returns a reference to a constant object of type&nbsp;<tt>type_info</tt>&nbsp;that is defined in the standard header file&nbsp;<tt>&lt;typeinfo&gt;</tt>. This returned value can be compared with another one using operators&nbsp;<tt>==</tt>&nbsp;and&nbsp;<tt>!=</tt>&nbsp;or can serve to obtain a null-terminated character sequence representing the data type or class name by using its&nbsp;<tt>name()</tt>&nbsp;member.</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; "></p><table class="codebox" style="font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; "><tbody><tr><td class="code"><pre><span class="comm"><font color="#007000">// typeid</font></span>
<span class="prep"><font color="#500070">#include &lt;iostream&gt;</font></span>
<span class="prep"><font color="#500070">#include &lt;typeinfo&gt;</font></span>
<span class="kw"><font color="#0000b0">using</font></span> <span class="kw"><font color="#0000b0">namespace</font></span> std;

<span class="kw"><font color="#0000b0">int</font></span> main () {
  <span class="kw"><font color="#0000b0">int</font></span> * a,b;
  a=0; b=0;
  <span class="kw"><font color="#0000b0">if</font></span> (<span class="kw"><font color="#0000b0">typeid</font></span>(a) != <span class="kw"><font color="#0000b0">typeid</font></span>(b))
  {
    cout &lt;&lt; <span class="str"><font color="#600030">"a and b are of different types:\n"</font></span>;
    cout &lt;&lt; <span class="str"><font color="#600030">"a is: "</font></span> &lt;&lt; <span class="kw"><font color="#0000b0">typeid</font></span>(a).name() &lt;&lt; <span class="str"><font color="#600030">'\n'</font></span>;
    cout &lt;&lt; <span class="str"><font color="#600030">"b is: "</font></span> &lt;&lt; <span class="kw"><font color="#0000b0">typeid</font></span>(b).name() &lt;&lt; <span class="str"><font color="#600030">'\n'</font></span>;
  }
  <span class="kw"><font color="#0000b0">return</font></span> 0;
}
</pre></td><td class="result"><pre>a and b are of different types:
a is: int *
b is: int  
</pre></td></tr></tbody></table><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; "></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; ">When&nbsp;<tt>typeid</tt>&nbsp;is applied to classes&nbsp;<tt>typeid</tt>&nbsp;uses the RTTI to keep track of the type of dynamic objects. When typeid is applied to an expression whose type is a polymorphic class, the result is the type of the most derived complete object:</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; "></p><table class="codebox" style="font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; "><tbody><tr><td class="code"><pre><span class="comm"><font color="#007000">// typeid, polymorphic class</font></span>
<span class="prep"><font color="#500070">#include &lt;iostream&gt;</font></span>
<span class="prep"><font color="#500070">#include &lt;typeinfo&gt;</font></span>
<span class="prep"><font color="#500070">#include &lt;exception&gt;</font></span>
<span class="kw"><font color="#0000b0">using</font></span> <span class="kw"><font color="#0000b0">namespace</font></span> std;

<span class="kw"><font color="#0000b0">class</font></span> CBase { <span class="kw"><font color="#0000b0">virtual</font></span> <span class="kw"><font color="#0000b0">void</font></span> f(){} };
<span class="kw"><font color="#0000b0">class</font></span> CDerived : <span class="kw"><font color="#0000b0">public</font></span> CBase {};

<span class="kw"><font color="#0000b0">int</font></span> main () {
  <span class="kw"><font color="#0000b0">try</font></span> {
    CBase* a = <span class="kw"><font color="#0000b0">new</font></span> CBase;
    CBase* b = <span class="kw"><font color="#0000b0">new</font></span> CDerived;
    cout &lt;&lt; <span class="str"><font color="#600030">"a is: "</font></span> &lt;&lt; <span class="kw"><font color="#0000b0">typeid</font></span>(a).name() &lt;&lt; <span class="str"><font color="#600030">'\n'</font></span>;
    cout &lt;&lt; <span class="str"><font color="#600030">"b is: "</font></span> &lt;&lt; <span class="kw"><font color="#0000b0">typeid</font></span>(b).name() &lt;&lt; <span class="str"><font color="#600030">'\n'</font></span>;
    cout &lt;&lt; <span class="str"><font color="#600030">"*a is: "</font></span> &lt;&lt; <span class="kw"><font color="#0000b0">typeid</font></span>(*a).name() &lt;&lt; <span class="str"><font color="#600030">'\n'</font></span>;
    cout &lt;&lt; <span class="str"><font color="#600030">"*b is: "</font></span> &lt;&lt; <span class="kw"><font color="#0000b0">typeid</font></span>(*b).name() &lt;&lt; <span class="str"><font color="#600030">'\n'</font></span>;
  } <span class="kw"><font color="#0000b0">catch</font></span> (exception&amp; e) { cout &lt;&lt; <span class="str"><font color="#600030">"Exception: "</font></span> &lt;&lt; e.what() &lt;&lt; endl; }
  <span class="kw"><font color="#0000b0">return</font></span> 0;
}
</pre></td><td class="result"><pre>a is: class CBase *
b is: class CBase *
*a is: class CBase
*b is: class CDerived
</pre></td></tr></tbody></table><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; "></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; ">Notice how the type that&nbsp;<tt>typeid</tt>&nbsp;considers for pointers is the pointer type itself (both&nbsp;<tt>a</tt>&nbsp;and&nbsp;<tt>b</tt>&nbsp;are of type&nbsp;<tt>class CBase *</tt>). However, when&nbsp;<tt>typeid</tt>&nbsp;is applied to objects (like&nbsp;<tt>*a</tt>&nbsp;and&nbsp;<tt>*b</tt>)&nbsp;<tt>typeid</tt>&nbsp;yields their dynamic type (i.e. the type of their most derived complete object: 真实的类型，即使子类对象使用的是父类的指针，但返回的子类的信息).</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; ">If the type&nbsp;<tt>typeid</tt>&nbsp;evaluates is a pointer preceded by the dereference operator (<tt>*</tt>), and this pointer has a null value,&nbsp;<tt>typeid</tt>&nbsp;throws a&nbsp;<tt>bad_typeid</tt>&nbsp;exception.</p><img src ="http://www.cppblog.com/biao/aggbug/128986.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2010-10-08 05:25 <a href="http://www.cppblog.com/biao/archive/2010/10/08/128986.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>