• <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>

            公告

            <2025年5月>
            27282930123
            45678910
            11121314151617
            18192021222324
            25262728293031
            1234567

            統(tǒng)計

            • 隨筆 - 9
            • 文章 - 13
            • 評論 - 3
            • 引用 - 0

            常用鏈接

            留言簿(1)

            隨筆分類

            隨筆檔案

            文章分類

            文章檔案

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            2007年10月23日

            使用標準C++的類型轉(zhuǎn)換符:static_cast、dynamic_cast、reinterdivt_cast、和const_cast

            使用標準C++的類型轉(zhuǎn)換符:static_cast、dynamic_cast、reinterdivt_cast、和const_cast。

            3.1 static_cast
            用法:static_cast < type-id > ( exdivssion )
            該運算符把exdivssion轉(zhuǎn)換為type-id類型,但沒有運行時類型檢查來保證轉(zhuǎn)換的安全性。它主要有如下幾種用法:
            ①用于類層次結(jié)構(gòu)中基類和子類之間指針或引用的轉(zhuǎn)換。
              進行上行轉(zhuǎn)換(把子類的指針或引用轉(zhuǎn)換成基類表示)是安全的;
              進行下行轉(zhuǎn)換(把基類指針或引用轉(zhuǎn)換成子類表示)時,由于沒有動態(tài)類型檢查,所以是不安全的。
            ②用于基本數(shù)據(jù)類型之間的轉(zhuǎn)換,如把int轉(zhuǎn)換成char,把int轉(zhuǎn)換成enum。這種轉(zhuǎn)換的安全性也要開發(fā)人員來保證。
            ③把空指針轉(zhuǎn)換成目標類型的空指針。
            ④把任何類型的表達式轉(zhuǎn)換成void類型。

            注意:static_cast不能轉(zhuǎn)換掉exdivssion的const、volitale、或者__unaligned屬性。


            3.2 dynamic_cast
            用法:dynamic_cast < type-id > ( exdivssion )
            該運算符把exdivssion轉(zhuǎn)換成type-id類型的對象。Type-id必須是類的指針、類的引用或者void *;
            如果type-id是類指針類型,那么exdivssion也必須是一個指針,如果type-id是一個引用,那么exdivssion也必須是一個引用。

            dynamic_cast主要用于類層次間的上行轉(zhuǎn)換和下行轉(zhuǎn)換,還可以用于類之間的交叉轉(zhuǎn)換。
            在類層次間進行上行轉(zhuǎn)換時,dynamic_cast和static_cast的效果是一樣的;
            在進行下行轉(zhuǎn)換時,dynamic_cast具有類型檢查的功能,比static_cast更安全。
            class B{
            public:
            int m_iNum;
            virtual void foo();
            };

            class D:public B{
            public:
            char *m_szName[100];
            };

            void func(B *pb){
            D *pd1 = static_cast(pb);
            D *pd2 = dynamic_cast(pb);
            }

            在上面的代碼段中,如果pb指向一個D類型的對象,pd1和pd2是一樣的,并且對這兩個指針執(zhí)行D類型的任何操作都是安全的;
            但是,如果pb指向的是一個B類型的對象,那么pd1將是一個指向該對象的指針,對它進行D類型的操作將是不安全的(如訪問m_szName),
            而pd2將是一個空指針。

            另外要注意:B要有虛函數(shù),否則會編譯出錯;static_cast則沒有這個限制。
            這是由于運行時類型檢查需要運行時類型信息,而這個信息存儲在類的虛函數(shù)表(
            關(guān)于虛函數(shù)表的概念,詳細可見)中,只有定義了虛函數(shù)的類才有虛函數(shù)表,
            沒有定義虛函數(shù)的類是沒有虛函數(shù)表的。

            另外,dynamic_cast還支持交叉轉(zhuǎn)換(cross cast)。如下代碼所示。
            class A{
            public:
            int m_iNum;
            virtual void f(){}
            };

            class B:public A{
            };

            class D:public A{
            };

            void foo(){
            B *pb = new B;
            pb->m_iNum = 100;

            D *pd1 = static_cast(pb); //compile error
            D *pd2 = dynamic_cast(pb); //pd2 is NULL
            delete pb;
            }

            在函數(shù)foo中,使用static_cast進行轉(zhuǎn)換是不被允許的,將在編譯時出錯;而使用 dynamic_cast的轉(zhuǎn)換則是允許的,結(jié)果是空指針。


            3.3 reindivter_cast
            用法:reindivter_cast (exdivssion)
            type-id必須是一個指針、引用、算術(shù)類型、函數(shù)指針或者成員指針。
            它可以把一個指針轉(zhuǎn)換成一個整數(shù),也可以把一個整數(shù)轉(zhuǎn)換成一個指針(先把一個指針轉(zhuǎn)換成一個整數(shù),
            在把該整數(shù)轉(zhuǎn)換成原類型的指針,還可以得到原先的指針值)。

            該運算符的用法比較多。

            3.4 const_cast
            用法:const_cast (exdivssion)
            該運算符用來修改類型的const或volatile屬性。除了const 或volatile修飾之外, type_id和exdivssion的類型是一樣的。
            常量指針被轉(zhuǎn)化成非常量指針,并且仍然指向原來的對象;
            常量引用被轉(zhuǎn)換成非常量引用,并且仍然指向原來的對象;常量對象被轉(zhuǎn)換成非常量對象。

            Voiatile和const類試。舉如下一例:
            class B{
            public:
            int m_iNum;
            }
            void foo(){
            const B b1;
            b1.m_iNum = 100; //comile error
            B b2 = const_cast(b1);
            b2. m_iNum = 200; //fine
            }
            上面的代碼編譯時會報錯,因為b1是一個常量對象,不能對它進行改變;
            使用const_cast把它轉(zhuǎn)換成一個常量對象,就可以對它的數(shù)據(jù)成員任意改變。注意:b1和b2是兩個不同的對象。
             

            == ===========================================

            == dynamic_cast .vs. static_cast
            == ===========================================

            class B { ... };
            class D : public B { ... };

            void f(B* pb)
            {

            D* pd1 = dynamic_cast(pb);

            D* pd2 = static_cast(pb);
            }

            If pb really points to an object of type D, then pd1 and pd2 will get the same value. They will also get the same value if pb == 0.

            If pb points to an object of type B and not to the complete D class, then dynamic_cast will know enough to return zero. However, static_cast relies on the programmer’s assertion that pb points to an object of type D and simply returns a pointer to that supposed D object.

            dynamic_cast可用于繼承體系中的向下轉(zhuǎn)型,即將基類指針轉(zhuǎn)換為派生類指針,比static_cast更嚴格更安全。dynamic_cast在執(zhí)行效率上比static_cast要差一些,但static_cast在更寬上范圍內(nèi)可以完成映射,這種不加限制的映射伴隨著不安全性。static_cast覆蓋的變換類型除類層次的靜態(tài)導航以外,還包括無映射變換、窄化變換(這種變換會導致對象切片,丟失信息)、用VOID*的強制變換、隱式類型變換等...


            == ===========================================
            == static_cast .vs. reinterdivt_cast
            == ================================================

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

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

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

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

            int n=9;

            double d=reinterdivt_cast (n);

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

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

            posted @ 2007-10-23 20:28 blues 閱讀(3176) | 評論 (0)編輯 收藏

            2007年10月22日

            offsetof 解析

             1offsetof(s,m)解析 offsetof(s,m)解析
             2 
             3今天看代碼時,發(fā)現(xiàn)一個有用的東東,offsetof(s,m),這是一個宏,定義如下:
             4
             5 #define offsetof(s,m) (size_t)&(((s *)0)->m)
             6 
             7 然后到網(wǎng)上查了一下,發(fā)現(xiàn)還真的是很有用,附帶一位大俠的解說:
             8
             9  struct   AAA   
            10  {   
            11  int   i;   
            12  int   j;   
            13  }
            ;   
            14    
            15  struct AAA *pAAA;   
            16  pAAA=new AAA;   
            17  這時,pAAA實際上是一個Pointer, 指向某一確定的內(nèi)存地址,比如0x1234;   
            18  而 pAAA->i 整體是一個int型變量,其地址是&(pAAA->i) ,'&'為取址運算符;   
            19  那么&(pAAA->i)一定等于0x1234,因為i是結(jié)構(gòu)體AAA的第一個元素。   
            20  而&(pAAA->j)一定是0x1234 + 0x4 = 0x1238; 因為sizeof(int= 4;
            21    
            22  這個做法的巧妙之處就是:它把“0”作為上例中的pAAA,那么 &(pAAA->j)就是j的   
            23  offset啦
            24
            25  解析結(jié)果是:   
            26  (s   *)0 ,將 0 強制轉(zhuǎn)換為Pointer to   "s"     
            27  可以記 pS = (s *)0 ,pS是指向s的指針,它的值是0;   
            28  那么pS->m就是m這個元素了,而&(pS->m)就是m的地址,而在本例中就是offset啦   
            29    
            30  再把結(jié)果強制轉(zhuǎn)換為size_t型的就OK 了,size_t其實也就是int啦!!    
            31 
            32 
            33

            posted @ 2007-10-22 16:02 blues 閱讀(4207) | 評論 (2)編輯 收藏

            2007年1月26日

            2006年世界頂級殺毒軟件排名

            http://blog.cnetnews.com.cn/hand/keji/3658/

            posted @ 2007-01-26 14:55 blues 閱讀(279) | 評論 (0)編輯 收藏

            2006年12月11日

            com中以結(jié)構(gòu)體作為接口參數(shù)

            ?1 通過VARIANT;???
            ?2 ??VARIANT???varData;(出參)???
            ?3 ????
            ?4 ??MYSTRUCT??? * pMyData??? = ???NULL;???
            ?5 ????
            ?6 ??pMyData??? = ???(MYSTRUCT * )CoTaskMemAlloc( sizeof (MYSTRUCT));???
            ?7 ??.???
            ?8 ????
            ?9 ??varData.byref??? = ???(LPVOID)pMyData;???
            10 ????
            11 ??在調(diào)用文件里,同樣定義???
            12 ??VARIANT???varData(入?yún)ⅲ???
            13 ????
            14 ??MYSTRUCT??? * pMyData??? = ???NULL;???
            15 ????
            16 ??pMyData??? = ???(MYSTRUCT * )varData.byref;???
            17 ????
            18 ??CoTaskMemFree((LPVOID)pMyData);

            posted @ 2006-12-11 15:39 blues 閱讀(973) | 評論 (0)編輯 收藏

            2006年11月7日

            Getting an (ATL) ActiveX control to print from Office Applications

            13 votes for this article.
            Popularity: 4.73. Rating: 4.25 out of 5.

            Introduction

            Seeing as this is my first ever post to CodeProject, let me do a quick introduction as to who I am and what I do. I have been working in one form of C and C++ or another for as long as I can remember (among the other myriad of languages that I've run into). Nowadays, most of my development is focused on Microsoft Windows platforms, and is done in VC6, VC2002.NET. I am heavily entrenched in BI (Business Intelligence) development, and in my spare time develop little ActiveX controls and games etc...

            The plot

            Having learnt COM a while ago, I made the obvious progression to ATL to ease the development of boilerplate code, and to leverage off Microsoft's template library. As my experience grew, I ventured into creating ActiveX controls using the ATL framework ... and life was good. I could spew out a fairly useful (albeit not overly complex) control within a short period of time. Recently, I was asked to create a KPI (Key Performance Indicator) control that could be embedded in a web page and an Excel document. Obviously based on my experience (which was obviously not vast) I thought that this would be no problem and off I went, creating code that would meet the functional spec (we all work to these don't we :)).

            A couple of days later the control was finished and the final tests were being run when someone asked me to print a hardcopy of an example spreadsheet with the embedded control. This is where my nightmares began. Not only did my control not print, but there was no clear indication as to why it didn't print. And so my exploration into this apparent mystery began.

            Have you ever tried to include 3rd party ActiveX controls into an Office document? They sure seem to work fine, but most (apart from the Microsoft controls) don't seem to render themselves when you request a Print Preview or a simple Print of the worksheet or document. So, if any of you have ever had this problem, or have never dabbled with this, but think that you may be heading this way, take note of this, cos it might save you hours of frustration and frantic searching on MSDN and Google.

            So what now?

            The first thing one needs to realize is that even though we have been blessed with Office 2000 and Office XP, the printing architecture still uses the old Windows-format metafile for its printing operations. This metafile format was used in 16-bit Windows-based applications (thinks back to Win3.1). Now, this becomes a major problem for ActiveX developers who wish their controls to be printable from within Office applications, because this old metafile format only supported a limited set of GDI functionality. The list of supported GDI functions can be found here.

            Now that you are armed with your limited function set, you cringe with the realization that you can no longer create memory DC's, you can no longer use your lovely DrawText() functions and you can definitely no longer call GetTextExtentPoint32() function. However, those realizations only hold true for the instance of when your control is being rendered to an old format metafile. So how do we empower our control to know that its being rendered to an old format metafile? Simple, we use the GetObjectType() function and check if the result is equal to OBJ_METADC (old metafile format):

            HRESULT Cxxxxx::OnDraw(ATL_DRAWINFO& di)
            {
                HDC hdc = di.hdcDraw;
                bool bMetaFile = false;
            
                //// lets check if we're drawing to an old// metafile format.. (like Office printing)//if ( GetObjectType(hdc) == OBJ_METADC )
                    bOldMetaFile = true;
            
                //// the rest of your code...//
            }

            For interest, the opposite of OBJ_METADC is OBJ_ENHMETADC (refer to this MSDN document).

            Now that we know if we're drawing to an old metafile format or not, we can write adaptive code to cater for each instance or we can just write all our drawing logic using the limited set of functionality that is supported by the old metafile DC.

            What about fonts and text extents?

            As any ATL ActiveX developer knows, using fonts in AX controls provides for limited amount of fun. The typical piece of code would probably look something like this:

            																//
            																// ... some code
            																//
                CComQIPtr<IFont, &IID_IFont> pFont(m_pFont);
                TEXTMETRICOLE tm;if ( pFont != NULL )
                {
                    pFont->get_hFont(&newFont);
                    pFont->AddRefHfont(newFont);
                    pFont->QueryTextMetrics(&tm);
                    oldFont = (HFONT) SelectObject(dc, newFont);
                }

            The Bolded lines of code are ones that I didn't use regularly, due to the fact that I didn't really need to know about the breakdown of my font's details because I had access to GetTextExtentPoint32() function. Unfortunately, in this scenario, we don't have access to that function to determine how wide (in pixels) our text is going to be. But there is another way to calculate this fairly accurately, as is demonstrated in the code below:

            																//
            																// assume that we have called QueryTextMetrics() and
            																// have a filled TEXTMETRICOLE structure called tm
            																//
            CComBSTR strText(_T("Hello, world"));
            SIZE sz;
            
            sz.cx = strText.Length() * tm.tmAveCharWidth;
            sz.cy = tm.tmHeight;

            Having said this, there are many other functions that I use a lot that I can't use if I want my ActiveX control to be printable by Office, but as with GetTextExtentPoint32() and its respective replacement, there is always a way to replace these functions using Old-Metafile-Safe-Drawing-Code (OMSDC). *maybe that acronym will catch on*

            Conclusion

            When creating an ActiveX control that you know will be used inside Office applications, and will most probably be printed, remember to stick to these guidelines when developing your drawing logic. I was fairly shocked by how little information was available in the MSDN and online in general, while I was searching for information on how to enable my ActiveX control to print from within an Office application. There are hundreds of documents on ActiveX controls being printed from within Internet Explorer, but none address this particular issue. Perhaps I was looking in the wrong places. Hopefully this article will help one or more of you one day ;)

            Acknowledgment

            Many thanks to Igor Tandetnik for pointing me in the right direction on this.

            About Peter Mares

            posted @ 2006-11-07 17:38 blues 閱讀(520) | 評論 (0)編輯 收藏
            轉(zhuǎn)===如何用ATL創(chuàng)建ActiveX控件(牛人翻譯的)

            http://www.czvc.com/down.asp?id=105

            posted @ 2006-11-07 16:53 blues 閱讀(511) | 評論 (0)編輯 收藏

            2006年9月12日

            DB2免費版

            http://www.ibm.com/developerworks/cn/downloads/im/udbexp/

            posted @ 2006-09-12 14:08 blues 閱讀(417) | 評論 (0)編輯 收藏

            2006年8月25日

            美國讓人噴飯的法律

            ?

            這美國還有這等噴飯的法律,實屬大開眼界,看來有機會要好好善加利用了!!現(xiàn)與各位一起分享:?
            美國聯(lián)邦法律規(guī)定:?

            1)不得與豪豬發(fā)生性關(guān)系。(*,誰敢呀)?
            2)每周四晚6:00以后不得放P。(以后還真要小心了,別一不留神坐牢了還不知為?
            啥)?
            3)任何人不得銷售其子女。(好象中國也不許吧)?

            阿拉巴馬州:?
            無論任何時候,將冰激淋卷放在口袋里是違法的。(有病丫的)?

            阿肯色州:?
            男性可以合法毆打其配偶,但每月最多一次。(估計很多東北的兄弟知道了一定想?
            移民阿肯色了,可也有例外呀,克林頓就是阿肯色的前州長,咋老被喜萊莉扁呀)?

            亞利桑納州:?
            任何房間中不得有兩根以上的假****。(估計那州的最高法官丫是個變態(tài)狂!)?

            夏威夷州:?
            不得將谷物放在耳朵里。(神經(jīng)病,以為偷太空種子呀)?

            印弟安納州:?
            1)任何年滿18歲的男性,若與17歲以下的女性發(fā)生性關(guān)系,而且當時她又沒穿鞋?
            襪,那將課重罪。(兄弟們千萬注意了呀!別全脫了)?
            2)圓周率在該州法定為4。(活活氣死咱祖沖之前輩呀!)?

            愛荷華州:?
            1)任何只有一只上臂的鋼琴演奏者必須免費演奏。(嚴重歧視殘疾藝術(shù)表演家)?
            2)任何有胃病的男性不得在公共場所與女性接吻。(接吻和胃有關(guān)系嗎?男性胃癌?
            晚期患者的福音)?

            紐約州:?
            1)不得僅為娛樂而將球砸向他人腦袋。(謀殺可以不?真的腦子進水了)?
            2)10:00以后不得穿拖鞋。(光腳吧)?

            新澤西州:?
            凡謀殺時不得穿防彈背心。(管得著嗎,警察這么沒自信!)?

            北卡州:?
            任何一位未婚男性與一為未婚女性,如果在任何旅館或汽車旅館登記為已婚,那么?
            他們即算合法夫妻了。(想帶小蜜開房的兄弟們千萬別去那州呀!)?

            賓西法尼亞州:?
            不得在浴室唱歌。(難怪在賓大商學院的同胞都不會K歌)?

            南卡州:?
            僅在每周六,男性被允許在法院的門前臺階上合法毆打其配偶。(這是啥規(guī)定,郁?
            悶ING)?

            猶他州:?
            1)不喝牛奶違法。(喝不完援助非洲難民呀,干么為難自己!難怪俺一只要喝牛?
            奶就拉肚子的朋友從猶大轉(zhuǎn)到紐約了,保命要緊呀。)?
            2)不得在正在執(zhí)行急救任務的救護車后座上Make?Love。(這好理解,怕病人看見血管?
            爆裂么!哈哈)?

            posted @ 2006-08-25 17:53 blues 閱讀(525) | 評論 (0)編輯 收藏

            2006年8月23日

            上海印象

            posted @ 2006-08-23 15:21 blues 閱讀(379) | 評論 (1)編輯 收藏
            僅列出標題  
            久久夜色撩人精品国产| 久久人妻少妇嫩草AV蜜桃| 国产成人久久精品麻豆一区| 精品久久国产一区二区三区香蕉| 久久久久久毛片免费看| 亚洲精品无码专区久久久| 国产精品久久99| 亚洲美日韩Av中文字幕无码久久久妻妇| 久久精品国产亚洲AV不卡| 久久九九亚洲精品| 亚洲中文字幕久久精品无码喷水| 岛国搬运www久久| 日韩精品久久无码中文字幕| 久久夜色精品国产| 免费国产99久久久香蕉| 久久亚洲中文字幕精品有坂深雪| 久久精品中文字幕一区| 久久精品无码专区免费东京热| 中文字幕亚洲综合久久菠萝蜜| 色综合久久88色综合天天| 亚洲精品国产美女久久久| 久久国产视频99电影| 99国产欧美精品久久久蜜芽| 2021国产精品午夜久久| 精品免费久久久久国产一区| 久久99国产综合精品| 超级97碰碰碰碰久久久久最新| 久久国产精品免费一区| 韩国无遮挡三级久久| 久久婷婷激情综合色综合俺也去| 精品一二三区久久aaa片| 久久免费视频6| 很黄很污的网站久久mimi色| 久久综合综合久久97色| a高清免费毛片久久| 国产精品美女久久久久| www.久久精品| 亚洲午夜久久影院| 国产精品成人99久久久久| 99久久精品九九亚洲精品| 精品无码人妻久久久久久 |