• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            隨筆 - 505  文章 - 1034  trackbacks - 0
            <2011年2月>
            303112345
            6789101112
            13141516171819
            20212223242526
            272812345
            6789101112


            子曾經(jīng)曰過:編程無他,唯手熟爾!

            常用鏈接

            留言簿(94)

            隨筆分類(649)

            隨筆檔案(505)

            相冊

            BCB

            Crytek

            • crymod
            • Crytek's Offical Modding Portal

            Game Industry

            OGRE

            other

            Programmers

            Qt

            WOW Stuff

            搜索

            •  

            積分與排名

            • 積分 - 914914
            • 排名 - 14

            最新隨筆

            最新評論

            閱讀排行榜

            評論排行榜


            static_cast  與 reinterpret_cast   

              reinterpret_cast是為了映射到一個完全不同類型的意思,這個關(guān)鍵詞在我們需要把類型映射回原有類型時用到它。我們映射到的類型僅僅是為了故弄玄虛和其他目的,這是所有映射中最危險的。(這句話是C++編程思想中的原話)

              static_cast 和 reinterpret_cast 操作符修改了操作數(shù)類型。它們不是互逆的; static_cast 在編譯時使用類型信息執(zhí)行轉(zhuǎn)換,在轉(zhuǎn)換執(zhí)行必要的檢測(諸如指針越界計算, 類型檢查). 其操作數(shù)相對是安全的。另一方面;reinterpret_cast 僅僅是重新解釋了給出的對象的比特模型而沒有進行二進制轉(zhuǎn)換, 例子如下:

              int n=9; double d=static_cast < double > (n);

              上面的例子中, 我們將一個變量從 int 轉(zhuǎn)換到 double。 這些類型的二進制表達式是不同的。 要將整數(shù) 9 轉(zhuǎn)換到 雙精度整數(shù) 9,static_cast 需要正確地為雙精度整數(shù) d 補足比特位。其結(jié)果為 9.0。而reinterpret_cast 的行為卻不同:

              int n=9;

              double d = reinterpret_cast<double  * > (n);

              這次, 結(jié)果有所不同. 在進行計算以后, d 包含無用值. 這是因為 reinterpret_cast 僅僅是復制 n 的比特位到 d, 沒有進行必要的分析.

              因此, 你需要謹慎使用 reinterpret_cast.



            Allows any pointer to be converted into any other pointer type. Also allows any integral type to be converted into any pointer type and vice versa.
            One practical use of reinterpret_cast is in a hash function, which maps a value to an index in such a way that two distinct values rarely end up with the same index.
            http://msdn.microsoft.com/en-us/library/e0w9f63b.aspx


            // 02_reinterpret_cast.cpp : 定義控制臺應用程序的入口點。
            //

            #include 
            "stdafx.h"
            #include 
            <iostream>

            using namespace std;

            // Returns a hash code based on an address
            unsigned short Hash( void *p ) 
            {
                unsigned 
            int val = reinterpret_cast<unsigned int>( p );
                
            return ( unsigned short )( val ^ (val >> 16));
            }

            int _tmain(int argc, _TCHAR* argv[])
            {
                
            int* pInt = NULL;
                
            // 把負數(shù)解釋為地址
                pInt = reinterpret_cast<int*>(-0x0024FFE4); 
                cout 
            << pInt << endl; // FFDB001C 地址
                
            // 4292542492,其十六進制表示正是FFDB001C
                cout << reinterpret_cast<unsigned int>(pInt) << endl; 
                cout 
            << UINT_MAX << endl; // 4294967295

                
            int iValue = 9;
                
            int* pValue = &iValue;
                cout 
            << &iValue << endl; // 0012FF1C 地址
                cout << pValue << endl;  // 0012FF1C 地址
                cout << *pValue << endl;  // 9 值

                cout 
            << reinterpret_cast<double*>(iValue) << endl; // 00000009 把iValue當作指針了
                cout << reinterpret_cast<double*>(pValue) << endl; // 0012FF1C 地址

                
            int a[20];
                
            for ( int i = 0; i < 20; i++ )
                    cout 
            << Hash( a + i ) << endl;

                
            return 0;
            }
            /* 輸出:
            FFDB001C
            4292542492
            4294967295
            0012FF1C
            0012FF1C
            9
            00000009
            0012FF1C
            65194
            65198
            65234
            65238
            65242
            65246
            65218
            65222
            65226
            65230
            65266
            65270
            65274
            65278
            65250
            65254
            65258
            65262
            65298
            65302

            */
            posted on 2011-02-16 20:00 七星重劍 閱讀(2509) 評論(0)  編輯 收藏 引用 所屬分類: PL--c/c++
            国产成人香蕉久久久久| 蜜臀av性久久久久蜜臀aⅴ麻豆 | 麻豆av久久av盛宴av| 伊人色综合九久久天天蜜桃| 久久婷婷五月综合国产尤物app| 蜜桃麻豆www久久| 久久久久se色偷偷亚洲精品av| 久久777国产线看观看精品| 一级a性色生活片久久无少妇一级婬片免费放| 狠狠色丁香婷婷久久综合五月 | 精品综合久久久久久88小说 | 国产精品久久久久乳精品爆| 精品国产乱码久久久久软件| 色综合久久中文综合网| 久久久av波多野一区二区| 一级a性色生活片久久无少妇一级婬片免费放| 久久超碰97人人做人人爱| 性做久久久久久免费观看| 婷婷综合久久狠狠色99h| 亚洲国产另类久久久精品小说| 久久国产成人| 99久久婷婷国产综合精品草原| 久久国产精品一国产精品金尊 | 久久精品国产99国产电影网| 亚洲精品美女久久久久99| 亚洲中文字幕伊人久久无码| 国产福利电影一区二区三区久久老子无码午夜伦不 | 性做久久久久久久久浪潮| 久久精品99无色码中文字幕| 伊人热人久久中文字幕| 久久久精品一区二区三区| 2020久久精品国产免费| 久久久久99精品成人片直播| 久久99精品久久久久久久不卡| 久久久久亚洲av无码专区| 久久人人爽人人爽人人AV| 无遮挡粉嫩小泬久久久久久久| 人妻无码精品久久亚瑟影视| 人妻精品久久久久中文字幕69| 国内精品伊人久久久久AV影院| 久久91精品久久91综合|