??xml version="1.0" encoding="utf-8" standalone="yes"?>久久久久九国产精品,青草影院天堂男人久久,久久久精品久久久久特色影视http://www.shnenglu.com/biao/category/9972.html 我不聪明Q但我会很努?/description>zh-cnSat, 03 Aug 2013 11:21:27 GMTSat, 03 Aug 2013 11:21:27 GMT60QtQFontMetricshttp://www.shnenglu.com/biao/archive/2013/08/03/202322.html逛奔的蜗?/dc:creator>逛奔的蜗?/author>Sat, 03 Aug 2013 10:27:00 GMThttp://www.shnenglu.com/biao/archive/2013/08/03/202322.htmlhttp://www.shnenglu.com/biao/comments/202322.htmlhttp://www.shnenglu.com/biao/archive/2013/08/03/202322.html#Feedback0http://www.shnenglu.com/biao/comments/commentRss/202322.htmlhttp://www.shnenglu.com/biao/services/trackbacks/202322.html在一个Rect的正中间l制文本: rect的中点与文本的中炚w?
int cx = rect.x + rect.width / 2;
int fx = cx - fm.width(text);

int cy = rect.y + rect.height / 2;
int fy = cy + fm.xHeight / 2;



@import url(http://www.shnenglu.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);

]]>
CQ函数指针数l的定义http://www.shnenglu.com/biao/archive/2013/07/26/202124.html逛奔的蜗?/dc:creator>逛奔的蜗?/author>Thu, 25 Jul 2013 23:56:00 GMThttp://www.shnenglu.com/biao/archive/2013/07/26/202124.htmlhttp://www.shnenglu.com/biao/comments/202124.htmlhttp://www.shnenglu.com/biao/archive/2013/07/26/202124.html#Feedback0http://www.shnenglu.com/biao/comments/commentRss/202124.htmlhttp://www.shnenglu.com/biao/services/trackbacks/202124.html
// [[1]] 使用typedef定义函数指针Z个类型,更喜Ƣ这U方?/font>
typedef int (*MYFUN)(int, int);
MYFUN funcs[10];

// [[2]] 使用typedef定义函数指针数组Z个类型,不是很直?/font>
typedef int (*MYFUN2[10])(int, int);
MYFUN2 funcs2;

int main(int argc, char *argv[]) {
    funcs[0] = add;
    qDebug() << (*funcs[0])(2, 3); // 也可以用 funcs[0](2, 3).

    funcs2[0] = add;
    qDebug() << (*funcs2[0])(2, 3);

    return 0;
}
@import url(http://www.shnenglu.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);

]]>
QtQ解读Q_GLOBAL_STATIC(QFontDatabasePrivate, privateDb) http://www.shnenglu.com/biao/archive/2013/06/26/201312.html逛奔的蜗?/dc:creator>逛奔的蜗?/author>Wed, 26 Jun 2013 02:02:00 GMThttp://www.shnenglu.com/biao/archive/2013/06/26/201312.htmlhttp://www.shnenglu.com/biao/comments/201312.htmlhttp://www.shnenglu.com/biao/archive/2013/06/26/201312.html#Feedback0http://www.shnenglu.com/biao/comments/commentRss/201312.htmlhttp://www.shnenglu.com/biao/services/trackbacks/201312.htmlFrom: http://zhgw01.blog.163.com/blog/static/1041481220105253491586/
Ҏ src/corelib/global.h
 template <typename T>
class QGlobalStatic
{
public:
    T *pointer;
    inline QGlobalStatic(T *p) : pointer(p) { }
    inline ~QGlobalStatic() { pointer = 0; }
};

#define Q_GLOBAL_STATIC(TYPE, NAME)                              \
    static TYPE *NAME()                                          \
    {                                                            \
        static TYPE this_##NAME;                                 \
        static QGlobalStatic<TYPE > global_##NAME(&this_##NAME); \
        return global_##NAME.pointer;                            \
    }

   

上面的展开?/span>
 static  QFontDatabasePrivate *privateDb()
{
    static QFontDatabasePrivate this_privateDb;  // 注意W一ơ引用的时候会开始进行初始化, 以后不再生成
   static QGLobalStatic<QFontDatabasePrivate> global_privateDb(&this_privateDb); //对上面生成的对象的指针进行包装, 同上Q只有第一ơ才会生?br />
  return global_privateDb;
}

QFontDatabase的构造函C会调用createDatabase Q里面调用了initializeDb), initializeDb里调用了privateDb(),一个这个时候是W一ơ调用, 会生成QFontDatabasePrivate对象

QFontDatabasePrivate中的QDataStream *stream成员才是真正处理qte的字?/p>

]]>
C++QQT插g开发方?/title><link>http://www.shnenglu.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.shnenglu.com/biao/archive/2013/06/25/201308.html</guid><wfw:comment>http://www.shnenglu.com/biao/comments/201308.html</wfw:comment><comments>http://www.shnenglu.com/biao/archive/2013/06/25/201308.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/biao/comments/commentRss/201308.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/biao/services/trackbacks/201308.html</trackback:ping><description><![CDATA[From: http://blog.csdn.net/henreash/article/details/7264489@import url(http://www.shnenglu.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文g,d一个接口定?h文g和一个接口实现类(一?h一?cpp).代码如下:</p> <p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">1.接口文g源码</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 <QString></p> <p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; "><br /> class EchoInterface<br /> {<br /> public:<br />  virtual ~EchoInterface() {}<br />  virtual QString echo(const QString &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.接口实现cd文g</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 <QObject><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 />  Q_OBJECT<br />  Q_INTERFACES(EchoInterface)<br /> public:<br />  Plugin();<br />  ~Plugin();<br /> public:<br />  QString echo(const QString &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.接口实现ccpp文g</p> <p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">#include "plugin.h"<br /> #include <QtGui></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 &message)<br /> {<br />  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格式的插?q里可能会遇到LNK2001错误,因ؓ插g接口定义依赖于QT的元数据,而在代码中手动添加Q_OBJECT宏后,~译器不会自动ؓ我们生成moc_XXXX.cpp文g,因此需要用命令行生成moc元数据文?cmd-->cd 源码所在命?-->moc -o moc_XXXX.cpp XXXX.h.q样可以手动创建出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 <QtGui><br /> #include <QMessageBox><br /> #include <QDir></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 />  : QMainWindow(parent, flags)<br /> {<br />  ui.setupUi(this);<br />  QObject::connect(ui.pushButton, SIGNAL(clicked()), this, SLOT(ButtonClicked()));<br />  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 />  QDir pluginsDir(qApp->applicationDirPath());<br />  foreach (QString filename, pluginsDir.entryList(QDir::Files))<br />  {<br />   QPluginLoader pluginLoader(pluginsDir.absoluteFilePath(filename));<br />   QObject *plugin = pluginLoader.instance();<br />   if(plugin)<br />   {<br />    echoInterface = qobject_cast<EchoInterface *>(plugin);//echoInterface是成员变?br />    if(echoInterface)<br />     return true;<br />   }<br />  }<br /> }</p> <p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">void Test::ButtonClicked()<br /> {<br />  QString plugin = ui.lineEdit->text();<br />  QMessageBox::information(NULL, "", echoInterface->echo(plugin));<br /> }</p> <p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">分析代码可见,q里Ҏ有与exe文g同目录的文g试加蝲插g,如果加蝲成功则返?代码实现非常z?在正式开发中可将已加载的插g接口存放在列表中,在需要的时候依ơ调用即?</p> <p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">另外如果惌取插件中cd义的元数?可以使插件接口类从QObjectl承,q用Q_CLASSINFO宏添加键值对,如Q_CLASSINFO("Author**", "Henreash**").q从ȝ序中使用插g对象元数据获取这些键值对:</p> <p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; "> const QMetaObject *mo = echoInterface->metaObject();<br />  for(int i = 0; i < mo->classInfoCount(); i++)<br />  {<br />   qDebug() << mo->classInfo(i).name() << mo->classInfo(i).value();<br />  }</p> <p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">使用q个机制可以在定义插件类的时候向ȝ序传递一些特D信?</p> <p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">试发现,如果在插件接口类中定义一个静态变?那么q个静态变量在插g中的地址和在ȝ序中的地址是不相同?</p> </div><img src ="http://www.shnenglu.com/biao/aggbug/201308.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/biao/" target="_blank">逛奔的蜗?/a> 2013-06-25 23:26 <a href="http://www.shnenglu.com/biao/archive/2013/06/25/201308.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C++Q单?Singletonhttp://www.shnenglu.com/biao/archive/2013/06/25/201306.html逛奔的蜗?/dc:creator>逛奔的蜗?/author>Tue, 25 Jun 2013 13:46:00 GMThttp://www.shnenglu.com/biao/archive/2013/06/25/201306.htmlhttp://www.shnenglu.com/biao/comments/201306.htmlhttp://www.shnenglu.com/biao/archive/2013/06/25/201306.html#Feedback0http://www.shnenglu.com/biao/comments/commentRss/201306.htmlhttp://www.shnenglu.com/biao/services/trackbacks/201306.htmlFrom: http://zh.wikipedia.org/wiki/单例模式

?a title="C++" style="text-decoration: none; color: #0b0080; background-image: none; background-position: initial initial; background-repeat: initial initial; ">C++~程语言中,单例模式应用的例子如下述代码所C(q里仅仅提供一个示例,q个例子对多U程的情况ƈ不是安全的)Q?/p>

// ...
class lock
{
public:
lock();
lock(lock const & l);
~lock();
lock & operator =(lock const & l);
void request();
void release();
// ...
};
lock::lock()
{
// ...
}
// ...
lock::~lock()
{
// ...
}
// ...
void lock::request()
{
// ...
}
void lock::release()
{
// ...
}
// ...
// assumes _DATA_TYPE_ has a default constructor
template<typename _DATA_TYPE_>
class singleton
{
public:
static _DATA_TYPE_ * request();
static void release();
private:
singleton();
singleton(singleton<_DATA_TYPE_> const & s);
~singleton();
singleton<_DATA_TYPE_> & operator =(singleton<_DATA_TYPE_> const & s);
static _DATA_TYPE_ * pointer;
static lock mutex;
// ...
};
template<typename _DATA_TYPE_>
_DATA_TYPE_ * singleton<_DATA_TYPE_>::pointer = 0;
template<typename _DATA_TYPE_>
lock singleton<_DATA_TYPE_>::mutex;
template<typename _DATA_TYPE_>
_DATA_TYPE_ * singleton<_DATA_TYPE_>::request()
{
if(singleton<_DATA_TYPE_>::pointer == 0)
{
singleton<_DATA_TYPE_>::mutex.request();
if(singleton<_DATA_TYPE_>::pointer == 0)
{
singleton<_DATA_TYPE_>::pointer = new _DATA_TYPE_;
}
singleton<_DATA_TYPE_>::mutex.release();
}
return singleton<_DATA_TYPE_>::pointer;
}
template<typename _DATA_TYPE_>
void singleton<_DATA_TYPE_>::release()
{
if(singleton<_DATA_TYPE_>::pointer != 0)
{
singleton<_DATA_TYPE_>::mutex.request();
if(singleton<_DATA_TYPE_>::pointer != 0)
{
delete singleton<_DATA_TYPE_>::pointer;
singleton<_DATA_TYPE_>::pointer = 0;
}
singleton<_DATA_TYPE_>::mutex.release();
}
}
template<typename _DATA_TYPE_>
singleton<_DATA_TYPE_>::singleton()
{
// ...
}
// ...
int main()
{
int * s;
s = singleton<int>::request();
// ...
singleton<int>::release();
return 0;
}

@import url(http://www.shnenglu.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);

]]>
OpenGLQ展COpenGLҎ的软gTutorshttp://www.shnenglu.com/biao/archive/2013/04/19/199557.html逛奔的蜗?/dc:creator>逛奔的蜗?/author>Thu, 18 Apr 2013 16:24:00 GMThttp://www.shnenglu.com/biao/archive/2013/04/19/199557.htmlhttp://www.shnenglu.com/biao/comments/199557.htmlhttp://www.shnenglu.com/biao/archive/2013/04/19/199557.html#Feedback0http://www.shnenglu.com/biao/comments/commentRss/199557.htmlhttp://www.shnenglu.com/biao/services/trackbacks/199557.html nate robins
main OPENGL chronicles allies
--- Tutors ---

Tutors source code package (tutors-src.zip 920KB) 

Source package + Windows executables (tutors-win32.zip 1.24MB) 

Source package + Mac OS X universal binaries (tutors-macosx.zip 1.2MB) 

Contributions from various people (Unix makefiles, etc). 

fog
(click on image to enlarge it)
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 exponential2 fog functions. The fog color can also be dynamically changed.
lightposition
(click on image to enlarge it)
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.
lightmaterial
(click on image to enlarge it)

The lightmaterial tutorial program (shown above) demonstrates how OpenGL lighting and material properties interact and effect each other. All material & light properties are tweakable. A second command panel allows spotlight parameters & attenuation factors to be changed.
projection
(click on image to enlarge it)
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.
transformation
(click on image to enlarge it)
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.
texture
(click on image to enlarge it)

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 & environment attributes are tweakable. A separate command panel allows manipulation of the texture matrix.
shapes
(click on image to enlarge it)
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 (mason@woo.com).



--- --- ---

@import url(http://www.shnenglu.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);

]]>
C++QLIB和DLL的区别与使用http://www.shnenglu.com/biao/archive/2013/03/14/198416.html逛奔的蜗?/dc:creator>逛奔的蜗?/author>Thu, 14 Mar 2013 06:38:00 GMThttp://www.shnenglu.com/biao/archive/2013/03/14/198416.htmlhttp://www.shnenglu.com/biao/comments/198416.htmlhttp://www.shnenglu.com/biao/archive/2013/03/14/198416.html#Feedback0http://www.shnenglu.com/biao/comments/commentRss/198416.htmlhttp://www.shnenglu.com/biao/services/trackbacks/198416.html共有两种库:
一U是LIB包含了函数所在的DLL文g和文件中函数位置的信息(入口Q,代码p行时加蝲在进E空间中的DLL提供Q称为动态链接库dynamic link library?/span>
一U是LIB包含函数代码本nQ在~译时直接将代码加入E序当中Q称为静态链接库static link library?/span>
共有两种链接方式Q?/span>
动态链接用动态链接库Q允许可执行模块Q?dll文g?exe文gQ仅包含在运行时定位DLL函数的可执行代码所需的信息?/span>
静态链接用静态链接库Q链接器从静态链接库LIB获取所有被引用函数Qƈ库同代码一h到可执行文g中?/span>

关于lib和dll的区别如下:
Q?Qlib是编译时用到的,dll是运行时用到的。如果要完成源代码的~译Q只需要libQ如果要使动态链接的E序q行hQ只需要dll?/span>
Q?Q如果有dll文gQ那么lib一般是一些烦引信息,记录了dll中函数的入口和位|,dll中是函数的具体内容;如果只有lib文gQ那么这个lib文g是静态编译出来的Q烦引和实现都在其中。用静态编译的lib文gQ在q行E序时不需要再挂动态库Q缺ҎD应用E序比较大,而且失去了动态库的灵zL,发布新版本时要发布新的应用程序才行?/span>
Q?Q动态链接的情况下,有两个文Ӟ一个是LIB文gQ一个是DLL文g。LIB包含被DLL导出的函数名U和位置QDLL包含实际的函数和数据Q应用程序用LIB文g链接到DLL文g。在应用E序的可执行文g中,存放的不是被调用的函C码,而是DLL中相应函C码的地址Q从而节省了内存资源。DLL和LIB文g必须随应用程序一起发行,否则应用E序会生错误。如果不想用lib文g或者没有lib文gQ可以用WIN32 API函数LoadLibrary、GetProcAddress装蝲?/span>

使用lib需注意两个文gQ?/span>
Q?Q?h头文Ӟ包含lib中说明输出的cLW号原型或数据结构。应用程序调用libӞ需要将该文件包含入应用E序的源文g中?/span>
Q?Q?LIB文gQ略?/span>

使用dll需注意三个文gQ?/span>
Q?Q?h头文Ӟ包含dll中说明输出的cLW号原型或数据结构的.h文g。应用程序调用dllӞ需要将该文件包含入应用E序的源文g中?/span>
Q?Q?LIB文gQ是dll在编译、链接成功之后生成的文gQ作用是当其他应用程序调用dllӞ需要将该文件引入应用程序,否则产生错误?/span>如果不想用lib文g或者没有lib文gQ可以用WIN32 API函数LoadLibrary、GetProcAddress装蝲?br />Q?Q?dll文gQ真正的可执行文Ӟ开发成功后的应用程序在发布Ӟ只需要有.exe文g?dll文gQƈ不需?lib文g?h头文件?/span>

使用lib的方法:
静态lib中,一个lib文g实际上是L个obj文g的集合,obj文g是cpp文g~译生成的。在~译q种静态库工程ӞҎ不会遇到链接错误Q即使有错,也只会在使用q个lib的EXT文g或者DLL工程里暴露出来?/span>
在VC中新Z个static librarycd的工ELibQ加入test.cpp文g和test.h文gQ头文g内包括函数声明)Q然后编译,q成了Lib.lib文g?/span>
别的工程要用这个lib有两U方式:
Q?Q在project->link->Object/Library Module中加入Lib.lib文gQ先查询工程目录Q再查询pȝLib目录Q;或者在源代码中加入指o#pragma comment(lib, “Lib.lib”)?/span>
Q?Q将Lib.lib拷入工程所在目录,或者执行文件生成的目录Q或者系lLib目录中?/span>
Q?Q加入相应的头文件test.h?/span>

使用DLL的方法:
使用动态链接中的libQ不是obj文g的集合,即里面不会有实际的实玎ͼ它只是提供动态链接到DLL所需要的信息Q这Ulib可以在编译一个DLL工程时由~译器生成?/span>
创徏DLL工程的方法(略)?/span>
Q?Q隐式链?/strong>
W一U方法是Q通过project->link->Object/Library Module中加?lib文gQ或者在源代码中加入指o#pragma comment(lib, “Lib.lib”)Q,q将.dll文g|入工程所在目录,然后d对应?h头文件?/span>

#include "stdafx.h"
#include "DLLSample.h"

#pragma comment(lib, "DLLSample.lib")    //你也可以在项目属性中讄库的链接

int main()
{
        TestDLL(123);   //dll中的函数Q在DllSample.h中声?/span>
        return(1);
}

Q?Q显式链?/strong>
需要函数指针和WIN32 API函数LoadLibrary、GetProcAddress装蝲Q用这U蝲入方法,不需?lib文g?h头文Ӟ只需?dll文g卛_Q将.dll文g|入工程目录中)?/span>

#include <iostream>
#include <windows.h>         //使用函数和某些特D变?/span>
typedef void (*DLLFunc)(int);
int main()
{
        DLLFunc dllFunc;
        HINSTANCE hInstLibrary = LoadLibrary("DLLSample.dll");

        
if (hInstLibrary == NULL)
        
{
          FreeLibrary(hInstLibrary);
        }

        dllFunc = (DLLFunc)GetProcAddress(hInstLibrary, "TestDLL");
        
if (dllFunc == NULL)
        
{
          FreeLibrary(hInstLibrary);
        }

        dllFunc(123);
        std::cin.get();
        FreeLibrary(hInstLibrary);
        
return(1);
}


LoadLibrary函数利用一个名UC为参敎ͼ获得DLL的实例(HINSTANCEcd是实例的句柄Q,通常调用该函数后需要查看一下函数返回是否成功,如果不成功则q回NULLQ句柄无效)Q此时调用函数FreeLibrary释放DLL获得的内存?/span>
GetProcAddress函数利用DLL的句柄和函数的名UC为参敎ͼq回相应的函数指针,同时必须使用{Q判断函数指针是否ؓNULLQ如果是则调用函数FreeLibrary释放DLL获得的内存。此后,可以使用函数指针来调用实际的函数?/span>
最后要记得使用FreeLibrary函数释放内存?/span>

注意Q应用程序如何找到DLL文gQ?/span>
使用LoadLibrary昑ּ链接Q那么在函数的参C可以指定DLL文g的完整\径;如果不指定\径,或者进行隐式链接,Windows遵循下面的搜烦序来定位DLLQ?/span>
Q?Q包含EXE文g的目?/span>
Q?Q工E目?/span>
Q?QWindowspȝ目录
Q?QWindows目录
Q?Q列在Path环境变量中的一pd目录

From: http://www.shnenglu.com/ming81/archive/2013/03/04/198215.html

.h头文件是~译时必ȝQlib是链接时需要的Qdll是运行时需要的?/span>

附加依赖的?lib不是.dllQ若生成了DLL,则肯定也生成 LIB文g。如果要完成源代码的~译和链接,有头文g和lib够了。如果也使动态连接的E序q行hQ有dll够了。在开发和调试阶段Q当然最好都有?/span>

.h .lib .dll三者的关系是:

H文g作用?声明函数接口 

DLL文g作用? 函数可执行代?nbsp;

当我们在自己的程序中引用了一个H文g里的函数,~链器怎么知道该调用哪个DLL文g?q就是LIB文g的作? 告诉链接?调用的函数在哪个DLL中,函数执行代码在DLL中的什么位|?Q这也就是ؓ什么需要附加依赖项 .LIB文gQ它起到桥梁的作用。如果生成静态库文gQ则没有DLL Q只有libQ这时函数可执行代码部分也在lib文g?/span>

目前以lib后缀的库有两U,一Uؓ静态链接库(Static LibaryQ以下简U?#8220;静态库”)Q另一Uؓ动态连接库(DLLQ以下简U?#8220;动态库”)的导入库(Import LibaryQ以下简U?#8220;导入?#8221;Q。静态库是一个或者多个obj文g的打?Q所以有人干脆把从obj文g生成lib的过E称为ArchiveQ即合ƈC赗比如你链接一个静态库Q如果其中有错,它会准确的找到是哪个obj有错Q即静态lib只是壛_。动态库一般会有对应的导入库,方便E序静态蝲入动态链接库 Q否则你可能需要自己LoadLibary调入DLL文gQ然后再手工GetProcAddress获得对应函数了。有了导入库Q你只需要链接导入库后按照头文g函数接口的声明调用函数就可以了。导入库和静态库的区别很大,他们实质是不一L东西。静态库本n包含了实际执行代码、符可{等Q而对于导入库而言Q其实际的执行代码位于动态库中,导入库只包含了地址W号表等Q确保程序找到对应函数的一些基本地址信息?/span>

一般的动态库E序有lib文g和dll文g。lib文g是必d~译期就q接到应用程序中的,而dll文g是运行期才会被调用的?如果有dll文gQ那么对应的lib文g一般是一些烦引信息,具体的实现在dll文g中。如果只有lib文gQ那么这个lib文g是静态编译出来的Q烦引和实现都在其中。静态编译的lib文g有好处:l用户安装时׃需要再挂动态库了。但也有~点Q就是导致应用程序比较大Q而且失去了动态库的灵zL,在版本升U时Q同时要发布新的应用E序才行。在动态库的情况下Q有两个文gQ而一个是引入库(.LIBQ文Ӟ一个是DLL文gQ引入库文g包含被DLL导出的函数的名称和位|,DLL包含实际的函数和数据Q应用程序用LIB文g链接到所需要用的DLL文gQ库中的函数和数据ƈ不复制到可执行文件中Q因此在应用E序的可执行文g中,存放的不是被调用的函C码,而是DLL中所要调用的函数的内存地址Q这样当一个或多个应用E序q行是再把程序代码和被调用的函数代码链接hQ从而节省了内存资源。从上面的说明可以看出,DLL?LIB文g必须随应用程序一起发行,否则应用E序会产生错误?/span>

静态库和共享库都是一个obj文g的集?Q但静态链接后Q执行程序中存在自己所需obj的一份拷贝,而动态链接后Q执行程序仅仅是包含对共享库的一个引用。共享库相当于一个由多个obj文gl合而成的obj文gQ在链接后其所有代码被加蝲Q不需要的q是不需要的?/span>
g可以得出一个结论:
  静态链接后的程序比动态链接的所用存储空间大Q因为执行程序中包含了库中代码拷贝;
  而动态链接的E序比静态链接的所用的q行I间大,因ؓ它将不需要的代码也加载到q行I间?/span>


针对上面的知? 个问题:

1Q?DLL?LIB文g必须随应用程序一起发行,否则应用E序会产生错误?nbsp;
我的{案Qlib应该不需要吧?br />
2Q如果是某个E序中调用了一个动态库Q通过header文gQlib+dll来调用)Q则对动态库的某个函数的内容修改了,但接口不改,则调用此动态库的程序需重新~译q接吗?如果是通过loadlibary动态加载,需要重新编译连接吗Q?br />

我的{案Q通过header+lib+dll调用的话需要重新编译连接,但是通过loadlibrary来用的话,不需要重新编译连接?/span>
W?个答案错?应该是不需要重新编?接口不变的话 .lib都不用更?br />From: http://www.shnenglu.com/mzty/archive/2010/11/28/134901.html

    1Q?节省内存。同一个Y件模块,若是以源代码的Ş式重用,则会被编译到不同的可执行E序中,同时q行q些exe时这些模块的二进制码会被重复加蝲到内存中。如 果用dll,则只在内存中加蝲一ơ,所有用该dll的进E会׃n此块内存Q当Ӟ像dll中的全局变量q种东西是会被每个进E复制一份的Q?/span>
    
    2Q?不需~译的Y件系l升U,若一个Y件系l用了dll,则该dll被改变(函数名不变)Ӟpȝ升只需要更换此dll卛_Q不需要重新编译整个系l。事实上Q很多Y仉是以q种方式升的。例如我们经常玩的星际、魔兽等游戏也是q样q行版本升的?/span>
    
    3Q?Dll库可以供多种~程语言使用Q例如用c~写的dll可以在vb中调用。这一点上DLLq做得很不够Q因此在dll的基上发明了COM技术,更好的解决了一pd问题?/span>

 

 



]]>
CQ+QC++ 的指针和句柄http://www.shnenglu.com/biao/archive/2012/08/07/186558.html逛奔的蜗?/dc:creator>逛奔的蜗?/author>Tue, 07 Aug 2012 04:44:00 GMThttp://www.shnenglu.com/biao/archive/2012/08/07/186558.htmlhttp://www.shnenglu.com/biao/comments/186558.htmlhttp://www.shnenglu.com/biao/archive/2012/08/07/186558.html#Feedback0http://www.shnenglu.com/biao/comments/commentRss/186558.htmlhttp://www.shnenglu.com/biao/services/trackbacks/186558.html1、句柄,是整个windows~程的基。一个句柄是指用唯一的整数|即一个四字节长的数|来标志应用程序中的不同对象和同类对象中的不同实例。应用程序通过句柄讉K相应对象的信息,但是据ƈ不是指针Q不能直接通过它来阅读文g中的信息?/div>
 2、对于c++中的句柄和指针容易؜淆?/div>
    指针Q他是内存的~号Q通过它我们可以直接对内存q行操作Q只要地址不变Q我们操作的物理地址业界不变?---与句柄的重大区别?/div>
   句柄Q一般是指向pȝ资源的位|,也可以说是地址Q但是这些地址一直保持不变。其实,对于windows pȝ来说支持的是虚拟内存的技术,l一旉内可能有些资源被换出Q有些资源被换进来。因此同一个资源在不同的时ȝ理位|是不同的。window是通过句柄来处理同一资源物理位置不断变化的问题。在window中固定的位置存储了一张表Q记录了所有资源的实时地址Q句柄其实没有直接指向资源的物理地址Q而是指向对应表中的某一,q样无论资源的物理地址怎么换,通过句柄都可以实时的扑ֈ他的实时位置?/div>

From: http://blog.chinaunix.net/uid-26874138-id-3302108.html
@import url(http://www.shnenglu.com/cutesoft_client/cuteeditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);

]]>其他~程Q版本控制入门简?/title><link>http://www.shnenglu.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.shnenglu.com/biao/archive/2012/04/27/172924.html</guid><wfw:comment>http://www.shnenglu.com/biao/comments/172924.html</wfw:comment><comments>http://www.shnenglu.com/biao/archive/2012/04/27/172924.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/biao/comments/commentRss/172924.html</wfw:commentRss><trackback:ping>http://www.shnenglu.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; ">版本控制已经出现有些q头了。然而,我还是会被h问v一些,诸如版本控制是什么或者它是如何工作的Q这样基的问题。本文会概括地解释版本控制解决的重要问题Q本文用的场景针对的是源代码版本控制?/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; " />目前有很多不同类型的版本控制pȝQVersion Control SystemQ?VCSQ。一些VCSQ比?a style="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 style="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>Q以中央仓库QrepositoryQؓ中心q行架构。此外,q有分布式的VCSQDistributed VCSQDVCSQ, <a style="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;<a style="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> 是两个新q出现的DVCS。然而,在上qCU类型的环境中,通常会有一?#8220;指定?#8221;中央仓库。对应地Q比如一个Subversion服务器或者一?a style="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>仓库。下面会Zq个场景q行囄说明。那么让我们开始吧?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; " />在开发者拷贝到本机之前Q服务器需要创Z个仓库。创建初始仓库会׃产品不同而有所差别。从现在P你所要知道的是Q在服务器上有一个初始空间。我把这个版本称作版?#8220;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; " /><a style="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; " />现在Q每个开发者(开发?和开发?Q都会拷贝版?#8220;A”C们本地电脑。再一ơ地Q从服务器拷贝的q程会由于品不同采用的技术会有所差别?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 style="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; " />每个开发者会在他们的本地拯上进行开发。他们的本地拯Z版本“A”。然而,׃他们<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>做同L开发,因而他们的版本会有所差别。因此,会有2个以上的版本会同时被创徏Q比如版?#8220;B”和版?#8220;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 style="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; " />开发?首先完成了她的工作ƈ提交到服务器。服务器上的当前版本被更新成版本“B”?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 style="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; " />开发?现在完成了他的工作ƈ试图提交到服务器。然而,q是服务器告知他Z开发的版本已经发生改变?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; ">q也是ؓ什么采取版本控制的首要原因之一?/strong>q个Ҏ是对网l共享代码然后由开发者手动更新的一个跨式发展Q这保了之前的~辑没有被新的修改覆盖?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 style="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; " />开发?必须首先获得所有版?#8220;B”的变化,q合q到他的修改中,然后才可以提交到服务器。这个过E听h有些复杂。然而,大多数现代的版本控制pȝ十分高Q能够自动在开发者的本地拯上完成合q。有几种情况会生冲H(例如Q开发?和开发?同时修改了同一个文件的同一行)。这是一些VCS产品比其他更高的地斏V不论如何完成合qӞ现在开发?在他们的本地pȝ上同时؜合了版本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 style="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; " />现在开发?可以提交他的版本到服务器?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 style="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; " />q是一个版本控制的基础。通过注意观察图中服务器的q线可以发现版本控制的原理。服务器记录了所有先前的版本包括发生的变化,什么时候发生以及由谁进行修攏V当需要进行代码回溯或者引入其他bugӞq个记录能够解除困境?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; " />我希望本文能够ؓ版本控制pȝ提供一个基的介l。如果你有Q何疑问,请就你问题发表评论?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; " />英文原文Q?a style="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 </a>~译Q?a style="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>在线 – <a style="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.shnenglu.com/biao/aggbug/172924.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/biao/" target="_blank">逛奔的蜗?/a> 2012-04-27 15:07 <a href="http://www.shnenglu.com/biao/archive/2012/04/27/172924.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C++Q位操作基础之位操作全面ȝhttp://www.shnenglu.com/biao/archive/2012/03/20/168357.html逛奔的蜗?/dc:creator>逛奔的蜗?/author>Mon, 19 Mar 2012 23:01:00 GMThttp://www.shnenglu.com/biao/archive/2012/03/20/168357.htmlhttp://www.shnenglu.com/biao/comments/168357.htmlhttp://www.shnenglu.com/biao/archive/2012/03/20/168357.html#Feedback0http://www.shnenglu.com/biao/comments/commentRss/168357.htmlhttp://www.shnenglu.com/biao/services/trackbacks/168357.html阅读全文

]]>
C++Q二l数l和二指针的传递问?/title><link>http://www.shnenglu.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.shnenglu.com/biao/archive/2012/01/12/164030.html</guid><wfw:comment>http://www.shnenglu.com/biao/comments/164030.html</wfw:comment><comments>http://www.shnenglu.com/biao/archive/2012/01/12/164030.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.shnenglu.com/biao/comments/commentRss/164030.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/biao/services/trackbacks/164030.html</trackback:ping><description><![CDATA[     摘要: 再次看这文章,感觉说的好多都是废话Q在文章最前面补充一句话Q?“[]的优先高于*”Q大家可以带着q句话看下面的~~~ ======================== 再一ơ的见证了自q基础不牢靠。。。幸好发现得早,看见|上_华ؓ的一个面试题p了q个斚w的?借那道华为的面试题引出问题,题目Q?char **p, a[16][8];  问:p=...  <a href='http://www.shnenglu.com/biao/archive/2012/01/12/164030.html'>阅读全文</a><img src ="http://www.shnenglu.com/biao/aggbug/164030.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/biao/" target="_blank">逛奔的蜗?/a> 2012-01-12 09:30 <a href="http://www.shnenglu.com/biao/archive/2012/01/12/164030.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C++QQҎ的存储方?/title><link>http://www.shnenglu.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.shnenglu.com/biao/archive/2011/12/18/162330.html</guid><wfw:comment>http://www.shnenglu.com/biao/comments/162330.html</wfw:comment><comments>http://www.shnenglu.com/biao/archive/2011/12/18/162330.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/biao/comments/commentRss/162330.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/biao/services/trackbacks/162330.html</trackback:ping><description><![CDATA[<span style="font-family: simsun; line-height: 23px; ">float内存存储l构(?字节Q?2位分别如?Q?br /> 31 30 29----23 22----0   <br /> <br /> 实数W号?指数W号?指数?有效C<br /> 其中实数W号?表示正,1表示负;指数W号?表示正,0表示?br /> <br /> float在计机中的存储计算ҎQ?br /> 1. 先将q个实数的绝对值化Zq制格式Q方法是Q实数的整数部分是除2取余和小数部分是?取整<br /> 2. 这个二q制格式实数的小数点左移或右Un位,直到数点移动到W一个有效数字的双?br /> 3. 从小数点双W一位开始数Z十三位数字放入第22到第0位?br /> 4. 如果实数是正的,则在W?1位放?#8220;0”Q否则放?#8220;1”?br /> 5. 如果n 是左Ud到的Q说明指数是正的Q第30位放?#8220;1”。如果n是右Ud到的或n=0Q则W?0位放?#8220;0”?br /> 6. 如果n是左Ud到的Q则n减去1后化Zq制Qƈ在左边加“0”补七位Q放入第29到第23位。如果n是右Ud到的或n=0Q?br /> <br /> 则将n化ؓ二进制后在左边加“0”补七位Q再各位求反Q再攑օW?9到第23位?nbsp; <br /> <br /> Ҏ上面那个步骤得:<br /> (1). 23.172001二进制Ş式ؓQ?001,0111.0010,1100,0000,1000,0100,0001....<br /> (2)左移4位得Q?.0111.0010,1100,0000,1000,0100,0001<br /> (3)取小数点?3位得Q?0111.0010,1100,0000,1000,010Q这23位ؓfloatCq制码的?3位)<br /> (4)W?1位: 0<br /> (5)W?0位: 1<br /> (6)左移4位,4的二q制0011Q不?位则Q?000,011<br /> 最后得23.172001在计机中的二进制表现Ş式ؓQ?100,0001,1011,1001,0110,0000,0100,0010<br /> 二进制{16q制得:41A96042</span> @import url(http://www.shnenglu.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: http://topic.csdn.net/u/20111217/19/6c5221d6-a779-4e6c-9eb0-72949857dc6e.html?95401</span></div><img src ="http://www.shnenglu.com/biao/aggbug/162330.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/biao/" target="_blank">逛奔的蜗?/a> 2011-12-18 11:20 <a href="http://www.shnenglu.com/biao/archive/2011/12/18/162330.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C/C++Q清I入缓冲区http://www.shnenglu.com/biao/archive/2010/12/17/136791.html逛奔的蜗?/dc:creator>逛奔的蜗?/author>Fri, 17 Dec 2010 10:17:00 GMThttp://www.shnenglu.com/biao/archive/2010/12/17/136791.htmlhttp://www.shnenglu.com/biao/comments/136791.htmlhttp://www.shnenglu.com/biao/archive/2010/12/17/136791.html#Feedback0http://www.shnenglu.com/biao/comments/commentRss/136791.htmlhttp://www.shnenglu.com/biao/services/trackbacks/136791.htmlint main(int argc, char *argv[]) {
    std::string str1, str2;
    
    std::cin >> str1;
    std::cin.sync();
    std::cin >> str2;
    
    std::cout << str1 << std::endl << str2 << std::endl;
    //fflush(stdin);
    
    return EXIT_SUCCESS;
}

int main(int argc, char *argv[]) {
    int v = 0;

    while (std::cin >> v) {
        std::cout << v << std::endl;
    }

    // The order clear() and sync() is very important.
    // Clear the input stream's state
    // and then clear the input stream's content
    // to input new string.
    std::cin.clear();
    std::cin.sync();

    std::string str;
    std::cin >> str;
    std::cout << str << std::endl;

    std::cout << v << std::endl;

    return EXIT_SUCCESS;
}

]]>
C/C++Q偷懒的qsort and bsearchhttp://www.shnenglu.com/biao/archive/2010/12/17/136788.html逛奔的蜗?/dc:creator>逛奔的蜗?/author>Fri, 17 Dec 2010 10:13:00 GMThttp://www.shnenglu.com/biao/archive/2010/12/17/136788.htmlhttp://www.shnenglu.com/biao/comments/136788.htmlhttp://www.shnenglu.com/biao/archive/2010/12/17/136788.html#Feedback1http://www.shnenglu.com/biao/comments/commentRss/136788.htmlhttp://www.shnenglu.com/biao/services/trackbacks/136788.html
利用C标准库函数提供的快速排序和二分查找法来q行排序和查?只需要自定义一个比较函数即?
   #include <stdlib.h>  void qsort( void *buf, size_t num, size_t size, int (*compare)(const void *, const void *) );
功能Q?/span> ?/span>buf 指向的数?/span>(包含num ?/span>,每项的大ؓsize)q行快速排序。如果函?/span>compare 的第一个参数小于第二个参数Q返回负|如果{于q回零|如果大于q回正倹{函数对buf 指向的数据按升序排序?/span>
 
   #include <stdlib.h>  void *bsearch( const void *key, const void *buf, size_t num, size_t size, int (*compare)(const void *, const void *) );
功能Q?/span> 函数用折半查找法在从数组元素buf[0]?/span>buf[num-1] 匚w参数key。如果函?/span>compare 的第一个参数小于第二个参数Q返回负|如果{于q回零|如果大于q回正倹{数l?/span>buf 中的元素应以升序排列。函?/span>bsearch()的返回值是指向匚w,如果没有发现匚w,q回NULL?/span>
 

struct Company {
        char number[9];
        char name[41];
        char name_jp[31];
        char region[9];
        char address[51];
        char telephonNumber[14]; // 因ؓ电话L后还跟了一个回车,所以得再多加1
};

int compare(const void* company1, const void* company2) {
        return strcmp(((struct Company*)company1)->number, ((struct Company*)company2)->number);
}

const struct Company* search(const char* number,
                const struct Company* companys,
                int numberOfCompanys) {
        int compare(const void* company1, const void* company2);
        struct Company tempCompany;
        int low = 0;
        int height = numberOfCompanys - 1;
        int middle = 0;
        if ((strlen(number) > 8) || (numberOfCompanys < 1) ||(companys == NULL) || (number == NULL)) {
                return NULL;
        }
        strcpy(tempCompany.number, number); // 构造一个时公司结构体,用于比较函数compare?
        /*while (low <= height) {
         middle = (low + height) / 2;
         if (compare(&tempCompany, &companys[middle])> 0) {
         low = middle + 1;
         } else if (compare(&tempCompany, &companys[middle])< 0) {
         height = middle - 1;
         } else if (compare(&tempCompany, &companys[middle])== 0) {
         return &companys[middle];
         }
         }
         return NULL;*/
        return (struct Company*)bsearch(&tempCompany, companys, numberOfCompanys, sizeof(struct Company),
                        compare);
}

void sort(struct Company* companys, int numberOfCompanys) {
        int compare(const void* company1, const void* company2); // 快速排序函数qsort需要的比较函数
        qsort(companys, numberOfCompanys, sizeof(struct Company), compare); // 调用pȝ提供的快速排序进行排?
}

 



]]>
C/C++Q命令行输入不回?/title><link>http://www.shnenglu.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.shnenglu.com/biao/archive/2010/12/17/136787.html</guid><wfw:comment>http://www.shnenglu.com/biao/comments/136787.html</wfw:comment><comments>http://www.shnenglu.com/biao/archive/2010/12/17/136787.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/biao/comments/commentRss/136787.html</wfw:commentRss><trackback:ping>http://www.shnenglu.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 <iostream><br style="line-height: 15px; ">#include <conio.h></div><div style="line-height: 15px; "><font color="#00ff00" style="line-height: normal; ">/**<br style="line-height: 15px; "> * U密在于conio.h中的getch()从键盘中d字符?q不会在屏幕上输出已l输入的字符,<br style="line-height: 15px; "> * 而用一个putch('*')来哄?代表已经输入一个字W?br style="line-height: 15px; "> * 怪不得这个头文g要叫conio.h, con的意思就有哄?看来是由此而来.<br style="line-height: 15px; "> */</font></div><div style="line-height: 15px; ">using namespace std;</div><div style="line-height: 15px; ">int main() {<br style="line-height: 15px; ">        char* password;<br style="line-height: 15px; ">        char* passwordConfirm;</div><div style="line-height: 15px; ">        int length = 4;<br style="line-height: 15px; ">        password = new char[length + 1];<br style="line-height: 15px; ">        passwordConfirm = new char[length + 1];</div><div style="line-height: 15px; ">        char* p = NULL;<br style="line-height: 15px; ">        int count = 0;</div><div style="line-height: 15px; ">        cout << "Input password : ";<br style="line-height: 15px; ">        p = password;<br style="line-height: 15px; ">        count = 0;<br style="line-height: 15px; ">        //fflush(stdin);<br style="line-height: 15px; ">        while (((*p = getch()) != 13) && count < length) {<br style="line-height: 15px; ">                <font color="#00ff00" style="line-height: normal; ">// q里不是'\n'(10), new line<br style="line-height: 15px; ">                // 而是'\r'(13), reback. x按下回R?好像q个东西是linux?<br style="line-height: 15px; ">                // 主要是与getchq个函数有关.<br style="line-height: 15px; "></font>                putch('*');<br style="line-height: 15px; ">                fflush(stdin);</div><div style="line-height: 15px; ">                p++;<br style="line-height: 15px; ">                count++;<br style="line-height: 15px; ">        }<br style="line-height: 15px; ">        password[count] = '\0';</div><div style="line-height: 15px; ">        cout << endl << "Confirm the password : ";<br style="line-height: 15px; ">        p = passwordConfirm;<br style="line-height: 15px; ">        count = 0;<br style="line-height: 15px; ">        //fflush(stdin);<br style="line-height: 15px; ">        while (((*p = getch()) != 13) && count < length) {<br style="line-height: 15px; ">                putch('*');<br style="line-height: 15px; ">                fflush(stdin);</div><div style="line-height: 15px; ">                p++;<br style="line-height: 15px; ">                count++;<br style="line-height: 15px; ">        }<br style="line-height: 15px; ">        passwordConfirm[count] = '\0';</div><div style="line-height: 15px; ">        cout << endl;<br style="line-height: 15px; ">        if (strcmp(password, passwordConfirm) == 0) {<br style="line-height: 15px; ">                cout << "The password is right." << endl;<br style="line-height: 15px; ">                cout << password << endl;<br style="line-height: 15px; ">        } else {<br style="line-height: 15px; ">                cout << "Confirm password fail." << endl;<br style="line-height: 15px; ">                cout << password << endl << passwordConfirm << endl;<br style="line-height: 15px; ">        }</div><div style="line-height: 15px; ">        return 0;<br style="line-height: 15px; ">}</div></span> <img src ="http://www.shnenglu.com/biao/aggbug/136787.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/biao/" target="_blank">逛奔的蜗?/a> 2010-12-17 18:12 <a href="http://www.shnenglu.com/biao/archive/2010/12/17/136787.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C/C++Q读取文件小l节http://www.shnenglu.com/biao/archive/2010/12/17/136786.html逛奔的蜗?/dc:creator>逛奔的蜗?/author>Fri, 17 Dec 2010 10:11:00 GMThttp://www.shnenglu.com/biao/archive/2010/12/17/136786.htmlhttp://www.shnenglu.com/biao/comments/136786.htmlhttp://www.shnenglu.com/biao/archive/2010/12/17/136786.html#Feedback0http://www.shnenglu.com/biao/comments/commentRss/136786.htmlhttp://www.shnenglu.com/biao/services/trackbacks/136786.html
以E中读文gӞ要非常小心,用二q制处理和用文本方式处理Q对于用文本方式写入的文Ӟ操作l果是相同的Q但是对于用二进制写入的文gQ结果却是大Z?例如用其?exe文gq行dQ?/font>
#include <iostream>
#include <ctime>
#include <fstream>
using namespace std;
int main(){
        FILE* fp;
        fp = fopen("d.txt", "wb");
        char* str = "biao";
        fwrite(str, strlen(str), 1, fp);
        fclose(fp);
        
        fp = fopen("test.exe", "rb");
        fseek(fp, 0L, SEEK_END);
        int length = ftell(fp);
        cout << "File length: " << length << endl;
        
        rewind(fp);
        char ch;
        int count = 0;
        while (!feof(fp)) {
                ch = fgetc(fp);
                //cout << ch;
                count++;
        }
        cout << endl << count << endl;
        cout << (int)ch << endl;
        cout << ftell(fp) << endl;
        
        fclose(fp);
        return 0;
}
 
文g长度?76268Q当用rbӞwhile (!feof(fp))能正常的d文g末,但是如果用r来读取,到最后,ftell(fp)Q正好是文g的长度,但用rӞwhile (!feof(fp))q不能到达文件尾Q最后ftell(fp)的值是7859,相差太多Q对囄文g也是一L.也许对于用r和rb打开的文件操作时Q对于feof(fp)来判断是否到达文件尾时的方式不一? 即用r打开Ӟfeof(fp)的判断是当读取遇到EOF?1Ӟ判断为文件结束,注意Q即使是用rb方式打开文gQ如果判断的时候不用while(!feof(fp))而用((ch = fgetc(fp)) != EOF)Q一L是读取遇到EOF时@环就不会l箋q行Q即EOF只适合于文本试Q但feof(fp)却会Ҏ是用文本方式q是用二q制方式来判断自动用不同的判断是否到达文g?
 
所以更通用一些的判断文gl束方式用feof.
 
通过对fread(buf, size, num, fp)的测试,当size * num相同Ӟ?font color="#00ff00" style="line-height: normal; ">fread(buf, 255, 1, fp)与fread(buf, 1, 255, fp)的速度相差非常d210M的二q制文g用了9U多一些(~冲|成256B, 128, 256, 1024时的速度都差不多Q但?00K时却?3U,看来q是缓冲区大好Q但是缓冲区大时Q可以减对盘q行操作的次敎ͼ.
 
如用rb(二进制方?把结构体的数据写入文件时Q不会生额外的数据Q用fseek(fp, 0L, SEEK_END), ftell(fp),取得的长度正好是l构的长度*个数Q但是用r(文本方式)把结构体的数据写入文件时Q却会生一些客外的数据Q此Ӟ如果想从文g中从后向前读取结构体的数据,׃能正常得到所有数据,好在从前向后L有问?以防万一Q应该用二进制方式来操作Q这P卛_以从前向后读Q也可以从后向前?在fseek中,可以用负数来相对于当前文件指针,把文件指针向前移?fseek(fp, -sizeof(STRUCT), SEEK_CUR).



]]>
C/C++Q~(~0 << 6)http://www.shnenglu.com/biao/archive/2010/12/17/136785.html逛奔的蜗?/dc:creator>逛奔的蜗?/author>Fri, 17 Dec 2010 10:10:00 GMThttp://www.shnenglu.com/biao/archive/2010/12/17/136785.htmlhttp://www.shnenglu.com/biao/comments/136785.htmlhttp://www.shnenglu.com/biao/archive/2010/12/17/136785.html#Feedback0http://www.shnenglu.com/biao/comments/commentRss/136785.htmlhttp://www.shnenglu.com/biao/services/trackbacks/136785.html
怎样使得一个数从右向左4~9位全?:
int a = 222222;
int temp = ~(~0 << 6);// ~0 使用数的每一位全?, 左移六位Q得最双6位全?, 再取反,例前面全?,后面6位ؓ1.
a >>= 4;
a &= temp;
a <<= 4;
 
32 bits binary nummer:
#include <iostream>
using namespace std;
int main() {
        int ref = 1 << 31;
        int a = 14;
        
        for (int i = 0; i < 32; i++) {
                cout << (a & ref? '1': '0');
                a <<= 1;
        }
        
        return 0;
}


]]>
JavaQ强大的Eclipsehttp://www.shnenglu.com/biao/archive/2010/12/17/136784.html逛奔的蜗?/dc:creator>逛奔的蜗?/author>Fri, 17 Dec 2010 10:09:00 GMThttp://www.shnenglu.com/biao/archive/2010/12/17/136784.htmlhttp://www.shnenglu.com/biao/comments/136784.htmlhttp://www.shnenglu.com/biao/archive/2010/12/17/136784.html#Feedback0http://www.shnenglu.com/biao/comments/commentRss/136784.htmlhttp://www.shnenglu.com/biao/services/trackbacks/136784.html
在Eclipse中写OpenGL, Qt, C, C++, Java, links方式安装插gQ导入工E:
Java: Eclipse要运行在jre的环境之上,自带Java环境
C, C++:  下蝲CDTQ不同版本的CDT与不同版本的Eclipse匚wQ因Z们的更新很快
 
安装CDT和EclipseӞq没有C/C++~译器,得自׃一个,对于windows有两个比较好:Cygwin和MinGWQ都是用Linux下的GCC windowsUL版,Ҏ准支持非常好Q不像windows自己的,搞得׃八糟Q不q最好是安装MinGWQ因为大多数都是支持MinGWQ而且Qt的windows安装版本只支持MinGWQ如果要用Cygwin的话Q那得自q译Qt的源码。安装MinGW的一个更好的方式Q用免费开源的Dev-CppQ其中就自带了MinGWQ更好的一ҎQ可以用他的包管理器到网l上下蝲需要的E序库,非常方便Q他q支持OpenGLQ如想用glut库,他也可以在网上自动找刎ͼQt的安装版中已l支持了OpenGLQ所以对于C/C++:
Dev-Cpp + CDT + Eclipse = 搞定.
 
OpenGL + glut: 用Dev-Cpp下蝲glut库,然后在Eclipse的工E的库中引入外部库:
opengl32, glu32, glut32, gdi32, winmm = 搞定
 
links方式安装插gQ?/font>在Eclipse的目录下Z个links文g夹,在里面Q意徏文gxxxx.linkQ在xxxx.link里写上插件所在目录:
path=D:/Software/Eclipse-SDK-3.3M6-win32/CDT-4.0.0-M7-win32.x86Q这里必L意,其中的文件分隔符用的是linux的文件分隔符/Q而不是windows的文件分隔符\Q写错了插g显CZ出来...开始的时候用\Q晕ingQ插件没昄出来Q还好不傻,试了一?Q嘿嘿,出来了?/div>
 
Qt: 先装CDTQ然后下载Qt Eclipse Integration安装Q直接搞定。对于新加入?h, .cpp文gQ?pro文g会自动更新?/div>
 
完整安装序Q?/font>
JDK + Eclipse + Dev-Cpp(MinGW...package tool + glut / boost==) + CDT(links安装) + Qt4.3 + Qt Eclipse Integration


]]>C/C++QEffective C++http://www.shnenglu.com/biao/archive/2010/12/17/136782.html逛奔的蜗?/dc:creator>逛奔的蜗?/author>Fri, 17 Dec 2010 10:07:00 GMThttp://www.shnenglu.com/biao/archive/2010/12/17/136782.htmlhttp://www.shnenglu.com/biao/comments/136782.htmlhttp://www.shnenglu.com/biao/archive/2010/12/17/136782.html#Feedback0http://www.shnenglu.com/biao/comments/commentRss/136782.htmlhttp://www.shnenglu.com/biao/services/trackbacks/136782.html
1.   量用const和inlineQ而不?defineQ即量用编译器Q而不用预处理器?/div>
2.   量用iostreamQ而不用stdio.h?/div>
3.   量用new和deleteQ而不用malloc和freeQ因为new和delete会执行构造函数和析构函数Q而malloc和free不会?/div>
4.   量在函C使用C++风格的注?// 函数头用/* */
5.   new和delete与malloc和free要成对出现?/div>
6.   析构函数里对指针成员deleteQdelete NULL指针q不影响什么?/div>
7.   预先准备好内在不够的情况Qtry{ new..} catch(bad_alloc baex) {}
8.   写operator new和operator delete时要遵@常规?/div>
9.   如果写了operator newQ就要写operator delete?/div>
10. 为需要动态分配内存的cL供一个复制构造函数和赋值操作运符?/div>
11. 量使用初始化,而不在构造函数里赋倹{CC::CC(int v_value) : value(v_value) {}
12. 初始化列表中成员的列出顺序与成员的在cM的声明顺序相同?/div>
13. 定基类有虚析构函数Q否则释攑֭cL只能执行基类的析构函敎ͼ而不会执行子cȝ虚构函数?/div>
14. 让operator= q回*this的引用, (a = b) = c;只是Zq个能正执行,不过q可是一个很阿格里拉的编E风根{?/div>
15. 在operator= 中对所有数据成员赋倹{?/div>
16. 在operator= 中检查对自己赋值的情况。如不处理这U情况,在operator= 中先释放指针对象数据Q?/div>
     然后再把目标的数据复制过来,因ؓ是自q自己复制Q但数据已经释放Q所以发生错误?/div>
17. 争取使类的接口完整ƈ最?/div>
18. 分清成员函数Q非成员函数和友元函数?/div>
19. 避免public接口出现数据成员。set, get.
20. 可能用const?/div>
21. 量传引用,而不是传倹{const ClassName&?/div>
22. 必须q回一个对象时Q不要试图返回一个引用。如函数中的临时变量的引用?/div>
23. 在函数重载和讑֮参数~省值的函数之间慎重选择?/div>
24. 避免指针和数字类型重载:void func(int x)与void func(string* str)传入0Ӟfunc(static_cast<string*>(null)?/div>
25. 如果不想使用隐式生成的函敎ͼ显C的用它:private Array& operator=(const Array& ref);
26. 使用名字I间的好处:可能的避免名字冲突引发的问题。假如一个程序中有几百个名字冲突Q名字空间能很好的解册个问题?/div>
27. 避免q回内部数据的句柄:指针?/div>
28. 避免q回private和protected成员的非const引用和指针?/div>
29. 千万不要q回局部对象的引用Q也不要q回函数内部定义的new初始化的指针Q容易忘掉delete?/div>
30. 可能推q变量的定义Q这个在C中做不到Q定义无用的变量必然伴随无用的开销?/div>
31. 文仉的编译依赖性降x低:分离的关键在于:对类的声明的依赖与对cȝ定义的依赖:
     量让头文g不要依赖于另的文件。如使用ImageLoad.h中的Imagec,在类的声明中不要#include"ImageLoad.h",
     而只是声明类class Image; 在类的定义中才?include"ImageLoad.h"?/div>
     量不要在头文g中包含其他的头文件?/div>
32. 使用函数体现“是一?#8221;的含义,而不?#8220;有一?#8221;?/div>
33. 明智的用多l承Q这东西不好玩?/div>
34. ~译器能为我们自动生成的函数Q?/div>
     构造函敎ͼ析构函数Q复制构造函敎ͼ赋D符重蝲Q一对取地址q算W?/div>
35. 宁可~译链接时出错,也不愿运行时出错。极情况下会用C++抛出异常Q内存耗尽Q?/div>
     C++中没有上溢,下溢Q除零检查,数组界查?/div>
36. 重视~译器警告?/div>
37. 熟悉标准库,在名字空间std中的。C++的伪标准库,如iostream.h, limits.h?/div>
38. 常变量,cL员,父类构造函数要攑֜初始化列表初始化?/div>
39. 指针定义时初始化为NULL?/div>
40. l极解决大法Q良好的~译习惯可很避免上面很多情况的发生,如有一天C++的语法变得像Java一样限刉么多Q?/div>
     ׃会出现这么多问题Q不q也正因为如此,才出CN多C++奇淫技巧,如茴香豆的茴有四种写法Q?/div>
     q里可不止四U?/div>

]]>C/C++QLittle-endian oder Big-endian --> CPUhttp://www.shnenglu.com/biao/archive/2010/12/17/136781.html逛奔的蜗?/dc:creator>逛奔的蜗?/author>Fri, 17 Dec 2010 10:07:00 GMThttp://www.shnenglu.com/biao/archive/2010/12/17/136781.htmlhttp://www.shnenglu.com/biao/comments/136781.htmlhttp://www.shnenglu.com/biao/archive/2010/12/17/136781.html#Feedback0http://www.shnenglu.com/biao/comments/commentRss/136781.htmlhttp://www.shnenglu.com/biao/services/trackbacks/136781.html
怎么定CPUҎ作数的存放方式,用的是Little-endianQ从低字节到高字节)q是Big-endianQ?/div>
呵呵Q如果知道怎么用联合体的话Q再加上你是个天才:能几分钟内搞个来的就是天才,天才也能q样定义Q出题的人大脑有问题Q嘿嘿,q问题好像很Ҏ啊)Q?/div>
union uT{
        short a;
        char b;
}u;
u.a = 0x1234;
 
cout << u.a << endl;
cout << (int)u.b << endl; // 如果u.b == 0x12,则采用的是Little-endianQ因合体占用同一个内存空间存放成员?/font>
                                   // 我的CPU用的是Big-endianQ从高字节到低字节,因ؓu.b = 52QTGA的文件也是这样存的?/font>
                                   // 联合体的成员都是按地址序从低地址向高地址存放?/font>


]]>C/C++Q读取文?/title><link>http://www.shnenglu.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.shnenglu.com/biao/archive/2010/12/17/136775.html</guid><wfw:comment>http://www.shnenglu.com/biao/comments/136775.html</wfw:comment><comments>http://www.shnenglu.com/biao/archive/2010/12/17/136775.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/biao/comments/commentRss/136775.html</wfw:commentRss><trackback:ping>http://www.shnenglu.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 <iostream><br style="line-height: 15px; ">#include <cmath><br style="line-height: 15px; ">#include <stdio.h></div><div style="line-height: 15px; "><br style="line-height: 15px; ">using namespace std;</div><div style="line-height: 15px; "> </div><div style="line-height: 15px; ">int main(){<br style="line-height: 15px; ">         FILE* file = NULL;</div><div style="line-height: 15px; "><br style="line-height: 15px; ">         file = fopen("main.cpp", "r");</div><div style="line-height: 15px; ">         if (NULL == file) {</div><div style="line-height: 15px; ">                  cerr << "Cann't open the file." << endl;</div><div style="line-height: 15px; ">                  return 1;</div><div style="line-height: 15px; ">         }<br style="line-height: 15px; "><br style="line-height: 15px; ">         char** line = NULL;<br style="line-height: 15px; ">         line = new char*[60];<br style="line-height: 15px; ">         for (int i = 0; i < 60; i++) {<br style="line-height: 15px; ">                  line[i] = new char[80];<br style="line-height: 15px; ">                 <font color="#ff9900" style="line-height: normal; "> line[i][0] = '\0';</font> <font color="#00ff00" style="line-height: normal; ">// 关键在q里Q如果不把每个串的第一个字W设?Q则在输出时Q如果源文g的最后一行是IQ?/font></div><div style="line-height: 15px; "><font color="#00ff00" style="line-height: normal; ">                                       // 则最后一行是两个字符的ؕ?x ?=)Q这有可能是跟读到文件末时的文gl束标志有关?br style="line-height: 15px; "></font>         }<br style="line-height: 15px; "> <br style="line-height: 15px; ">         int row = 0;<br style="line-height: 15px; ">         while (!feof(file)) {<br style="line-height: 15px; ">                  fgets(line[row++], 80, file);  // 每读取一行后Q记录读取的行数加一?br style="line-height: 15px; ">         }<br style="line-height: 15px; ">         fclose(file);</div><div style="line-height: 15px; "><br style="line-height: 15px; ">         for (int i = 0; i < row; i++) {<br style="line-height: 15px; ">                  cout << line[i];<br style="line-height: 15px; ">         }</div><div style="line-height: 15px; "><br style="line-height: 15px; ">         return 0; <br style="line-height: 15px; ">}</div></span> <img src ="http://www.shnenglu.com/biao/aggbug/136775.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/biao/" target="_blank">逛奔的蜗?/a> 2010-12-17 17:54 <a href="http://www.shnenglu.com/biao/archive/2010/12/17/136775.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C/C++Q向函数传递二l指针的ȝhttp://www.shnenglu.com/biao/archive/2010/12/17/136773.html逛奔的蜗?/dc:creator>逛奔的蜗?/author>Fri, 17 Dec 2010 09:52:00 GMThttp://www.shnenglu.com/biao/archive/2010/12/17/136773.htmlhttp://www.shnenglu.com/biao/comments/136773.htmlhttp://www.shnenglu.com/biao/archive/2010/12/17/136773.html#Feedback0http://www.shnenglu.com/biao/comments/commentRss/136773.htmlhttp://www.shnenglu.com/biao/services/trackbacks/136773.html
array[3][3],q时Qarray占有9个连l的内存单元Q地址是连l的Q如果真可以直接单的传给指向指针的指针的Q则二维指针取下一个元素地址是单的加法了,但是如果传入的指针是int** p; p = new int*[3];呢?
for (int i = 0; i < 3; i++) {
        *(p + i) = new int[3];
}
q时二维指针p的元素占有的9个内存单元却不能保证是连l的Q只能保证有每行?个是q箋的)Q取下一行地址Ӟ׃能如上面的那L单的直接相加了(上一行末元素的地址直接?Q,所以得传入一个指向指针的指针才行Q如int* p[3]Q存储的是每行的指针Q或者int** pQؓp初始化,分配I间;
所以int** p 和int p[row][column]l对不是一回事?/div>

像下面这样可以正运行:
#include <iostream>
#include <stdio.h>
using namespace std;
void func(int** array, int row, int column) {
         for (int i = 0; i < row; i++) {
                for (int j = 0; j < column; j++) {
                        cout << *(*(array+i) + j) << "\t";
                }
                cout << endl;
        }
}

int main(){
        int array[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
        
        int* p[3];
        p[0] = array[0];
        p[1] = array[1];
        p[2] = array[2];
        
        func(p, 3, 3); // 如果q里传入array, array[0], &array[0][0]Q则~译不通过
        // 传入int* q = array[0]; int** p = &q;~译通过Q但q行时要么错退出,要么输出的结果是错的?br style="line-height: 15px; ">        
        return 0;
}


]]>C/C++Q八皇后http://www.shnenglu.com/biao/archive/2010/12/17/136772.html逛奔的蜗?/dc:creator>逛奔的蜗?/author>Fri, 17 Dec 2010 09:51:00 GMThttp://www.shnenglu.com/biao/archive/2010/12/17/136772.htmlhttp://www.shnenglu.com/biao/comments/136772.htmlhttp://www.shnenglu.com/biao/archive/2010/12/17/136772.html#Feedback0http://www.shnenglu.com/biao/comments/commentRss/136772.htmlhttp://www.shnenglu.com/biao/services/trackbacks/136772.html

#include <iostream>
#include <cstdio>
#include <ctime>
#include <cmath>

using namespace std;

void queen(int** array, int row, FILE* fp);
bool isAvailable(int** array, int y, int x);
bool isResult(int** array);

int main(int arg, char** argv) {
        int** array;
        
        // 分配I间
        array = new int*[8];
        for (int i = 0; i < 8; i++) {
                array[i] = new int[8];
        }
        
        // 初始化皇后状?/font>
        for (int i = 0; i < 8; i++) {
                for (int j = 0; j < 8; j++) {
                        array[i][j] = 0;
                }
        }
        
        FILE* fp = fopen("data.txt", "w");
        queen(array, 0, fp);
        fclose(fp);
        
        // 释放I间
        for (int i = 0; i < 8; i++) {
                delete[] array[i];
        }
        delete array;
        array = NULL;

        return 0;
}

void queen(int** array, int row, FILE* fp) {
        static int size = 1;
        if (row > 7) {
                return;
        }
        for (int j = 0; j < 8; j++) {
                // 如果合法
                if (isAvailable(array, row, j)) {
                        array[row][j] = 1;
                        
                        // 当前位置合法Q赋值后Q进入下一?br style="line-height: 15px; ">                        queen(array, row + 1, fp);

                        // 如果退回来后是l果Q则打印出来
                        if (isResult(array)) {
                                // 把结果写入文?br style="line-height: 15px; ">                                fputc('\n', fp);
                                fputs("***********************************", fp);
                                fprintf(fp, " %d ", size++);
                                fputs("***********************************", fp);
                                fputc('\n', fp);
                                for (int m = 0; m < 8; m++) {
                                        for (int n = 0; n < 8; n++) {
                                                if (array[m][n] == 1) {
                                                        fputc('#', fp);
                                                        fputc(' ', fp);
                                                        fputc(' ', fp);
                                                } else {
                                                        fputc('O', fp);
                                                        fputc(' ', fp);
                                                        fputc(' ', fp);
                                                }
                                        }
                                        fputc('\n', fp); 
                                }

                        }
                        array[row][j] = 0;

                }
        }
}

bool isAvailable(int** array, int row, int column) { // Wrow行,column?br style="line-height: 15px; ">        int i, j;

        // 查row?/font>
        for (i = 0; i < 8; i++) {
                if (array[row][i] != 0) {
                        return false;
                }
        }

        // columnx?/font>
        for (j = 0; j < 8; j++) {
                if (array[j][column] != 0) {
                        return false;
                }
        }

        // 查对角向左上
        i = row;
        j = column;
        while (i >= 0 && j >= 0) {
                if (array[i][j] != 0) {
                        return false;
                }
                i--;
                j--;
        }

        // 查对角向右上
        i = row;
        j = column;
        while (i >= 0 && j < 8) {
                if (array[i][j] != 0) {
                        return false;
                }
                i--;
                j++;
        }

        // 查对角向左下
        i = row;
        j = column;
        while (i < 8 && j >= 0) {
                if (array[i][j] != 0) {
                        return false;
                }
                i++;
                j--;
        }

        // 查对角向右下
        i = row;
        j = column;
        while (i < 8 && j < 8) {
                if (array[i][j] != 0) {
                        return false;
                }
                i++;
                j++;
        }

        return true;
}

bool isResult(int** array) {
        int size = 0; // 皇后个数

        // l计皇后个数
        for (int i = 0; i < 8; i++) {
                for (int j = 0; j < 8; j++) {
                        if (array[i][j] != 0) {
                                size++;
                        }
                }
        }

        // 没有Q个皇后Q说明不是结?/font>

       // ?个皇?因ؓ每个皇后的放|都是在合法位置, 所以只需要简单的计算皇后个数q道是不是l果.
        if (size != 8) {
                return false;
        }

        return true;
}



]]>
C/C++QC中随写文?/title><link>http://www.shnenglu.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.shnenglu.com/biao/archive/2010/12/17/136766.html</guid><wfw:comment>http://www.shnenglu.com/biao/comments/136766.html</wfw:comment><comments>http://www.shnenglu.com/biao/archive/2010/12/17/136766.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/biao/comments/commentRss/136766.html</wfw:commentRss><trackback:ping>http://www.shnenglu.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 <iostream><br style="line-height: 15px; ">#include <cstdio></div><div style="line-height: 15px; "> </div><div style="line-height: 15px; ">using namespace std;</div><div style="line-height: 15px; "> </div><div style="line-height: 15px; ">int main(int argc, char** argv) {<br style="line-height: 15px; ">        FILE* fp = NULL;<br style="line-height: 15px; ">        fp = fopen("data.txt", "w");</div><div style="line-height: 15px; ">        fputs("gooooooooogle", fp);<br style="line-height: 15px; ">        fclose(fp);<br style="line-height: 15px; ">        fp = NULL;</div><div style="line-height: 15px; ">        fp = fopen("data.txt", "r+");</div><div style="line-height: 15px; ">        <font color="#00ff00" style="line-height: normal; ">// d修改前的内容<br style="line-height: 15px; "></font>        char str[50];<br style="line-height: 15px; ">        cout << "------------修改前的内容---------" << endl;<br style="line-height: 15px; ">        cout << fgets(str, 50, fp) << endl;</div><div style="line-height: 15px; "> </div><div style="line-height: 15px; ">        <font color="#00ff00" style="line-height: normal; ">// 随机修改文g.<br style="line-height: 15px; "></font>        rewind(fp);<br style="line-height: 15px; ">        fseek(fp, 3L, SEEK_SET);<br style="line-height: 15px; ">        //cout << ftell(fp) << endl;<br style="line-height: 15px; ">        fputc('A', fp);<br style="line-height: 15px; ">        fputc('B', fp);<br style="line-height: 15px; ">        fseek(fp, 1L, SEEK_CUR);<br style="line-height: 15px; ">        fputc('C', fp);</div><div style="line-height: 15px; ">        <font color="#00ff00" style="line-height: normal; ">// d修改后的内容<br style="line-height: 15px; "></font>        rewind(fp);<br style="line-height: 15px; ">        cout << "------------修改后的内容---------" << endl;<br style="line-height: 15px; ">        cout << fgets(str, 50, fp) << endl;</div><div style="line-height: 15px; ">        fclose(fp);<br style="line-height: 15px; ">        return 0;<br style="line-height: 15px; ">}</div><div style="line-height: 15px; "> </div><div style="line-height: 15px; "><font color="#00ff00" style="line-height: normal; ">/**<br style="line-height: 15px; "> * 不能用fopen(fileName, "w+"); // 如果文g不存?创徏文g,如果存在,则清I?然后d<br style="line-height: 15px; "> * fopen(fileName, "a+"); // 只能写到文g?但可以读?<br style="line-height: 15px; "> * fopen(fileName, "r+"); // 可以d, 如果文g不存? 则发生错?可以实现随机d.<br style="line-height: 15px; "> */</font></div></span> <img src ="http://www.shnenglu.com/biao/aggbug/136766.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/biao/" target="_blank">逛奔的蜗?/a> 2010-12-17 17:48 <a href="http://www.shnenglu.com/biao/archive/2010/12/17/136766.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C/C++QOverload + Templatehttp://www.shnenglu.com/biao/archive/2010/12/17/136757.html逛奔的蜗?/dc:creator>逛奔的蜗?/author>Fri, 17 Dec 2010 09:30:00 GMThttp://www.shnenglu.com/biao/archive/2010/12/17/136757.htmlhttp://www.shnenglu.com/biao/comments/136757.htmlhttp://www.shnenglu.com/biao/archive/2010/12/17/136757.html#Feedback0http://www.shnenglu.com/biao/comments/commentRss/136757.htmlhttp://www.shnenglu.com/biao/services/trackbacks/136757.html
函数重蝲Q?/font>
函数名字相同Q最v码是参数的类型不一P参数个数不一样那更好了,但是不能只是q回cd不一栗?/font>
void func(int n) {...};
void func(float n) {...};
一赯用:
func(10);
func(20.001);
q时~译不通过Q提C重载函数的调用不清楚,因ؓW二个可能会把float强制转换为intQ所以必L的强制指定为float才行Q下面的调用~译通过Q?/font>
func(10);
func((float)20.001); 或?func(20.00f);
 
重蝲函数通过{֐(signature)q行识别Q签名是函数名和参数cd的组合?/font>
~译器用函数参个数和cd~码每个函数标识W?或名字改~?Q以保证cd安全q接。如Borland C++中,每个
改编名用 @加上函数?/font>
改编参数?$q 开?/font>
zc 表示 char, pf ?float*, pd ?double*, i ?int, pi, f, d,
?void nothing(int a, float b, char c, double *d); 的改~名?/font>
@nothing$qifzcpd
 
重蝲函数通常用于不同cd用不同程序逻辑q行cM的操作?/font>
如果每种数据cd的程序逻辑和操作相同,那么使用函数模板会更好一些?/font>
template <class T>
T max(T a, T b, T c) {
        T max;
        max= a > b? a : b;
        max = max > c? max : c;
        return max;
}
 


]]>
C/C++QArray and Pointerhttp://www.shnenglu.com/biao/archive/2010/12/17/136756.html逛奔的蜗?/dc:creator>逛奔的蜗?/author>Fri, 17 Dec 2010 09:30:00 GMThttp://www.shnenglu.com/biao/archive/2010/12/17/136756.htmlhttp://www.shnenglu.com/biao/comments/136756.htmlhttp://www.shnenglu.com/biao/archive/2010/12/17/136756.html#Feedback0http://www.shnenglu.com/biao/comments/commentRss/136756.htmlhttp://www.shnenglu.com/biao/services/trackbacks/136756.html
数组和指针这东西有时q是比较ȝQ?/font>
指针是很危险的,但同时也是非常强大的Q?/font>如一个高手拿AWP和一个菜鸟拿AWP一P一个是最恐怖的鬼Q另一个却是被虐的对象?/font>
const int *p ?int const *p 指的是数l的内容不能改变?/font>
int * const p 指的是指针名不能改变。谁?const 后面Q谁是不变的?/font>
 
二维数组动态分配:
int **p;
int row = 5;
int column = 10;
p = new int*[row];
for (int i=0; i<row; i++) {
        p[i] = new int[column];
}
 
指针x地址(0xf0765fea)Q是无符h敎ͼ占用四个字节Q3Q位?/font>怎么判断一个指针所指向的类型?
L一个 * 和变量名后所留下的部分就是指针所指向的类型?/font>
?int *p; L一?* 和变量名 p 后就只有 int 了,所?p 指向 int 的整形数?/font>
float **p; ?p 指向的类型是 float *Q即 float cd的指针,?float cd的地址Q也xl?/font>
那么在给 p 分配内存Ӟp = new float*[23]Q即指向一?/font>
在E或EQ+中有一个很好的地方是,即你定义的变量是指针,但可与用讉K数组下标的方法来讉K指针所指向的数据;int **p;   *(*(p+i) + j) {同?p[i][j]Q用数l下标来讉K更直观?/font>
 
使用变量来声明数l的长度QɽE序更具有健壮? int length = 10; int array[length].
l定一个二l数l和一个位|数locQ怎么定q个数对应数l中的那一个位|?
array[loc / column][loc % column]
 
当定义一个数l时Q但没有初始化,q时数组的内Ҏq是原来内存中的倹{?/font>
 
void * 的?/font>
可以把Q何类型的指针直接传给 void* 的变量?/font>
但是反过来,却不能直接把 void* 传给其他cd指针Q必L的q行强制cd转换
int *pi;
void * pv = pi; // Ok
float *pf = (float*)pv; // Ok
double *pd = pv; // Error
 
char *str = (char*)malloc(sizeof(char) * number);
malloc分配一D内存,q回q段内存的首地址Q其gؓ void *Q这P不管什么类型的指针都可以用q个函数来进行内存分配,void * 实现了相当于 C++ 中的函数模板的功能,实现了在Q中的多态。各U类型说明符Q就是ؓ了说明每ơ进行存取的时候操作多字节?/font>
 
数组名是q个数组的首地址Q与他的W一个元素的地址是指向同一个地址Q但数组却是一个常量,只能q行叛_D,是不可改变的。指针变量也是指向地址Q但指针变量是可以改变的Q可以进行右D,也可以左D?/font>
 
字符数组的结束符h'\0'Q其整型值是 0Q即 NULL
虽然q样Q但在OpenGL中,如在 glGenTextures 的调用中Q经常用的是W一个元素的地址Q而不是用数组名?/font>
 
用数l实现折半查找:
在最坏的情况下,1024个元素只需要进?0查找Q但查找前要先排?/font>
q回要查扄数在数组的下标,当不在数l中是,q回 -1Q因为数l下标是从0开始的?/font>
int binarySearch(const int *p, const int g, int low, int height) {
         int mid;
 
         while (low <= height) {
                 mid = (low + height) / 2;
                 if (g < p[mid]) {
                          height = mid - 1;
                 } else if (g > p[mid]) {
                          low = mid + 1;
                 } else {
                          return mid;
                 }
          }
 
          return -1;
}
 
快速排序:
void quickSort(int *p, int low, int height) {
          if (low >= height) {
                  return;
          }
 
          const int l = low;
          const int h = height;
          int ref = p[(low + height)/2];
          int temp = 0;
 
          // 如果没有{于q个条gQ那么如 3, 2, 1Ӟ׃造成下一ơ排序ؓ1,2 ; 2, 3Q多出来一?2
          while (low <= height) {
                    // 扑ֈ大于{于 ref 的数Q这里很重要Q如果用 <= 的话Q即扑ֈ?ref 大的敎ͼ
                  // 可能会造成数组界
                    while (p[low] < ref) { 
                              low++;
                    }  
                    while (p[height] > ref) {
                              height--;
                    }
  
                    // q个条g是必ȝQ否则不能正排序, lQ,Q,Q,Q,Q,Q,hQ? 3为ref
                    // W一ơ排序后Q?, 3, l4, 2, h2, 3, 5
                    // W二ơ排序后Q?, 3, 2, lh2, 4, 3, 5Q这时还可以q行下一ơ排?/font>
                    // W三ơ排序,h仍在2处,但l要找大于{于他的敎ͼ?, 3, 2, h2, l4, 3, 5
                    // 如果没有q个条gQ那么就要进行交换,1, 3, h2, 4, 2, l3, 5Q有错误发生
                    if (low <= height) {
                              temp = p[low];
                              p[low] = p[height];
                              p[height] = temp;
                              low++;
                              height--;
                    }
  
          }
 
          quickSort(p, low, h);
          quickSort(p, l, height);
          // W一ơ压?low, height)
     // 从栈中取Z?low, height)q行每个片段的排?/font>
          // 每排完一遍后Q即上面的while执行完一ơ,压入(low,h)?l,height)
     // 利用栈消除递归
 
          return;
}
 
排序一般当元素个数大于三十个时利用快速排序的速度比较快,当元素个数小于三十个Ӟ利用选择排序比较好,所以一般都是选择排序和快速排序结合v来用?/font>


]]>
C/C++Q函数指?/title><link>http://www.shnenglu.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.shnenglu.com/biao/archive/2010/12/17/136754.html</guid><wfw:comment>http://www.shnenglu.com/biao/comments/136754.html</wfw:comment><comments>http://www.shnenglu.com/biao/archive/2010/12/17/136754.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/biao/comments/commentRss/136754.html</wfw:commentRss><trackback:ping>http://www.shnenglu.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; ">函数指针Q?/font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; ">函数名就是函数操作代码在内存中的起始地址Q就像数l名是数l在内存中的起始地址?/font></div><div style="line-height: 15px; "><font color="#dba04f" style="line-height: normal; ">所以用函数指针时Q直接传入函数名׃么了。函数指针也是E中实现多态的一U方法?/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 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 << pf(x) << endl;}</font></div><div style="line-height: 15px; "><font color="#00ff00" style="line-height: normal; ">== void func(int (*pf)(int), int x) { cout << (*pf)(x) << 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> </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> // 呵呵Q头晕吧</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 color="#ff9900" style="line-height: normal; ">void (*pf[10])(int);</font> // 表示pf?0个函数指针的数组</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每个选项׃同的函数提供服务Q每个函数的指针存放在函数指针数l中。用户选项作ؓ数组下标Q数l中的指针用于调用这个函数?/font></div><div style="line-height: 15px; "><font color="#ff9900" style="line-height: normal; ">void Q?pf[3])(int) = {func1, func2, func3};</font></div></span><img src ="http://www.shnenglu.com/biao/aggbug/136754.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/biao/" target="_blank">逛奔的蜗?/a> 2010-12-17 17:27 <a href="http://www.shnenglu.com/biao/archive/2010/12/17/136754.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>CQ在l端里输出正玄余?/title><link>http://www.shnenglu.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.shnenglu.com/biao/archive/2010/11/07/132831.html</guid><wfw:comment>http://www.shnenglu.com/biao/comments/132831.html</wfw:comment><comments>http://www.shnenglu.com/biao/archive/2010/11/07/132831.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/biao/comments/commentRss/132831.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/biao/services/trackbacks/132831.html</trackback:ping><description><![CDATA[ 因ؓ在终端里输出Q只能往前,往下输出的Ҏ,所以要Uȝ对称性来实现输出正玄Q余玄图案? <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><stdio.h></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"><math.h></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">    <span style="color: #a1006a">double</span> y;</p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">    <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">    </p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #2f9570"><span style="color: #000000">    </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">    </span>/* 要分成两个部分来计算输出 */</p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">    <span style="color: #a1006a">for</span> (y = 1; y > 0; y -= 0.1) {</p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">        x = asin(y) * 10;</p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; min-height: 19.0px">        </p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">        <span style="color: #a1006a">for</span> (i = 1; i < x; ++i) printf(<span style="color: #5000ff">" "</span>);</p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">        printf(<span style="color: #5000ff">"*"</span>);</p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">        <span style="color: #a1006a">for</span> (; i < 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">        printf(<span style="color: #5000ff">"*\n"</span>);</p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">    }</p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; min-height: 19.0px">    </p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">    <span style="color: #a1006a">for</span> (y = 0; y >= -1; y -= 0.1) {</p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">        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">        </p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">        <span style="color: #a1006a">for</span> (i = 1; i < 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">        printf(<span style="color: #5000ff">"*"</span>);</p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">        <span style="color: #a1006a">for</span> (; i < 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">        printf(<span style="color: #5000ff">"*\n"</span>);</p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">    }</p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; min-height: 19.0px">    </p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">    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">    </p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #2f9570"><span style="color: #000000">    </span>/* 输出余玄, 单调区间 x: [0, pi], y: [1, -1] */</p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">    <span style="color: #a1006a">for</span> (y = 1; y >= -1; y -= 0.1) {</p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">        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">        </p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">        <span style="color: #a1006a">for</span> (i = 0; i <= 64; ++i) {</p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">            <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">                printf(<span style="color: #5000ff">"*"</span>);</p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">            } <span style="color: #a1006a">else</span> {</p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">                printf(<span style="color: #5000ff">" "</span>);</p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">            }</p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">        }</p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">        printf(<span style="color: #5000ff">"\n"</span>);</p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">    }</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.shnenglu.com/biao/aggbug/132831.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/biao/" target="_blank">逛奔的蜗?/a> 2010-11-07 06:40 <a href="http://www.shnenglu.com/biao/archive/2010/11/07/132831.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C++QSnow Leopard里安装OpenCV 2.1 (折磨人的东西)http://www.shnenglu.com/biao/archive/2010/10/30/131849.html逛奔的蜗?/dc:creator>逛奔的蜗?/author>Sat, 30 Oct 2010 08:05:00 GMThttp://www.shnenglu.com/biao/archive/2010/10/30/131849.htmlhttp://www.shnenglu.com/biao/comments/131849.htmlhttp://www.shnenglu.com/biao/archive/2010/10/30/131849.html#Feedback0http://www.shnenglu.com/biao/comments/commentRss/131849.htmlhttp://www.shnenglu.com/biao/services/trackbacks/131849.htmlhttp://sites.google.com/site/michaelsafyan/setup-guide/mac-os-x-10-6-snow-leopard
1. 安装MacPortQ?nbsp;下蝲Download MacPorts的dmgQ直接安?/strong>
2. Install PkgConfig using MacPorts: sudo port install pkgconfig
3. Install CMake using MacPorts:: sudo port install cmake
4. Configure PKG_CONFIG_PATH: 
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:/usr/lib/pkgconfig:/usr/X11/lib/pkgconfig
export PKG_CONFIG_PATH=~/.local/lib/pkgconfig:/opt/local/lib/pkgconfig:$PKG_CONFIG_PATH
echo "export PKG_CONFIG_PATH=$PKG_CONFIG_PATH" >> ~/.profile
在安装cmake时要注意Q因为在Snow Leopard 10.6.4中因为JDK已经升q了Q在sudo port install cmake时会提示错误Q解x法:

I am in a different position with respect to this fix. I had installed cmake 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.

1) Create directory

/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Headers

2) Copy entire contents of directory

/Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Headers

to directory

/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Headers

After this fix the upgrade to cmake 2.8.2_4 worked. However, if the macports 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.

From: https://trac.macports.org/ticket/26934


5. 最l大戏来了,安装OpenCV: http://sunny.in.th/2010/04/27/installing-opencv-21-on-snow-leopard.html

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.

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.

To install OpenCV check out the code from the svn repository and compile it.

$ svn co https://code.ros.org/svn/opencv/trunk/opencv
$ cd opencv
$ mkdir build 
$ cd build 
$ cmake .. 

Configure the make by

$ ccmake .

If you want you can build the samples as well.

ccmake

Press c to configure, followed by g to generate. Next build and install OpenCV by

$ make -j8
$ sudo make install

That’s it!!! Simple right.

If you want to use Python with OpenCV there’s a little more that needs to be done. By default OpenCV copies the shared object file required by the Python interface to /usr/local/lib/python2.6/site-packages/cv.so which is not under Python path. Add the following line to your .bashrc or .bash_profile

PYTHONPATH=/usr/local/lib/python2.6/site-packages/cv.so:$PYTHONPATH

Trying running some C samples which are in /usr/local/share/opencv/samples/c/. The Python samples at/usr/local/share/opencv/samples/python/ doesn’t work since they are based on the old SWIG-Python interface. Try running the sample Python code which is in the opencv directory that was checked out from subversion. It’s insidesamples/python.

 



]]>
C++QType Castinghttp://www.shnenglu.com/biao/archive/2010/10/08/128986.html逛奔的蜗?/dc:creator>逛奔的蜗?/author>Thu, 07 Oct 2010 21:25:00 GMThttp://www.shnenglu.com/biao/archive/2010/10/08/128986.htmlhttp://www.shnenglu.com/biao/comments/128986.htmlhttp://www.shnenglu.com/biao/archive/2010/10/08/128986.html#Feedback0http://www.shnenglu.com/biao/comments/commentRss/128986.htmlhttp://www.shnenglu.com/biao/services/trackbacks/128986.htmlConverting an expression of a given type into another type is known as type-casting. We have already seen some ways to type cast:

Implicit conversion

Implicit conversions do not require any operator. They are automatically performed when a value is copied to a compatible type. For example:

short a=2000;
int b;
b=a;

Here, the value of a has been promoted from short to int 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 (short to intint to floatdouble to int...), to or from bool, 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.

Implicit conversions also include constructor or operator conversions, which affect classes that include specific constructors or operator functions to perform conversions. For example:

class A {};
class B { public: B (A a) {} };

A a;
B b=a;

Here, a implicit conversion happened between objects of class A and class B, because B has a constructor that takes an object of class A as parameter. Therefore implicit conversions from A to B are allowed.

Explicit conversion

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:

short a=2000;
int b;
b = (int) a;    // c-like cast notation
b = int (a);    // functional notation

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:

// class type-casting
#include <iostream>
using namespace std;

class CDummy {
    float i,j;
};

class CAddition {
	int x,y;
  public:
	CAddition (int a, int b) { x=a; y=b; }
	int result() { return x+y;}
};

int main () {
  CDummy d;
  CAddition * padd;
  padd = (CAddition*) &d;
  cout << padd->result();
  return 0;
}
 

The program declares a pointer to CAddition, but then it assigns to it a reference to an object of another incompatible type using explicit type-casting:

padd = (CAddition*) &d;

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 result will produce either a run-time error or a unexpected result.

In order to control these types of conversions between classes, we have four specific casting operators: dynamic_castreinterpret_caststatic_cast and const_cast. Their format is to follow the new type enclosed between angle-brackets (<>) and immediately after, the expression to be converted between parentheses.

dynamic_cast <new_type> (expression)
reinterpret_cast <new_type> (expression)
static_cast <new_type> (expression)
const_cast <new_type> (expression)

The traditional type-casting equivalents to these expressions would be:

(new_type) expression
new_type (expression)

but each one with its own special characteristics:

dynamic_cast: 转换子类的指?或引?为父cȝ指针(或引?

dynamic_cast 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.

Therefore, dynamic_cast is always successful when we cast a class to one of its base classes:

class CBase { };
class CDerived: public CBase { };

CBase b; CBase* pb;
CDerived d; CDerived* pd;

pb = dynamic_cast<CBase*>(&d);     // ok: derived-to-base
pd = dynamic_cast<CDerived*>(&b);  // wrong: base-to-derived

The second conversion in this piece of code would produce a compilation error since base-to-derived conversions are not allowed with dynamic_cast unless the base class is polymorphic.

When a class is polymorphic, dynamic_cast performs a special checking during runtime to ensure that the expression yields a valid complete object of the requested class:

// dynamic_cast
#include <iostream>
#include <exception>
using namespace std;

class CBase { virtual void dummy() {} };
class CDerived: public CBase { int a; };

int main () {
  try {
    CBase * pba = new CDerived;
    CBase * pbb = new CBase;
    CDerived * pd;

    pd = dynamic_cast<CDerived*>(pba);
    if (pd==0) cout << "Null pointer on first type-cast" << endl;

    pd = dynamic_cast<CDerived*>(pbb);
    if (pd==0) cout << "Null pointer on second type-cast" << endl;

  } catch (exception& e) {cout << "Exception: " << e.what();}
  return 0;
}
Null pointer on second type-cast

Compatibility note: dynamic_cast 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 dynamic_cast to work properly.

The code tries to perform two dynamic casts from pointer objects of type CBase* (pba and pbb) to a pointer object of type CDerived*, but only the first one is successful. Notice their respective initializations:

CBase * pba = new CDerived;
CBase * pbb = new CBase;

Even though both are pointers of type CBase*pba points to an object of type CDerived, while pbb points to an object of type CBase. Thus, when their respective type-castings are performed using dynamic_castpba is pointing to a full object of class CDerived, whereas pbb is pointing to an object of class CBase, which is an incomplete object of class CDerived.

When dynamic_cast 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 dynamic_cast is used to convert to a reference type and the conversion is not possible, an exception of type bad_cast is thrown instead.

dynamic_cast can also cast null pointers even between pointers to unrelated classes, and can also cast pointers of any type to void pointers (void*).

static_cast: 指针的{? 1. 子类和父cM间指针互相{?不进行安全检?. 非指针的转换: 2. 标准隐式转换(如int->float, double->int). 3. 用户定义转换(构造函数{换,转换函数)

static_cast 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 dynamic_cast is avoided.

class CBase {};
class CDerived: public CBase {};
CBase * a = new CBase;
CDerived * b = static_cast<CDerived*>(a);

This would be valid, although b would point to an incomplete object of the class and could lead to runtime errors if dereferenced.

static_cast 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:

double d=3.14159265;
int i = static_cast<int>(d); 

Or any conversion between classes with explicit constructors or operator functions as described in "implicit conversions" above.

reinterpret_cast: 1. M指针之间的相互{换,即ɘq些cd之间没有M关系. 2. 指针和整数类型的怺转换(指针->int时在Mac上会报错: loses precision).

reinterpret_cast 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.

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.

The conversions that can be performed by reinterpret_cast but not by static_cast 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:

class A {};
class B {};
A * a = new A;
B * b = reinterpret_cast<B*>(a);

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.

const_cast: 转换对象(primitivecd的不可以: int, float, double...)Q指针,引用的const属性,有则LQ没有则加上

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:

// const_cast
#include <iostream>
using namespace std;

void print (char * str)
{
  cout << str << endl;
}

int main () {
  const char * c = "sample text";
  print ( const_cast<char *> (c) );
  return 0;
}
sample text

typeid

typeid allows to check the type of an expression:

typeid (expression)

This operator returns a reference to a constant object of type type_info that is defined in the standard header file <typeinfo>. This returned value can be compared with another one using operators == and != or can serve to obtain a null-terminated character sequence representing the data type or class name by using its name() member.

// typeid
#include <iostream>
#include <typeinfo>
using namespace std;

int main () {
  int * a,b;
  a=0; b=0;
  if (typeid(a) != typeid(b))
  {
    cout << "a and b are of different types:\n";
    cout << "a is: " << typeid(a).name() << '\n';
    cout << "b is: " << typeid(b).name() << '\n';
  }
  return 0;
}
a and b are of different types:
a is: int *
b is: int  

When typeid is applied to classes typeid 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:

// typeid, polymorphic class
#include <iostream>
#include <typeinfo>
#include <exception>
using namespace std;

class CBase { virtual void f(){} };
class CDerived : public CBase {};

int main () {
  try {
    CBase* a = new CBase;
    CBase* b = new CDerived;
    cout << "a is: " << typeid(a).name() << '\n';
    cout << "b is: " << typeid(b).name() << '\n';
    cout << "*a is: " << typeid(*a).name() << '\n';
    cout << "*b is: " << typeid(*b).name() << '\n';
  } catch (exception& e) { cout << "Exception: " << e.what() << endl; }
  return 0;
}
a is: class CBase *
b is: class CBase *
*a is: class CBase
*b is: class CDerived

Notice how the type that typeid considers for pointers is the pointer type itself (both a and b are of type class CBase *). However, when typeid is applied to objects (like *a and *btypeid yields their dynamic type (i.e. the type of their most derived complete object: 真实的类型,即子类对象使用的是父类的指针,但返回的子类的信?.

If the type typeid evaluates is a pointer preceded by the dereference operator (*), and this pointer has a null value, typeid throws a bad_typeid exception.



]]>
þ777߿ۿƷ| ŷƷƵһþþþƷ | ޹Ʒþһ| þþƷAV뽿ɫ | AVݺɫۺϾþ| ŷպƷþþþ| Ʒһþò| պӰþþñ| ľƷþþþùַ| պƷþþþþ| ҹƷþþþ| ھƷþþþþ鶹| þ91Ʒ91þû| þۺ༤| ɫۺϾþ| þþƷѹۿ| 91þþƷһ| þþƷ99þþùŴ| ӰɫۺϾþ| ձһþ| þþƷav| þAVĻ| AVһþ| һƷþð͹| ޹Ʒþþþ| þþѾƷre6| ˾þô߽| þ޾Ʒϵַ| ݺ޾þþþþۺ| 99þùۺϾƷӰԺ | պþþþƷӰԺҳ| 99þ99ֻѷѾƷ| 鶹AVһþ| þɧ| þ޴ɫĻþþ| պƷþþþþ| þþþAVר| þþƷ77777| ƷþþþӰԺ۲| Ʒ99þþƷ| һձȾþۺ|